Community
    • Login

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

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    70 Posts 5 Posters 15.6k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • EkopalypseE
      Ekopalypse
      last edited by

      The ILoader interface could help here.
      First load only the “visible” part and then more and more. But I suppose without Npp taking this into account as well, it might be difficult to implement this in a safe way.

      Bas de ReuverB 1 Reply Last reply Reply Quote 2
      • Bas de ReuverB
        Bas de Reuver @Ekopalypse
        last edited by

        @ekopalypse Thanks the SetIdleStyling seems to work as intended. When I set the following at the beginning, then large files will open faster

            var editor = new ScintillaGateway(PluginBase.GetCurrentScintilla());
        
            editor.SetIdleStyling(IdleStyling.ALL); // instead of default IdleStyling.NONE
        

        The file is not immediately colored, which is as expected, and the syntax coloring runs in the background. You can freely scroll to the end of a large >10MB csv file,. At first it’s white and then the syntax colors will apear from top to bottom with a short delay. Also, smaller files still work as well, if you open them they are immediately in the correct colors.

        I wonder though, why is IdleStyling.NONE the standard, why not always use IdleStyling.ALL by default?

        EkopalypseE 1 Reply Last reply Reply Quote 2
        • EkopalypseE
          Ekopalypse @Bas de Reuver
          last edited by

          @bas-de-reuver

          to be honest, I have no experience with this feature.
          I’ve never tested or researched what idle styling means for UI responsiveness in general, or when it was introduced and under what conditions at the time. If it really runs in the main Scintilla thread, then it could mean that on a machine that is always highly loaded, the editing part is significantly worse, meaning noticeable to a user.

          1 Reply Last reply Reply Quote 0
          • Bas de ReuverB Bas de Reuver referenced this topic on
          • Bas de ReuverB
            Bas de Reuver
            last edited by Bas de Reuver

            With a custom lexer, is it possible to update the syntax highlighting colors through code?

            In my plug-in the user can select 4 pre-set colors schemes, and the plugin just overwrites the <plug-in name>.xml file with the new colors and then shows a dialog asking the user to restart Notepad++ so that the new colors are applied.

            <?xml version="1.0" encoding="utf-8"?>
            <NotepadPlus>
            	<Languages><!-- etc.. --></Languages>
            	<LexerStyles>
            		<LexerType name="CSVLint" desc="CSV Linter and validator" excluded="no">
            			<WordsStyle styleID="0" name="Default" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />
            			<WordsStyle styleID="1" name="ColumnColor1" fgColor="000000" bgColor="E0E0FF" fontName="" fontStyle="0" />
            			<WordsStyle styleID="2" name="ColumnColor2" fgColor="000000" bgColor="FFFF80" fontName="" fontStyle="0" />
            			<WordsStyle styleID="3" name="ColumnColor3" fgColor="000000" bgColor="FFE0FF" fontName="" fontStyle="0" />
            			<!-- etc.. -->
            

            But is it possible to update the syntax highlighting colors immediately without restarting Notepad++? So update the color settings through code or a API call?

            I know something might be possible, because when you go to Settings -> Style Configurator.. and change the syntax highlighting colors, Notepad++ will immediately update them visibly. Meaning that, if you have a file opened with that particular syntax highlighting then you will see the new colors being applied right away.

            I’ve looked at the documentation but couldn’t find anything for this.

            1 Reply Last reply Reply Quote 2
            • EkopalypseE
              Ekopalypse
              last edited by Ekopalypse

              @bas-de-reuver

              What you are looking for are the style definitions.
              The styleID from the xml is the int style value from the methods.
              But that doesn’t inform Npp (afaik, there is no official API that can be used to do this) that there are changes that need to be reaccounted for,
              and you therefore need to do this with each bufferactivated event which means it is effectively done twice, once by Npp and once by your lexer.

              Bas de ReuverB 1 Reply Last reply Reply Quote 1
              • Bas de ReuverB
                Bas de Reuver @Ekopalypse
                last edited by

                @Ekopalypse I’ve got some more time to work on the plug-in again. And I’m looking at the STYLE_LASTPREDEFINED but how do I use this, is this a notification message, or do I need to send it Scintilla using Win32.SendMessage(scintilla, .. ?

                I’m trying to dynamically (at runtime) determine how many Styles are defined for the custom Lexer. The idea is that the user can decide to add more or fewer color styles in the XML, and the plug-in Lexer will use more or fewer of those styles, depending on how many there are.

                So is it possible to get the maximum valid styleID? Or maybe a list of the Style names? So in the example below, it’s 10 styles, from Default to HigLightNumeric

                notepad_style_items.png

                EkopalypseE 2 Replies Last reply Reply Quote 1
                • EkopalypseE
                  Ekopalypse @Bas de Reuver
                  last edited by

                  @Bas-de-Reuver

                  STYLE_LASTPREDEFINED is used internally by Scintilla and is not publicly available through the API, but I’ll have to double check. Your lexer has 256 styles available, with Scintilla using IDs from 32-39 internally. Since it’s your lexer, you should be able to get the list of style IDs used via some configuration settings.
                  I’m at the end of my lunch break right now, but I’ll try to come back to this later in the day. See you then.

                  1 Reply Last reply Reply Quote 3
                  • EkopalypseE
                    Ekopalypse @Bas de Reuver
                    last edited by Ekopalypse

                    @Bas-de-Reuver

                    If I did my homework, STYLE_LASTPREDEFINED is only used internally by Scintilla and is only available in a header to inform implementing applications and lexers that this range, 32-STYLE_LASTPREDEFINED, should not be used.
                    As for the actual problem, from my point of view there is a problem. Npp reads the XML file before the user uses it, and there is currently no way to change this from Npp so that Npp would re-read this file. That means the user would have to restart Npp every time they changed it, or you provide that option via the lexer and a custom configuration menu.

                    Bas de ReuverB 1 Reply Last reply Reply Quote 0
                    • Bas de ReuverB
                      Bas de Reuver @Ekopalypse
                      last edited by Bas de Reuver

                      @Ekopalypse thanks for the answer and looking into this, really appreciate it. I am able to update the custom style of my lexer plug-in, using StyleSetBack and StyleSetFore. The style changes are immediately visible, so that is a nice Quality-Of-Life improvement I will put in the next update. 😀

                      // get handle for editor
                      var IScintillaGateway editor = new ScintillaGateway(PluginBase.GetCurrentScintilla());
                      	
                      // etc..
                      	
                      // update style immediately
                      var fgcolor = x0ff00ff; //pink
                      var fgcolor = x000ffff; //yellow, apparently it's stored b-g-r so not x0ffff00
                      	
                      editor.StyleSetBack(idx, new Colour(fgcolor));
                      editor.StyleSetFore(idx, new Colour(bgcolor));
                      editor.StyleSetBold(idx, bold);
                      

                      However, I wasn’t looking for that in my previous post. And to be clear I also wasn’t looking to reload the XML dynamically or live-update any changes made externally to the XML settings file.

                      I meant, programmatically get the number of Style items currently in the lexer (also storedin in the XML file). Or maybe get a string-list of the currently available styles. I tried the method GetNamedStyles, expecting to return the number of Styles in the list

                      // color index
                      var test123 = editor.GetNamedStyles(); // always returns 1?
                      

                      So in the lexer in the example screenshot, I expected it to return 10 but it returns 1(?)

                      EkopalypseE 2 Replies Last reply Reply Quote 0
                      • EkopalypseE
                        Ekopalypse @Bas de Reuver
                        last edited by Ekopalypse

                        @Bas-de-Reuver

                        The xml file is only there to tell Npp how the
                        properties of the various styles CSVLint uses are to be set
                        once a buffer is activated, reflecting of course what has been coded internally,
                        so GetNamedStyles returns what your lexer has specified and in fact
                        the CSVLintLexer has only one style defined.

                        You need to find a way to map your idx to the styles you
                        provided in the xml, and then code the associated functions

                                    public ILexerNamedStyles NamedStyles;
                                    public ILexerNameOfStyle NameOfStyle;
                                    public ILexerTagsOfStyle TagsOfStyle;
                                    public ILexerDescriptionOfStyle DescriptionOfStyle;
                        

                        to your liking.

                        I hope I have understood the question correctly this time. :-)

                        1 Reply Last reply Reply Quote 0
                        • EkopalypseE
                          Ekopalypse @Bas de Reuver
                          last edited by

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

                          StyleSetBack and StyleSetFore

                          btw … this is usually what Npp does when the buffer is activated or a user changes the configuration via the style configuration dialog.

                          Bas de ReuverB 1 Reply Last reply Reply Quote 0
                          • Bas de ReuverB
                            Bas de Reuver @Ekopalypse
                            last edited by Bas de Reuver

                            @Ekopalypse I can update the style using StyleSetBack and StyleSetFore and this works, the new colors are immediately visible. I also overwrite the new style to the XML styler file, so on next start-up it keeps the news colors.

                            However, I just noticed that when I don’t restart Notepad++ and switch to a different file, the colors in the other file are still in the old/original color style. And when I go back to the first file tab that’s now also back in the old color style. (Btw the colors are correctly changed when I restart Notepad++ because of also the XML update, but I don’t want the user to have to restart)

                            I suspect it has to do with getting a handle to the editor through GetCurrentScintilla function. But that doesn’t explain why all colors as changed back to the original, and not just for one tab.

                            Do I have to do anything additional in code to make the style change permanent? Seems like the lexer plugin also stored the original colors from startup (read form the XML) somewhere, but where is that?

                            EkopalypseE 1 Reply Last reply Reply Quote 0
                            • EkopalypseE
                              Ekopalypse @Bas de Reuver
                              last edited by

                              @Bas-de-Reuver

                              … to another file, the colors in the other file are still in the old/original color style. And when I go back to the first file tab, the colors are also back in the old color style.

                              … I suspect it has to do with getting a handle on the editor via the GetCurrentScintilla function. But that doesn’t explain why all the colors were reset to the original, and not just for one tab.

                              Npp only knows that styles for this lexer should be changed by reading the XML when starting the application or if modified via the style dialog during runtime. Whenever switching to another tab, Npp will determine which lexer is used for this buffer and accordingly changes the styles via StyleSet… Functions. There is no other official way, that I know of, to make this known to Npp at runtime.

                              Do I need to do something additional in the code to make the style change permanent? It seems that the Lexer plugin also has the original colors from the start (read from the XML) stored somewhere, but where is that?

                              There is of course the way to find out where Npp keeps the data in memory and then modify accordingly, but you don’t want to go that way, it is not only highly error prone but more likely to crash Npp. One possibility would be to do the same as Npp, when a BufferActivated event is triggered, set the styles as you already did.

                              Bas de ReuverB 1 Reply Last reply Reply Quote 0
                              • Bas de ReuverB
                                Bas de Reuver @Ekopalypse
                                last edited by

                                @Ekopalypse thanks for the answer. 🤔 Adding listeners for the BufferActivated event and manually setting the style each time the user switches to a different tab adds a lot of code and possibilities for bugs, for something that the user typically only does once…

                                Honestly I think I’ll undo the new code and go back to just asking the user to restart Notepad++ after they change the default color set.

                                1 Reply Last reply Reply Quote 0
                                • Michael VincentM Michael Vincent referenced this topic on
                                • First post
                                  Last post
                                The Community of users of the Notepad++ text editor.
                                Powered by NodeBB | Contributors