shysaur / Fragaria

Cocoa syntax highlighting text view
54 stars 21 forks source link

DIFF highlighting (feature request) #90

Open balthisar opened 8 years ago

balthisar commented 8 years ago

I'll look into this myself, too, as I have some time but I wanted to capture it here for discussion.

Currently SMLSyntaxColouringDelegate offers delegates for syntax groups allowing applications to customize the coloring of groups (i.e., the attributes, strings, etc.). However a line-by-line delegate method would be useful, too. Although I have in mind the specific case of highlighting diff results, I'm sure other line-based custom highlighting cases can be found.

Adding to the existing delegate seems like the logical place and would allow the application to pass back a block providing any arbitrary styling desired for the line while keeping Fragaria light-weight.

(Alternatively it could be handled similar to the error icons with a dedicated delegate, but this might limit the styling that can be applied without making Fragaria too heavy in an effort to be universal.)

Something like:

fragariaDocument:shouldColorLine:withBlock: and fragariaDocument:didColourLine:withBlock

I've not worked with Fragaria's internals since early this year so I don't know how aware the syntax highlighter is of individual lines. It's something I will dig into when I have a chance unless someone beats me to it.

shysaur commented 8 years ago

Fragaria's syntax highlighter is a mess, and I am seriously considering a total rewrite of it, but real life is getting in the way, and I keep thinking about it without actually coding anything.

How it works is, in pseudocode:

  for each syntax color (coloring group):
      match some regular patterns on a range of text, and color the matches.

Since you can't parse almost anything serious with regular expressions alone, each coloring group has some hack applied to it to make the colorer seem aware of the context, but it's still an ugly hack with no future.

For example, it still has serious problems handling multiline strings, despite the many hacks that were applied to the colorer to improve the situation, both by me and by the original authors.

A real fix would be a reimplementation of the parsing components of the syntax colorer in the form of a state machine (the same approach used by Scintilla). The issue is that the current language definition is rigidly tailored upon the current approach, so we still have to support the current parser somehow.

Back on topic, implementing your proposal is a bit of a problem with the current design of the syntax colourer, but I think you could still implement it manually by listening for text change notifications on Fragaria's text view and then setting the custom color on the text storage, instead than with temporary attributes.