0

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!

JSON C11
  • 11,272
  • 7
  • 78
  • 65
C. Hediger
  • 434
  • 7
  • 22
  • Does this problem occur for all source files? How about a file just containing, say, **import** sys – David Heffernan Apr 11 '15 at 09:20
  • I have changed it to: // List of keywords KEYWORDCOUNT = 2; KEYWORDS: array [1..KEYWORDCOUNT] of UnicodeString = ( 'import' ); It could not recognize "import" it seems that its not a problem of the position in the array. It is a problem of the word itself... i have tried it with JScript. Words like import were recognized now. But i need python... – C. Hediger Apr 11 '15 at 09:27

1 Answers1

1

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

Community
  • 1
  • 1
Altronicx
  • 161
  • 1
  • 9