Forum > SynEdit

Detect keyword

(1/1)

pcurtis:
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?

Martin_fr:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    function GetHighlighterAttriAtRowCol(XY: TPoint; out Token: string;      out Attri: TSynHighlighterAttributes): boolean;    function GetHighlighterAttriAtRowColEx(XY: TPoint; out Token: string;      out TokenType, Start: Integer;      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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---case a of1..9: ;CONST_NUM: ;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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  TtkTokenKind = (tkComment, tkDatatype, tkDefaultPackage, tkException,         // DJLP 2000-08-11    tkFunction, tkIdentifier, tkKey, tkNull, tkNumber, tkSpace, tkPLSQL,        // DJLP 2000-08-11    tkSQLPlus, tkString, tkSymbol, tkTableName, tkUnknown, tkVariable);         // DJLP 2000-08-11 So

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if Tokentype = ord(tkKey)But

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TSynSQLSyn.IsKeyword(const AKeyword: string): boolean;var  tk: TtkTokenKind;begin  tk := IdentKind(PChar(AKeyword));  Result := tk in [tkDatatype, tkException, tkFunction, tkKey, tkPLSQL,    tkDefaultPackage];end; Indicates there are other values, also used for keywords. So pick the list that you are interested in.

Navigation

[0] Message Index

Go to full version