-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout-of-brackets.py
55 lines (48 loc) · 1.65 KB
/
out-of-brackets.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sublime, sublime_plugin
class OutOfBracketsCommand(sublime_plugin.TextCommand):
def run(self, edit):
# remove current line if its empty
# dont remove it if next line contains code other than brackets
# (i.e. this line is delimiter between code blocks)
for sel in self.view.sel():
if sel.empty():
pos = sel.b
line = self.view.substr(self.view.line(pos))
line = line.replace(' ', '').replace('\t', '')
if len(line) <= 0:
next_line_pos = self.view.line(pos).b + 1
next_line = self.view.substr(self.view.line(next_line_pos))
next_line = next_line.replace(' ', '').replace('\t', '')
if len(next_line) <= 0 or next_line[0] == "}":
self.view.erase(edit, self.view.line(pos))
self.view.run_command("left_delete")
# move out of brackets
for sel in self.view.sel():
if sel.empty():
self.view.run_command("move_to", {
"to": "brackets"
})
self.view.run_command("move_to", {
#"extend": false,
"to": "eol"
})
# insert ; when required
for sel in self.view.sel():
if sel.empty():
pos = sel.b
prev_char = self.view.substr(pos - 1)
if prev_char != ";" and prev_char != "}":
self.view.insert(edit, pos, ";\n")
else:
self.view.insert(edit, pos, "\n")
self.view.run_command("reindent")
# remove next line if empty
for sel in self.view.sel():
if sel.empty():
pos = sel.b
line = self.view.line(pos)
next_line_pos = line.b + 1
next_line = self.view.substr(self.view.line(next_line_pos))
next_line = next_line.replace(' ', '').replace('\t', '')
if len(next_line) <= 0:
self.view.erase(edit, self.view.full_line(next_line_pos))