Forum > LCL

[SOLVED] SynEdit and custom Highlighter



In ../examples/SynEdit/SynPositionHighliter is a nice demo showing how to color some text. But TSynPositionHighlighter class always begin coloring from first char on each line. I do not know why it made such a restriction. This class like all highlighter classes inherit from TSynCustomHighlighter, so I think it is possible to write own highlighter but I can't understand how tokens work. I just want functionality like in RichEdit - set attributes on custom text. Maybe someone had a similar problem?


PositionHighlighter is very old, and could probably be improved.

A token is a part of the line9in pascal it may be a symbol, keyword, text, number,...). But  a token is not necessary bound to the flow of the text. It could spawn across many words, or end in the middle of the text.
The end of one token must always be the start of the next. No part of the line must be left out, no overlaps, and the order must be the same as in the line.

PositionHighlighter returns all text, that is not in user specified tokens, as tkText-tokens.

Now ideally, the user could define a token as from x1 to x2, but the PHl needs to check every new token added, and reject any token that overlaps (it can still fill the gaps with tktext)

So now you just specify the end pos of a token, and it starts from the end of the previous token (or start of line)

I did not invent that, nor do I say it's good. I just describe what I observed....

Anyway, if the highlighter is called as in synedit.pp line 3750

--- Code: ---        fHighlighter.StartAtLineIndex(CurTextIndex);
        while not fHighlighter.GetEol do begin
          attr := fHighlighter.GetTokenAttribute;



--- End code ---

Then it must iterate the whole line, without a gap.

Based on that, it should be possible to improve the position highlighter.

Also you only need to create one attr of yourself:


  // Line 1, up to col 3
  // Line 1, up to col 7

I will update the example

If I understand correctly (my english is bad), it will be possible highlight something like this?

--- Quote ---fsdfdsfdsdf fgf dgdfg dfg dfgdfg dfgdfg
dsf sdf sdf sdf sdsdfdf1234sdfdsfdsfsdf
dfg dfg dfgdfg dfgdfghdgfgdfgdfgdfgdfg
--- End quote ---

Because now, with AddToken procedure, I can highlight only from first pos on line:

--- Quote ---fsdfdsfdsdf fgf dgdfg dfg dfgdfg dfgdfg
dsf sdf sdf sdf sdsdfdf1234sdfdsfdsfsdf
dfg dfg dfgdfg dfgdfghdgfgdfgdfgdfgdfg
--- End quote ---

You can highlight the text until the middle of the line and "un-highlight" the beginning of the line.

That's right. I read again Martin_fr post and now I understand. The whole trick is to "wiping" text using the token tkText. It's work, thanks.


[0] Message Index

Go to full version