Author Topic: Diff files syntax highlight support  (Read 5298 times)

Teknomancer

  • Community Member
  • Posts: 23
  • Hero Points: 1
Diff files syntax highlight support
« on: September 16, 2010, 06:25:29 pm »
I noticed that while opening .diff files (patches) e.g svn diff > Currdiff.diff ; vs ./Currdiff.diff

SlickEdit does not recognize the diff files. I know the diff/patch is not a real "language" but many other editors (including gedit) support colouring diff files. Does someone have any readymade patches/files for this diff "language" ?

LBCEi

  • Senior Community Member
  • Posts: 158
  • Hero Points: 8
Re: Diff files syntax highlight support
« Reply #1 on: September 17, 2010, 07:02:47 am »
I asked essentially the same question a few years ago -- in this thread http://community.slickedit.com/index.php?topic=1963.0.  I thought it would be relatively easy (until I tried it) to set up a color coding lexer to color the diff files (like a "language").  It proved to be otherwise.

This question led to several issues/wishes related to the lexer setup: multiple 'user' tokens, regexp definitions for tokens,   etc.

So far these issues/wishes have not been addressed as far as I know, and it still is not possible to do what you want to do.  I still use my Word macro to color diff files for use in documentation.

Maybe your question will bring this a bit more attention.

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Diff files syntax highlight support
« Reply #2 on: September 17, 2010, 08:04:00 am »
I did this for another diff format and didn't seem to have any trouble.

I'll be happy to adjust my macro to support this .DIFF diff format as well and post the macro here.

I just need two things from you:
1.  Post a sample .DIFF file that shows the details of the format.  A link to a web page with a definition of the format would be helpful as well, but I still need a sample .DIFF file so I can test the macro without having to download and configure tools to generate my own .DIFF file for testing.
2.  Post a corresponding .TXT file that references specific line numbers from the sample .DIFF file, and say what color rule should apply to those lines.

Teknomancer

  • Community Member
  • Posts: 23
  • Hero Points: 1
Re: Diff files syntax highlight support
« Reply #3 on: September 17, 2010, 11:23:51 am »
The sample diff files I produce do not cover all the "elements" in the format.

But take a look at this URL: http://geany.pdarko.co.cc/filetypes/index.php?edit=filetypes.diff

It is a colour scheme editor for the Geany Editor, this shows an example diff file and the colour elements on the left side and updates them live as you change them. This is the format of the .diff files.

Also I think any line that doesn't begin with a space is non-content (i.e. the "Comment" shown in the above format applies to lines not just starting with '#' but anylines that do not start with a space afaik.

