Recent

Author Topic: [SOLVED] SynEdit and custom Highlighter  (Read 7540 times)

Dibo

  • Hero Member
  • *****
  • Posts: 1048
[SOLVED] SynEdit and custom Highlighter
« on: October 11, 2010, 08:56:20 pm »
Hi,

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?

Regards
« Last Edit: October 12, 2010, 12:31:29 pm by Dibo »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8099
  • Debugger - SynEdit - and more
    • wiki
Re: SynEdit and custom Highlighter
« Reply #1 on: October 11, 2010, 10:49:59 pm »
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: [Select]
        fHighlighter.StartAtLineIndex(CurTextIndex);
        while not fHighlighter.GetEol do begin
          fHighlighter.GetTokenEx(sToken,nTokenLen);
          attr := fHighlighter.GetTokenAttribute;

          DrawHiLightMarkupToken(attr,sToken,nTokenLen);

          fHighlighter.Next;
        end;
      end;

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:

  Attr1:=Highlighter.CreateTokenID('Attr1',clRed,clNone,[]);

  // Line 1, up to col 3
  Highlighter.AddToken(1,3,tkText);
  // Line 1, up to col 7
  Highlighter.AddToken(1,7,Attr2);


I will update the example

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: SynEdit and custom Highlighter
« Reply #2 on: October 12, 2010, 11:12:49 am »
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

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

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: SynEdit and custom Highlighter
« Reply #3 on: October 12, 2010, 11:22:13 am »
You can highlight the text until the middle of the line and "un-highlight" the beginning of the line.

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: SynEdit and custom Highlighter
« Reply #4 on: October 12, 2010, 11:27:42 am »
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.

 

TinyPortal © 2005-2018