Search code examples
pythondelphisyntax-highlightinghighlightingsynedit

Delphi SynEdit does not recognize all Keywords from the list (Python)


I use SynEdit r117 from the Repository in my Delphi XE6 application. I would like to highlight Python code.

For that, I placed a SynEdit Component onto my Form. Additionaly I added the Component SynPythonSyn onto it. I have connected them through the Objectinspector.

Now I am able to highlight SOME Python keywords. After a few hours of searching, I opened the Sourcfile SynHighlighterPython.pas which was included in the Package ZIP of SynEdit.

There is a section with all the Keywords of python:

 // List of keywords
  KEYWORDCOUNT = 29;
  KEYWORDS: array [1..KEYWORDCOUNT] of UnicodeString =
    (
    'and',
    'assert',
    'break',
    'class',
    'continue',
    'def',
    'del',
    'elif',
    'else',
    'except',
    'exec',
    'finally',
    'for',
    'from',
    'global',
    'if',
    'import',
    'in',
    'is',
    'lambda',
    'not',
    'or',
    'pass',
    'print',
    'raise',
    'return',
    'try',
    'while',
    'yield'
    ); 

My problem is, that "exec" is the last highlighted keyword. All other in the list after "exec" will not be highlighted.

Does anyone have any idea what could cause this failure?

Thank you!


Solution

  • I was able to get the Syntax Highlighting working. It appears that the List of words populated in the SynHighligterPython.pas contains the Keywords and the NonKeywords in one list. Then it assumes that the combined list is sorted.

    So the problem arises in the Function IdentKind(Maybe:PWideChar) which uses FKeywords.Find(s, i) to search for the matching keyword. "Find" by definition only works on sorted list. My workaround was to change this line to use the indexof function to search as shown below.

    I := FKeywords.IndexOf(s);

    The search functions are documented on this link