Recent

Author Topic: Detect keyword  (Read 635 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 634
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
Laz 2.0.10
FPC 3.2.0

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7266
  • 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?

1: https://wiki.lazarus.freepascal.org/SynEdit_Highlighter

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
  4.  
So
Code: Pascal  [Select][+][-]
  1. if Tokentype = ord(tkKey)
But
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;
  9.  
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