-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndentText.py
More file actions
59 lines (43 loc) · 1.75 KB
/
IndentText.py
File metadata and controls
59 lines (43 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from Npp import notepad, editor
NUMBER_OF_INDENT_SPACES = 4
# Wim Gielis
# Mar. 2025
#
# IndentText script (Alt-i):
# - The selected text is indented
# - We add 4 spaces to the beginning of every non-empty line
# - Empty lines are unaffected so as to not introduce unnecessary whitespace
# If there is no selection, do nothing
# if selected_text.strip():
# if editor.getSelectionEmpty():
# current_position = editor.getCurrentPos()
# current_line_number = editor.lineFromPosition(current_position)
# start_of_curr_line_pos = editor.positionFromLine(current_line_number)
# editor.setSel(start_of_curr_line_pos, start_of_curr_line_pos + len(editor.getLine(current_line_number)) - 1)
# Get the start and end positions of the current selection
sel_start = editor.getSelectionStart()
sel_end = editor.getSelectionEnd()
# Extend the selection to the start of the first selected line
line_start = editor.lineFromPosition(sel_start)
sel_start = editor.positionFromLine(line_start)
# Extend the selection to the end of the last selected line
line_end = editor.lineFromPosition(sel_end)
sel_end = editor.getLineEndPosition(line_end)
# Set the new selection range
editor.setSelection(sel_end, sel_start)
# Get the selected lines of text
selected_text = editor.getSelText()
# Split into lines and process
lines = selected_text.splitlines()
new_lines = []
for line in lines:
line = line.rstrip()
if line == '':
new_lines.append('')
else:
new_lines.append(f"{' ' * NUMBER_OF_INDENT_SPACES}{line}")
if new_lines:
# Join the processed lines back together
new_text = '\r\n'.join(new_lines)
# Replace the content in the editor
editor.replaceSel(new_text)