-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveWhitespace.py
More file actions
50 lines (38 loc) · 1.37 KB
/
Copy pathRemoveWhitespace.py
File metadata and controls
50 lines (38 loc) · 1.37 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
from Npp import *
import re
# Wim Gielis
# Aug. 2026
#
# RemoveWhitespace script (Ctrl-<):
# - From the end of the line, put a space, remove all whitespace until the following non-whitespace encountered
# - This could span several linse possibly.
# - This is useful for cleaning up code and texts.
#
editor.beginUndoAction()
try:
# Current line
line = editor.lineFromPosition(editor.getCurrentPos())
# End of the current line, before its line ending
start = editor.getLineEndPosition(line)
# Get everything from here to the end of the document
remaining = editor.getTextRange(start, editor.getLength())
# Find the first non-whitespace character
match = re.search(r'\s*(\S)', remaining)
if match:
# Replace ALL whitespace between the current line
# and the first non-whitespace character with ONE space.
#
# Example:
# "\r\n \r\n W"
# becomes:
# " W"
target_start = start
target_end = start + match.end()
editor.setTargetRange(target_start, target_end)
editor.replaceTarget(" " + match.group(1))
else:
# Nothing but whitespace remains.
# Just put one space at the end of the current line.
editor.insertText(start, " ")
finally:
editor.endUndoAction()