-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeLines_in_coding.py
More file actions
65 lines (51 loc) · 2.01 KB
/
Copy pathMergeLines_in_coding.py
File metadata and controls
65 lines (51 loc) · 2.01 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
60
61
62
63
64
65
import re
# Wim Gielis
# Aug. 2026
#
# MergeLines_in_coding script (Alt-Shift-m):
# - The selected lines are merged with a space, except line 1 + 2, and last but one line + last line
# - Typical use case is in coding, to merge multiple lines into 1 longer line
#
# Rules:
# 1. First line + second line: no separator
# 2. Middle lines: one space between them
# 3. Last-but-one + last line: no separator
# 4. Remove a trailing comma before a closing ), ] or }
# 5. Works with LF, CRLF, or CR line endings
# 6. The selection is replaced in-place
# 7. The whole operation is one Ctrl+Z undo step
selected = editor.getSelText()
if selected:
# Split on any of the three possible line-ending styles.
lines = re.split(r'\r\n|\r|\n', selected)
# Remove indentation and whitespace around each line.
lines = [line.strip() for line in lines]
# Remove empty lines at the beginning/end of the selection.
while lines and not lines[0]:
lines.pop(0)
while lines and not lines[-1]:
lines.pop()
if lines:
# If the final line is a closing bracket,
# remove the trailing comma from the preceding line.
if len(lines) >= 2 and lines[-1] in (')', ']', '}'):
lines[-2] = re.sub(r',\s*$', '', lines[-2])
if len(lines) == 1:
result = lines[0]
elif len(lines) == 2:
# First/second and last-but-one/last are the same join.
result = lines[0] + lines[1]
else:
# First + second: no space
result = lines[0] + lines[1]
# Middle joins: one space
for i in range(1, len(lines) - 2):
result += ' ' + lines[i + 1]
# Last-but-one + last: no space
result += lines[-1]
# One undo operation for the entire replacement.
editor.beginUndoAction()
try:
editor.replaceSel(result)
finally:
editor.endUndoAction()