Example (colours in maroon shown below are for lines that don't begin with a space, which is what that above online colour editor refers to as "comment") :
Property changes on: .
___________________________________________________________________
Modified: svn:mergeinfo

   Merged /trunk:r57455

Tell me if you need more information, I don't think there is any format "format"/definition. We'd probably have to build one from the example I posted.

Teknomancer

  • Community Member
  • Posts: 23
  • Hero Points: 1
Re: Diff files syntax highlight support
« Reply #4 on: September 17, 2010, 11:42:15 am »
Also you can refer http://en.wikipedia.org/wiki/Diff (search for "Unified format").

LBCEi

  • Senior Community Member
  • Posts: 158
  • Hero Points: 8
Re: Diff files syntax highlight support
« Reply #5 on: September 17, 2010, 04:59:45 pm »
It sounds like Teknomancer and I are trying to color different diff formats.  I was trying to color a 'normal' unix style diff and he is trying to do the 'unified' diff format.

Attached is a sample of the 'normal' diff format after coloring with my Word macro.  '>' at the beginning of a line indicates a 'new' line and those lines are colored blue.  '<' at the beginning of a line indicates an 'old' line and those lines are colored red. 

My macro also checks for 'w>' and 'w<' at the beginning of a line (indicating 'new' and 'old' lines with only white space changes and colors them gray (no examples of this in the attached image).  The whitespace only markings aren't really standard diff format markings and could be skipped (cvs diff generates these but the diff I usually use (lately) -- the generate patch feature of WinMerge -- doesn't).

I tried to generate a SE macro to do this but never was successful.


chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Diff files syntax highlight support
« Reply #6 on: September 17, 2010, 10:35:50 pm »
Ok good, my macro already recognizes and colors Normal format, and Unified format is easy to add (Context format and RCS format are a little more complex and I don't feel like adding support for them, though someone else could extend my macro to handle them).

I'll see about sharing an updated macro this weekend.

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Diff files syntax highlight support
« Reply #7 on: September 18, 2010, 07:45:47 am »
I think the attached macro will do what you're looking for.

Load the module, and it will create a new custom DIFF language type.
The new DIFF language type automatically colors various lines, and recognizes both Normal and Unified diffs.
It has no tagging support, because it doesn't make sense to try to parse tags out of diffs.
Note that colors are only applied when the file is opened:  if you edit the file it's not going to keep reapplying colors as you edit.

If you don't like the yellow/red colors, modify the RGB_ color constants near the top of the macro file to suit your taste.
« Last Edit: September 19, 2010, 06:19:29 am by chrisant »

LBCEi

  • Senior Community Member
  • Posts: 158
  • Hero Points: 8
Re: Diff files syntax highlight support
« Reply #8 on: September 18, 2010, 11:19:37 pm »
Very cool! Thanks :)

It seems that the regex test on lines 151 & 152 doesn't consistently match the lines that mark add/change/delete areas.  I just commented out those two lines and that seems to work for me.  See attached screenshots.

Does this use the same coloring mechanism that is used for coloring regular language buffers?  If I copy the text from the colored diff buffer and paste into Word, for example, the colors are not retained.  They are retained when I copy from a cpp buffer.  I guess I can just continue to use my Word macro if I need to put diffs into a document (such as a Version Description doc).  I'd be willing to share that Word macro if others are interested.

Can you explain what you meant in the comment "Scan difflog file for revisions to show in the Defs toolbar."?  I don't see anything in the Defs toolbar for that buffer (except the filename of course).  I wouldn't really expect to since, as you said, it doesn't make sense to parse tags from a diff.

Thanks again
Les

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Diff files syntax highlight support
« Reply #9 on: September 19, 2010, 06:27:19 am »
Quote
It seems that the regex test on lines 151 & 152 doesn't consistently match the lines that mark add/change/delete areas.  I just commented out those two lines and that seems to work for me.
I added that test at the last minute.  If you look at the regex, there is an obvious simple typo:  there should be a question mark immediately preceding the dollar sign.  The reason I'd asked for a sample .DIFF file was to help calibrate the macro to avoid that kind of bug.  :(  I updated the macro file attached to the earlier post, to include the fix for the regex typo, and clean up a couple other minor issues (for example not applying coloring to the first line in the file).

Quote
Does this use the same coloring mechanism that is used for coloring regular language buffers?
No:  It uses the same coloring mechanism as the Symbol Coloring feature uses.  The basic keyword/comment coloring capabilities are not sophisticated enough to be able to color diffs.  Copy/paste does not include any Symbol Coloring colors, just like it does not include these colors.

@SlickTeam -- I wish there were a flag in a stream marker to apply its effects to the RTF/HTML clipboard, and I wish that Symbol Coloring used that flag.  I would also use that flag in this macro, and in a few other macros.  In contrast, I would not use that flag when hilighting incremental search matches, for example.

Quote
Can you explain what you meant in the comment "Scan difflog file for revisions to show in the Defs toolbar."?
I mentioned earlier that this is a quick derivative of a macro I already had for a different purpose.  Apparently I missed removing a comment that alluded to the original macro.  The comment is not relevant in the context of this macro, and has been removed.

LBCEi

  • Senior Community Member
  • Posts: 158
  • Hero Points: 8
Re: Diff files syntax highlight support
« Reply #10 on: September 19, 2010, 03:10:27 pm »
Excellent.

Thanks for the update and explanations.

Much appreciated.

Les

Teknomancer

  • Community Member
  • Posts: 23
  • Hero Points: 1
Re: Diff files syntax highlight support
« Reply #11 on: September 21, 2010, 02:28:28 pm »
Cool. This works well!! :D Thanks a lot!!