[c#] Adding a custom styler or lexer in C# for scintilla/notepad++



  • c630e845-147c-4f37-baa2-4dbf45d8e825-image.png



  • @Ekopalypse Wow, that is a lot of extra code, thanks so much for helping to look into this.

    If I understand correctly, a lot of it is so called “boiler plate code”, needed to set up hooks and connections for Notepad++ and/or Scintilla. Connecting the wires, so to speak. And in your example ultimately the methods Lex() and Fold() are where you would code the behaviour that is specific for that language/lexer.

    I don’t have time right now, but I will look into this further later this week and I’ll update the example lexer and hopefully get it to work 😃 thanks again



  • @Bas-de-Reuver

    If you don’t have keywords, then you can look at it that way, yes.



  • @Bas-de-Reuver

    Keep an eye on the TODOs. I used i64 or u64 but in reality these are ints that depend on the architecture. i64 for x64 and i32 for x86 …



  • @Ekopalypse as for the i64 or u64, I figure you can just use IntPtr or UIntPtr which automatically adjusts for the 32bit or 64bit architecture at compile time.

    Anyway thanks again, I got it to work for the Edifact files, sort of, there still are some quirks and bugs. When you open the file, only the visible part is stylised, when you scroll down it’s all default white. When you then switch language to None and back to EdifactLexer, then it’s all styled correctly.

    Also, how do I get access to the text file from the Lex() function? In the example I’m using nppeditor.GetCharAt(pos) and it works, but I’ve seen an C++ examples where it goes straight to the ScintillaGateway for a character array. Btw I’m also looking at the Notepad++ built-in lexers, for tips in the source code. I’m only little familiar with C++ but they seem to use a weird for-construction and also rely on the Scintilla More() and Forward() functions, see for example here.

    I’ll try out some more things, check examples and try to update the example code.



  • @Bas-de-Reuver said in [c#] Adding a custom styler or lexer in C# for scintilla/notepad++:

    I figure you can just use IntPtr or UIntPtr

    sounds reasonable

    only the visible part is stylised

    It’s not with me, unless you do what I described as a note in
    the Lex method, you scroll an inactive window.
    The lex method is always called again for every change,
    even those to the visual area, and tells what needs to be
    rechecked from where to where.
    What I could imagine is that there might be a problem
    if you “style more” than Scintilla expects.
    I’ll check it out.

    how do I get access to the text fil

    by using either SCI_GETRANGEPOINTER or SCI_GETCHARACTERPOINTER

    C++ but they seem to use a weird for-construction and also rely on the Scintilla

    When using C++ one has the advantage to be able to
    use already existing auxiliary classes.
    Other languages could only realize this if they implement
    a further C++ interface, the IDocument.
    However, searching the C# documentation the only thing
    I found was this.
    This is also the meaning of the last paramter of the lex method,
    it is a pointer to the above mentioned interface.



  • @Bas-de-Reuver

    I see you have updated your repo with an example, let me try it.



  • @Ekopalypse said in [c#] Adding a custom styler or lexer in C# for scintilla/notepad++:

    I see you have updated your repo with an example, let me try it.

    You mean you’ll try the source code, or you want the release DLLs? I’ve just updated github and added the 32bit and 64bit dll files.



  • @Bas-de-Reuver

    I forked your repository and made some minor changes and a little reorganization in my fork.
    Maybe there is something there for you.
    One issue, maybe not, is the delegates and garbage collection.
    I’m not sure if my changes prevent that, but I played with it for some time and it didn’t crash anymore, but I’m still not 100% convinced that the problem is solved.



  • @Ekopalypse I’ve looked at the code, and the separate ILexer class is a good improvement, and the Lex() function accessing the text using the GetRangePointer is cleaner (probably faster too). Also, the use of the keywords styling.xml is good to have as an example.

    I’ve tried the new version and the styling is applied instantly when editing and also to new lines etc. I’d be happy to accept a pull request of your forked project, or shall I just add these changes to my example project?



  • @Bas-de-Reuver
    PR made.
    I will make the other ILexer methods examples in the next days.


Log in to reply