-
Notifications
You must be signed in to change notification settings - Fork 11
/
swap_assignment.py
38 lines (35 loc) · 1.37 KB
/
swap_assignment.py
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
#
# Swap the assignment fields of the selected text - works across multiple lines,
# swapping assignments on each selected line.
#
text = ""
if not komodo.editor.selText:
# No selection - abort.
return
newlines = []
for line in komodo.editor.selText.splitlines(1):
idx = line.find("=")
if idx < 0:
# Allow argument (comma separated) swapping too.
idx = line.find(",")
if idx > 0:
# The complicated swap code, find all the different positions.
leftCode = line[:idx].rstrip()
rightCode = line[idx+1:].lstrip()
beginSpaces = leftCode[:len(leftCode) - len(leftCode.lstrip())]
endSpaces = rightCode[len(rightCode) - (len(rightCode) - len(rightCode.rstrip())):]
middle = line[len(leftCode):idx + ((len(line) - idx) - len(rightCode))]
leftCode = leftCode.lstrip()
rightCode = rightCode.rstrip()
endField = ""
# Special case for C or Javascript code (a trailing semi-colan).
if rightCode[-1] in (";"):
endField = rightCode[-1]
rightCode = rightCode[:-1]
# And now we rotate it.
newlines.append(beginSpaces + rightCode + middle + leftCode + endField + endSpaces)
else:
# Didn't find anything - so don't change it.
newlines.append(line)
# Update the selection with the swapped code.
komodo.editor.replaceSel("".join(newlines))