-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_indent_tag.py
58 lines (44 loc) · 1.74 KB
/
auto_indent_tag.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
56
57
58
import sublime_plugin
class AutoIndentTagCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
cursor = view.sel()[0].a
cur_line = view.line(cursor)
line_start = cur_line.a
end = cursor - line_start
preceeding = view.substr(cur_line)
prev_tag = None
next_tag = None
while True:
pos = preceeding.rfind('<', 0, end)
# Not on the current line
if pos == -1:
# Abort at the beginning of the file
if line_start == 0:
break
prev_line = view.full_line(line_start - 1)
line_start = prev_line.a
preceeding = view.substr(prev_line)
end = len(preceeding)
continue
point = line_start + pos
if view.score_selector(point, 'punctuation.definition.tag.begin') > 0:
# Skip the "<"
point = self.skip_whitespace(point + 1)
# If the preceeding tag is a close tag, no auto indent
if view.substr(point) == '/':
break
prev_tag = view.substr(view.extract_scope(point))
break
end = pos
# Skip the "</"
point = self.skip_whitespace(cursor + 2)
next_tag = view.substr(view.extract_scope(point))
if not prev_tag or not next_tag or prev_tag != next_tag:
view.run_command('insert', {'characters': '\n'})
return
view.run_command('insert_snippet', {'contents': "\n\t$0\n"})
def skip_whitespace(self, point):
while self.view.substr(point) in {' ', '\t', '\n'}:
point += 1
return point