Author Topic: Detect keyword  (Read 2589 times)


  • Hero Member
  • *****
  • Posts: 951
Detect keyword
« on: April 30, 2021, 09:29:02 am »
How can I detect if a keyword in Synedit text?

For example SELECT (upper lower or mixed case), but not '-- SELECT', :SELECT, ... and so on?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2


  • Administrator
  • Hero Member
  • *
  • Posts: 8438
  • Debugger - SynEdit - and more
    • wiki
Re: Detect keyword
« Reply #1 on: April 30, 2021, 12:37:22 pm »
That depends what you are trying to do.

1) Do you want to write a new highlighter?
2) Or do you have a highlighter, and want your code to find out about the word at caret or word at x/y
3) Or something else?


2: Use either
Code: Pascal  [Select][+][-]
  1.     function GetHighlighterAttriAtRowCol(XY: TPoint; out Token: string;
  2.       out Attri: TSynHighlighterAttributes): boolean;
  3.     function GetHighlighterAttriAtRowColEx(XY: TPoint; out Token: string;
  4.       out TokenType, Start: Integer;
  5.       out Attri: TSynHighlighterAttributes): boolean;        
However there is no formal definition for "keyword".
You can compare the attri to the HL.KeywordAttr;
Or you can find out the value of TokenType for whatever HL you use (every HL has its own value for Keyword)

Some HL implement Hl.IsKeyword(txt); But that does not account for context (such as if it appears in strings/comments)

Note that comparing the Attr is only 99%.
You need to check that the HL does return the real Attr, and not a (modified) copy. And you need to repeat the check, on each update. So TokenType is better.

For example the Pas-HL for case labels:
Code: Pascal  [Select][+][-]
  1. case a of
  2. 1..9: ;
  3. CONST_NUM: ;
  4. end;
Will create a mixed new Attri  that says "1" is a label and a number.

The Sql HL has in its unit
Code: Pascal  [Select][+][-]
  1.   TtkTokenKind = (tkComment, tkDatatype, tkDefaultPackage, tkException,         // DJLP 2000-08-11
  2.     tkFunction, tkIdentifier, tkKey, tkNull, tkNumber, tkSpace, tkPLSQL,        // DJLP 2000-08-11
  3.     tkSQLPlus, tkString, tkSymbol, tkTableName, tkUnknown, tkVariable);         // DJLP 2000-08-11
Code: Pascal  [Select][+][-]
  1. if Tokentype = ord(tkKey)
Code: Pascal  [Select][+][-]
  1. function TSynSQLSyn.IsKeyword(const AKeyword: string): boolean;
  2. var
  3.   tk: TtkTokenKind;
  4. begin
  5.   tk := IdentKind(PChar(AKeyword));
  6.   Result := tk in [tkDatatype, tkException, tkFunction, tkKey, tkPLSQL,
  7.     tkDefaultPackage];
  8. end;
Indicates there are other values, also used for keywords. So pick the list that you are interested in.
« Last Edit: April 30, 2021, 12:52:45 pm by Martin_fr »


TinyPortal © 2005-2018