Community
    • Login

    VBA formatting with line after SUB's

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    15 Posts 3 Posters 5.2k 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.
    • DevSrc8D
      DevSrc8
      last edited by

      Is there a way when language is set to VBA to have SUB’s separated by a line like it is in VBA editor in excel?

      9b62bee6-a569-4333-b77c-58c516fe0018-image.png

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @DevSrc8
        last edited by

        @devsrc8 ,

        There isn’t any feature built into Notepad++ which would automatically enforce that.

        There’s a simple search/replace which will do it:

        • FIND = (\R)+Sub
        • REPLACE = $1$1Sub
        • SEARCH MODE = regular expression
        • Replace All (or do many replace one-at-a-time)

        You can even record a macro that will do that, and assign a keystroke to that macro; so you could then run that macro just before you save (or include the SAVE action in the macro, and then re-map Ctrl+S to be your macro, and then Ctrl+S would be “separate subs then save” – I do something similar, but run a macro which does Trim Trailing then Save whenever I Ctrl+S, so I never have stray spaces at the end of my line)

        DevSrc8D 1 Reply Last reply Reply Quote 1
        • DevSrc8D
          DevSrc8 @PeterJones
          last edited by

          @peterjones thanks for the quick reply!

          What that seemed to do was remove blank lines not insert a physical line.

          PeterJonesP 2 Replies Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @DevSrc8
            last edited by

            @devsrc8 ,

            What that does is take any number of newline sequences before Sub and convert it to exactly two newlines (one blank line between)

            sub first
            end sub
            
            sub oneLineBefore
            end sub
            
            
            sub twoLinesBefore
            endsub
            sub noLinesBefore
            endsub
            

            becomes

            sub first
            end sub
            
            Sub oneLineBefore
            end sub
            
            Sub twoLinesBefore
            endsub
            
            Sub noLinesBefore
            endsub
            
            1 Reply Last reply Reply Quote 0
            • PeterJonesP
              PeterJones @DevSrc8
              last edited by

              @devsrc8 ,

              Oh, I just realized, I interpreted “separated by a line” as “separated by an empty row of text”. Now that I reread your posts, I now think what you want is for Notepad++ to display the horizontal bar at the end of each sub.

              Yeah, Notepad++ doesn’t do that natively. That would require a plugin (or script using a scripting plugin)

              DevSrc8D Alan KilbornA 2 Replies Last reply Reply Quote 1
              • DevSrc8D
                DevSrc8 @PeterJones
                last edited by

                @peterjones got it!

                yes, your last post answers the question. with that said, do you know of any plug in?

                1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @PeterJones
                  last edited by

                  @peterjones said in VBA formatting with line after SUB's:

                  That would require a plugin (or script using a scripting plugin)

                  Do you have some ideas on how the scripting plugin would “draw the line”?

                  I mean, is there some ability in Scintilla currently to command this visual effect?

                  I’d shudder at the thought of some code trying to paint this line on independently of Scintilla support.

                  Scintilla (but a newer one that N++ currently uses) has support for such a line as part of a hidden-lines feature (the idea being that such a line is shown at the vertical point of lines-currently-hidden).

                  PeterJonesP 1 Reply Last reply Reply Quote 0
                  • PeterJonesP
                    PeterJones @Alan Kilborn
                    last edited by

                    @DevSrc8 ,

                    with that said, do you know of any plug in?

                    Nope. It would probably have to be written – or maybe have that feature added to an existing plugin that helps with VB/VBA development (if such a plugin existed, you could ask the author of that plugin if they would implement the additional feature).

                    @alan-kilborn,

                    Do you have some ideas on how the scripting plugin would “draw the line”?

                    Not fully.

                    My original thought was using indicators, with one of the underlining INDICSTYLE values. I converted EnhanceAnyLexer.py to use that INDICSTYLE.DIAGONAL instead, and set it up…

                    vb_regexes = _dict()
                    vb_regexes[(0, (255,0,0))] = (r'(?i)^\h*End Sub.*$', 0)
                    vb_excluded_styles = []
                    

                    I was able to get an indication of the end of the sub, but not the whole thing, because indicators only extend to the end of the matched text, not the full width:
                    519712ad-6013-4f6c-96bd-01f4f095fc0b-image.png

                    I know that Notepad++ can get the horizontal line drawn somehow – if I collapse Sub B in that example, you can see it:
                    ef1206e2-d112-4078-b3de-ce7b3d1db3d7-image.png
                    … but I don’t know whether that’s an inherent part of the Scintilla code folding, or whether Notepad++ added that flourish after-the-fact. Someone who was willing to dig into the NPP and SCI source codes could probably find it.

                    Then again, maybe it would just be sufficient for the OP to just use View > Fold All or View > Collapse Level > 1: the horizonal lines would be drawn on each sub, and give the visual separation; and then unfolding the sub of interest would allow one to edit that function; and turning on View > Function List panel would help with navigation to the desired subroutine.

                    Alan KilbornA 1 Reply Last reply Reply Quote 0
                    • Alan KilbornA
                      Alan Kilborn @PeterJones
                      last edited by Alan Kilborn

                      @peterjones

                      Yea, IMO this is eye-candy for someone stuck on the way the VBA editors show this. But, people want what they want, and if it is easily achievable why not give it to them? But I think in this case maybe it is not easy, so probably forget-about-it.

                      DevSrc8D 1 Reply Last reply Reply Quote 0
                      • DevSrc8D
                        DevSrc8 @Alan Kilborn
                        last edited by

                        @alan-kilborn hi all,

                        thanks for looking into this. I am keeping up with the thread.

                        PeterJonesP 1 Reply Last reply Reply Quote 0
                        • PeterJonesP
                          PeterJones @DevSrc8
                          last edited by PeterJones

                          @devsrc8 ,

                          With the PythonScript plugin, you can send certain messages to the editor (Scintilla).

                          It looks like, by default, Notepad++ sends SCI_SETFOLDFLAGS with a setting of SC_FOLDFLAG_LINEAFTER_CONTRACTED, which is what draws the horizontal rule inside the folded Sub: the missing line 6 in my second image above, repeated here:

                          But with PythonScript, we can change that behavior. editor.setFoldFlags(FOLDFLAG.LINEBEFORE_EXPANDED|FOLDFLAG.LINEAFTER_CONTRACTED) will make it draw that same line inside the Sub when the Sub is folded, but it will draw it above the Sub when the Sub is not folded (is expanded). So that would look like: 1ef97fc0-cc43-4d7e-a444-19fa2b44094a-image.png

                          It puts the lines before the Sub XXX, rather than after the End Sub… but maybe that’s close enough for @DevSrc8. If so:

                          1. Install PythonScript plugin (Plugins > Plugins Admin, then toggle the ☑ Python Script to on; click Install)
                          2. Look for Plugins > Python Script > Scripts > startup (User)
                            • if all you see is startup without a startup (User), then Plugins > Python Script > New Script, call it startup.py, make sure it’s in the ...\plugins\Config\PythonScript\scripts folder, and create, which will bring it into editor
                            • if startup (User) exists, then Ctrl+Click on startup (User)
                          3. Edit the user startup.py to include editor.setFoldFlags(FOLDFLAG.LINEBEFORE_EXPANDED|FOLDFLAG.LINEAFTER_CONTRACTED)
                            If it was empty, you will want it to be something like
                            from Npp import *
                            import sys
                            editor.setFoldFlags(FOLDFLAG.LINEBEFORE_EXPANDED|FOLDFLAG.LINEAFTER_CONTRACTED)    
                            
                            and save the file
                          4. Plugins > PythonScript > Configuration…, change Intialisation: LAZY to Intialisation: ATSTARTUP
                          5. You can run Plugins > PythonScript > Scripts > startup (User) from the menu to get it to take effect without restarting Notepad++

                          ----
                          Note: One should probably make it a fancier script if one is going to be editing more file types than just Visual Basic, because otherwise, all the HTML and JSON files you edit will also add lines before the folding points, which might not be what you want. In that case, you would need to define a handler function, which checks if the file type is currently Visual Basic, and if so, turn on the LINEBEFORE_EXPANDED

                          Alan KilbornA 1 Reply Last reply Reply Quote 1
                          • Alan KilbornA
                            Alan Kilborn @PeterJones
                            last edited by Alan Kilborn

                            @peterjones said in VBA formatting with line after SUB's:

                            import sys

                            Save a line, as this isn’t used. :-)

                            BTW, in scripts I publish now I usually start them this way:

                            # -*- coding: utf-8 -*-
                            from __future__ import print_function
                            

                            even though the script itself may not print anything. But it is there in case some quick debugging prints are thrown in.

                            Just in case I’m called out on the future for a needless line. :-)

                            DevSrc8D 1 Reply Last reply Reply Quote 1
                            • DevSrc8D
                              DevSrc8 @Alan Kilborn
                              last edited by

                              @alan-kilborn thanks everyone for assisting!
                              I was able to get lines via the recommended method, however they look like this (code snippet below).
                              c392c900-76cb-4c92-a703-1dc9da837b21-image.png

                              PeterJonesP 1 Reply Last reply Reply Quote 0
                              • PeterJonesP
                                PeterJones @DevSrc8
                                last edited by

                                @devsrc8 said in VBA formatting with line after SUB's:

                                I was able to get lines via the recommended method, however they look like this (code snippet below).

                                Yes, that is a drawback, because the LINEBEFORE_EXPANDED (and all the other fold flags) apply to all the code folding in the current language, like IF/THEN/ELSE and FOR loops, not just the function-level folding.

                                The feature you actually want doesn’t exist. You have to then decide which you would prefer: partial workaround that adds it in the right place but also in other places where you don’t, or no workaround which uses the default settings and other folding sections will still look right.

                                Personally, I don’t find the horizontal bars by just the Subs to be super useful: in VBA editor, they are mildly nice, because VBA doesn’t have any code folding, so it’s really the only way to get good separation between multiple functions/subs in the code. But Notepad++ has code folding and function navigation built in… so if you really want good separation between subs/functions, just use Alt+0 to fold everything then unfold just the function you are working on. But that’s just my preference.

                                DevSrc8D 1 Reply Last reply Reply Quote 1
                                • DevSrc8D
                                  DevSrc8 @PeterJones
                                  last edited by

                                  @peterjones very fair points. I will use your alt0 recommendation. thank you for the quick and helpful feedback.

                                  1 Reply Last reply Reply Quote 1
                                  • First post
                                    Last post
                                  The Community of users of the Notepad++ text editor.
                                  Powered by NodeBB | Contributors