I would like to do my own PowerShell Indenting
-
Since about a year ago in Notepad++, the program has been automatically shifting my indents back in my Powershell code. I typically write code like this:
If (blablabla)
{
perform blablablaBut now, the program automatically shifts the opening curly brace back to make it look like this:
if (blablabla)
{
perform blablablaI would like to be albe to stop it from pushing my { back one indent so that I can have my code look the way that I like it. Is that posible?
Thanks so much,
-
When you previewed your post in the PREVIEW window, did you not notice that your two example texts rendered exactly the same?
There is a “Read Before Posting” post right underneath the “New Topic” button that you pressed, that linked to a description of how to format your example text in this Forum so that we will be able to see what your data really looks like. Since you didn’t follow that advice, your question’s example data doesn’t match with your description, so it’s much harder for us to guess what your data really looks like, and thus much harder for us to help you.
When I View Source on your message, it appears that maybe you had written something like
If (blablabla) { perform blablabla
but that Notepad++ is making it come out like:
if (blablabla) { perform blablabla
I tried it out: if I try to tab/indent then type the
{
, it does outdent a level. I would have thought it was a feature of the PowerShell lexer, which Notepad++ inherits from the Scintilla project. But when I use SciTe 4.4.6 (which matches the version of Scintilla that Notepad++ uses), it doesn’t do that on PowerShell files. Weird.I cannot think of a setting in Notepad++ that allows you to control whether or not the brace-indentation fixes itself, separately from other features: you can turn off Settings > Preferences > Auto-Completion > Auto-indent , which allows you to manually indent the brace as far as you want… but it also means that the ENTER will take you to the beginning of the next line, rather than lined up under the {, or indented one more level beyond the {, so that will probably annoy you even more than the auto-fixed braces do.
I wonder if the EditorConfig plugin can influence brace location… @Alan-Kilborn , you know more about EditorConfig than I do: does it have a brace-indentation-style option?
If it doesn’t, and if no one else here can think of another way to work around the problem, then you might have to ask for a feature request. Make sure you phrase it in a way that the developers will understand that just turning off auto-indent is not sufficient; it would be nice if there were a way to choose between “brace indentation matches outer level” (which is the current behavior) or “brace indentation matches inner level” (which is what you describe); I am a matches-outer-level guy myself, which is why I’ve never noticed this before. Also, I am not an expert on other people’s indentation styles; are there any other brace styles? (Well, I know that there is the brace-on-same-line-as-conditional style, which I sometimes use, but that still has the final-brace level, which has the same two rule styles already mentioned.)
-
@peterjones said in I would like to do my own PowerShell Indenting:
I wonder if the EditorConfig plugin can influence brace location… @Alan-Kilborn , you know more about EditorConfig than I do: does it have a brace-indentation-style option?
No, not currently.
Here’s some docs on what it does support, for ref:
https://editorconfig.org/#:~:text=Supported Properties -
@peterjones said in I would like to do my own PowerShell Indenting:
I cannot think of a setting in Notepad++ that allows you to control whether or not the brace-indentation fixes itself, separately from other features
PowerShell auto-indent didn’t work at all and was fixed back in https://github.com/notepad-plus-plus/notepad-plus-plus/issues/9049
It acts like other languages that require braces for block control. There is no setting to turn on/off. Maybe a feature request that implements a brace behavior per language? That may be way more complicated, but the way it works now is “best” for most people as this is the only complaint against the default behavior I’ve seen.
This could also be addressed with a language styler or style guide implementing a brace indent behavior (https://en.wikipedia.org/wiki/Indentation_style). For example, I prefer Allman for C/C++ but K&R for Perl - where I also prefer cuddled-else. I use both AStyle and PerlTidy respectively to enforce those preferences by running those external programs (via NppExec or Pythonscript) when I save files.
Cheers.
-
@michael-vincent said in I would like to do my own PowerShell Indenting:
…the way it works now is “best” for most people as this is the only complaint against the default behavior I’ve seen.
I’ll add my name.
I usually use Notepad++ for languages that have no/limited use of curly braces (e.g. SQL, Python) and have recently started writing PowerShell, which is replete with them. The indent on curly braces has been nagging at me - especially because it doesn’t happen for other delimiters.
It’d be nice to be able to disable this behavior - but not nice enough for me to actually do anything about it, nor expect that someone else would on my behalf. Just didn’t want the OP to feel alone :)
-
@michael-vincent said in I would like to do my own PowerShell Indenting:
This could also be addressed with a language styler or style guide implementing a brace indent behavior
@mmannion
@Steve-Almes
@Alan-Kilborn
@PeterJonesI know this can be done after-the-fact with a lint or style program (e.g., AStyle), but I thought maybe it could be done real-time with a PythonScript. It seems the PythonScript callbacks fire BEFORE the Notepad++ “auto-styling-indent” so it doesn’t work. If the PythonScript callbacks fired AFTER the Notepad++ “auto-styling-indent”, the following would work:
from Npp import ( editor, notepad, LANGTYPE, NOTIFICATION, SCINTILLANOTIFICATION ) class GnuIndent(object): def __init__(self): self.DEBUG = False self.ENABLED = False self.my_language = [LANGTYPE.POWERSHELL] self.is_my_language = False self.nppCallbacks = { self.on_buffer_activated: [ NOTIFICATION.BUFFERACTIVATED, NOTIFICATION.LANGCHANGED ] } self.edCallbacks = { self.on_charadded: [ SCINTILLANOTIFICATION.CHARADDED ] } def start(self): for cb in self.nppCallbacks: notepad.callback(cb, self.nppCallbacks[cb]) for cb in self.edCallbacks: editor.callbackSync(cb, self.edCallbacks[cb]) self.ENABLED = True def status(self): for e in sorted(self.__dict__): print("{0:<25} : {1}".format(e, self.__dict__[e])) def stop(self): for cb in self.nppCallbacks: notepad.clearCallbacks(cb) for cb in self.edCallbacks: editor.clearCallbacks(cb) self.ENABLED = False def on_buffer_activated(self, args): lang = notepad.getCurrentLang() if lang in self.my_language: self.is_my_language = True else: self.is_my_language = False def on_charadded(self, args): if not self.is_my_language: return c = chr(args['ch']) if c == '{': tabwidth = editor.getTabWidth() pos = editor.getCurrentPos() currline = editor.lineFromPosition(pos) indent = editor.getLineIndentation(currline - 1) if self.DEBUG: print(pos,currline,indent,tabwidth) editor.setLineIndentation(currline, indent + tabwidth) if __name__ == '__main__': gnuIndent = GnuIndent() gnuIndent.start()
On language changes or edit buffer switches, we check to see if we have a desired language and if so, set the
is_my_language
flag. Then, on every character insert, ifis_my_language
, check if it’s a{
and if so, grab the previous line indent, and add that+
the default tab indent to make (for example):if (stuff) { | }
Where the
|
above is the cursor.Feel free to experiment with this and see if you can get it to work AFTER Notepad++ does it’s auto-indenting-of
{
.Cheers.
-
@michael-vincent said in I would like to do my own PowerShell Indenting:
Feel free to experiment with this and see if you can get it to work AFTER Notepad++ does it’s auto-indenting-of {.
@mmannion
@Steve-Almes
@Alan-Kilborn
@PeterJonesI think I got it working by calling 2 callbacks (
CHARADDED
andUPDATEUI
) instead of justCHARADDED
. The following works for my when I type:if (test) [ENTER]
I get:
if (test) |
Where
|
is cursor. Then I type the{
and get this:if (test) {|}
Where
|
is cursor. The{
auto-inserts the closing}
and both are auto-indented 4-spaces as requested. PressingEnter
now yeilds:if (test) { | }
Obviously you can adjust the
_set_indent()
function to perform how you want.from Npp import editor, notepad, LANGTYPE, NOTIFICATION, SCINTILLANOTIFICATION class GnuIndent(object): def __init__(self): self.DEBUG = False self.ENABLED = False self.my_language = [LANGTYPE.POWERSHELL] self.is_my_language = False self._do_indent = False self.nppCallbacks = { self._on_buffer_activated: [NOTIFICATION.BUFFERACTIVATED, NOTIFICATION.LANGCHANGED] } self.edCallbacks = { self._on_charadded: [SCINTILLANOTIFICATION.CHARADDED], self._on_updateui: [SCINTILLANOTIFICATION.UPDATEUI] } def start(self): """Start the service""" for cb in self.nppCallbacks: notepad.callback(cb, self.nppCallbacks[cb]) for cb in self.edCallbacks: editor.callbackSync(cb, self.edCallbacks[cb]) self.ENABLED = True def status(self): """Status the service""" for e in sorted(self.__dict__): print("{0:<25} : {1}".format(e, self.__dict__[e])) def stop(self): """Stop the service""" for cb in self.nppCallbacks: notepad.clearCallbacks(cb) for cb in self.edCallbacks: editor.clearCallbacks(cb) self.ENABLED = False def _on_buffer_activated(self, args): lang = notepad.getCurrentLang() if lang in self.my_language: self.is_my_language = True else: self.is_my_language = False def _on_charadded(self, args): if not self.is_my_language: return if chr(args['ch']) == '{': self._do_indent = True def _on_updateui(self, args): if not self.is_my_language: return if args['updated'] & 0x01 and self._do_indent: self._do_indent = False self._set_indent() def _set_indent(self): tabwidth = editor.getTabWidth() pos = editor.getCurrentPos() currline = editor.lineFromPosition(pos) indent = editor.getLineIndentation(currline - 1) if self.DEBUG: print(pos, currline, indent, tabwidth) editor.setLineIndentation(currline, indent + tabwidth) if __name__ == '__main__': gnuIndent = GnuIndent() gnuIndent.start()