The following code supports PythonScript 3.0.23 as well as earlier versions of PythonScript 3.x.
#------------------------------------------------------------------------
# If the character specified in the current selection is a white space,
# then prompt the user to enter the alignment character (or characters),
# using this character as the initial default.
#------------------------------------------------------------------------
default_align_char = ','
from enum import Enum
class PaddingSide(Enum):
LEFT = 0
RIGHT = 1
def align_selected_text(max_align_char_count = None,
padding_side = PaddingSide.LEFT):
"""Insert padding into the lines in the selection, as needed, to align up to max_align_char_count instances of a specific character or string of characters
The default is to align all instances of the specific character.
At present, the alignment character is taken as the character at the top of
the current selection. You can uncomment some code below to change this
policy to instead take the alignment character from within the selection at
whichever end has the cursor. Either way, if that character is white space,
the user is prompted to type the character (or characters). If you really
wish to align on a white space character, you can just click OK at the
prompt.
When prompted to type the alignment character, the user may enter a
sequence of characters, e.g., "-->", in which case the alignment is on the
instances of that entire character sequence. For example, if the user
enters "-->" at the prompt, then instances of the "-" character get
aligned only if they're followed immediately by the characters "->", while
instances of, say, "-1" and "- " remain unaltered.
If there is no current selection, then aligns all lines in the editor.
If there is a current selection, then aligns only the lines that are
at least partially included in the selection, and the selection is
changed to the entire block of newly-padded lines.
Parameters
----------
max_align_char_count : positive integer, optional
The maximum number of instances to align of the specific
character. For example, set to 1 to align only the first
instance of the character on each line. The default is to
align all instances of the specific character.
"""
from Npp import editor
#----------------------------------------------------------------------------
# For the alignment character, take the character just inside the bounds of
# the selection block (at either the start or the end, as determined below).
#----------------------------------------------------------------------------
editor.targetFromSelection()
selected_text = editor.getTargetText()
# Use this code to get the align_char unconditionally from the start
# of the selection.
align_char = selected_text[0]
# Optionally use this code to get the align_char from within the selection
# at whichever end has the cursor.
# (startByte, endByte) = editor.getUserCharSelection()
# if startByte == editor.getCurrentPos():
# align_char = selected_text[0]
# else:
# align_char = selected_text[-1]
# If the character from the selection seems implausible as the
# align_char, then prompt the user for it.
if align_char.isspace():
from Npp import notepad
global default_align_char
align_char = notepad.prompt('Align character:',
'Enter Alignment Character',
default_align_char)
if align_char is not None:
default_align_char = align_char
#----------------------------------------------------------------------------
#%% Get the lines of text within the selected alignment block
#----------------------------------------------------------------------------
(startLine, endLine) = editor.getUserLineSelection()
startPos = editor.positionFromLine(startLine)
endPos = editor.getLineEndPosition(endLine)
text_lines = editor.getTextRangeFull(startPos, endPos).splitlines(True)
#----------------------------------------------------------------------------
# Remember whether there is a user-selected block, so we can restore a
# corresponding selection after aligning the text.
#----------------------------------------------------------------------------
restore_selection = editor.getSelectionStart() != editor.getSelectionEnd()
#----------------------------------------------------------------------------
# Align all instances of align_char within the lines of text
#----------------------------------------------------------------------------
if align_char is not None:
# Enable the following to save the align_char, however it was determined,
# to be the default_align_char when prompting for it next time.
# default_align_char = align_char
padding_side_offset = padding_side.value * len(align_char)
if max_align_char_count is None:
align_char_count = max(line.count(align_char) for line in text_lines)
else:
align_char_count = max_align_char_count
start = 0
for instance in range(align_char_count):
# Set the target column using the index of the align_char, ignoring
# immediately preceding space, or the length of the line
tgt_char_col = max(len(line[:line.find(align_char, start)].rstrip())
for line in text_lines)
for (idx,line) in enumerate(text_lines):
align_char_col = line.find(align_char, start)
if align_char_col >= 0:
text_lines[idx] = line[:align_char_col+padding_side_offset].rstrip().ljust(tgt_char_col) \
+ line[align_char_col+padding_side_offset:]
start = tgt_char_col + len(align_char)
editor.setTarget(startPos, endPos)
editor.replaceTarget(''.join(text_lines))
if restore_selection:
startPos = editor.positionFromLine(startLine)
endPos = editor.getLineEndPosition(endLine)
editor.setSelectionStart(startPos)
editor.setSelectionEnd(endPos)
if __name__ == '__main__':
align_selected_text()