Hello. It has long been written on Delphi, and decided to warm up a little ... And the very first thing that occurred to me was to write a text editor. But I decided to complicate my goal a bit, to add the highlight of my native Pascal to this editor. SynEdit (SynMemo) and SynPasSyn came to the rescue. Well, in order to get a better look at SynEdit, I decided to use demos, namely HighlighterDemo.

This demo presents a component (TSynCustomHighlighter) with its own highlighting of the words hello , synEdit and world :

  TSynSampleSyn = class(TSynCustomHighlighter) private fRange: TRangeState; fTokenID: TtkTokenKind; fIdentFuncTable: array[0..3] of TIdentFuncTableFunc; ... KeyWords: array[0..3] of UnicodeString = ( 'hello', 'synedit', 'world', 'highlighter' ); KeyIndices: array[0..3] of Integer = ( 0, 2, 1, 3 ); procedure TSynSampleSyn.InitIdent; var i: Integer; begin for i := Low(fIdentFuncTable) to High(fIdentFuncTable) do if KeyIndices[i] = -1 then fIdentFuncTable[i] := AltFunc; fIdentFuncTable[0] := FuncHello; fIdentFuncTable[2] := FuncWorld; fIdentFuncTable[1] := FuncSynedit; fIdentFuncTable[3] := FuncHightLight; ... function TSynSampleSyn.FuncHightLight(Index: Integer): TtkTokenKind; begin if IsCurrentToken(KeyWords[Index]) then Result := tkKey else Result := tkIdentifier; end; 

But to make it so that the highlighter is highlighted also doesn’t work out ...

UPDATE Updated indexes, but still the highlighter not highlighted

  • And why did you assign the same indexes for hello and highlighter ? In my opinion, this is exactly the problem. Give the new keyword a new index. - zed
  • Fixed indexes, but no result - VeryBadUser

1 answer 1

If you carefully read the comments in SynHighlighterSample.pas , you can see the following lines:

 Code template generated with SynGen. The original code is: D:\Quellen\Komponenten\SynEdit\Demos\HighlighterDemo\SynHighlighterSample.pas, released 2008-10-25. Description: The initial author of this file is Maël Hörz. Copyright (c) 2008, all rights reserved. 

From which we can conclude that SynEdit uses code generation to create highlights, and the utility for generating code is called SynGen . This utility comes with SynEdit and has its own help ( SynGen\Highlighters-HowTo.html ).

If in a nutshell, then you need to write / edit a template for your backlight (a file with the .msg extension), open this template in SynGen and generate the source code. In the demo there is a template SynHighlighterSample.msg and to make the highlighter to highlight the word highlighter , just enter it in the KEYS section:

 KEYS Hello World Highlighter |><| 

However, manually changing the sorts of the ready highlighter (in terms of adding new keywords) will not work because of the TSynSampleSyn.HashKey function. This function counts the hash of words, which is then used to call the corresponding function from fIdentFuncTable .

In general, the HashKey function looks like this:

 {$Q-} function TSynSampleSyn.HashKey(Str: PWideChar): Cardinal; begin Result := 0; while IsIdentChar(Str^) do begin Result := Result * c + Ord(Str^) * d; inc(Str); end; Result := Result mod m; fStringLen := Str - fToIdent; end; {$Q+} 

And the task of the code generator is to calculate the coefficients c , d and m , depending on the keywords (the generator algorithm can be viewed in SynGen\HashTableGen.pas ).

  • Thank. All last night I dealt with this demo (HighlighterDemo). I understood how this backlight works, but I manually edited the KeyIndices and HashKey rules, but I could not figure out why I needed SynHighlighterSample.msg. But now everything is clear. Thanks again! +1 to your post)) - VeryBadUser