@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
@PeterJones
I think I got it working by calling 2 callbacks (CHARADDED and UPDATEUI) instead of just CHARADDED. 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. Pressing Enter 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()