VBA formatting with line after SUB's
-
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?
-
@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)
- FIND =
-
@peterjones thanks for the quick reply!
What that seemed to do was remove blank lines not insert a physical line.
-
@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
-
@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)
-
@peterjones got it!
yes, your last post answers the question. with that said, do you know of any plug in?
-
@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).
-
@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).
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:
I know that Notepad++ can get the horizontal line drawn somehow – if I collapse Sub B in that example, you can see it:
… 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.
-
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.
-
@alan-kilborn hi all,
thanks for looking into this. I am keeping up with the thread.
-
@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:It puts the lines before the
Sub XXX
, rather than after theEnd Sub
… but maybe that’s close enough for @DevSrc8. If so:- Install PythonScript plugin (Plugins > Plugins Admin, then toggle the
☑ Python Script
to on; click Install) - Look for Plugins > Python Script > Scripts >
startup (User)
- if all you see is
startup
without astartup (User)
, then Plugins > Python Script > New Script, call itstartup.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 onstartup (User)
- if all you see is
- Edit the user
startup.py
to includeeditor.setFoldFlags(FOLDFLAG.LINEBEFORE_EXPANDED|FOLDFLAG.LINEAFTER_CONTRACTED)
If it was empty, you will want it to be something like
and save the filefrom Npp import * import sys editor.setFoldFlags(FOLDFLAG.LINEBEFORE_EXPANDED|FOLDFLAG.LINEAFTER_CONTRACTED)
- Plugins > PythonScript > Configuration…, change Intialisation:
LAZY
to Intialisation:ATSTARTUP
- 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 - Install PythonScript plugin (Plugins > Plugins Admin, then toggle the
-
@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. :-)
-
@alan-kilborn thanks everyone for assisting!
I was able to get lines via the recommended method, however they look like this (code snippet below).
-
@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. -
@peterjones very fair points. I will use your alt0 recommendation. thank you for the quick and helpful feedback.