-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtag_close_tag.py
executable file
·87 lines (73 loc) · 2.26 KB
/
tag_close_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import sublime, sublime_plugin
from Tag import Tag
Tag = Tag.Tag()
class TagCloseTagCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
is_xml = Tag.view_is_xml(view);
closed_some_tag = False
new_selections = []
new_selections_insert = []
for region in view.sel():
cursorPosition = region.begin()
tag = self.close_tag(view.substr(sublime.Region(0, cursorPosition)), is_xml)
if tag and tag != '</':
if region.empty():
replace = False
view.insert(edit, cursorPosition, tag);
else:
replace = True
view.replace(edit, sublime.Region(region.begin(), region.end()), '');
view.insert(edit, cursorPosition, tag);
if tag != '</':
closed_some_tag = True
if replace:
new_selections_insert.append(sublime.Region(region.begin()+len(tag), region.begin()+len(tag)))
else:
new_selections_insert.append(sublime.Region(region.end()+len(tag), region.end()+len(tag)))
else:
new_selections.append(sublime.Region(region.end()+len(tag), region.end()+len(tag)))
else:
new_selections.append(sublime.Region(region.end(), region.end()))
view.sel().clear()
# we inserted the "</tagname" part.
# running the command "insert" with parameter ">" to allow
# to the application indent these tags correctly
if closed_some_tag:
view.run_command('hide_auto_complete')
for sel in new_selections_insert:
view.sel().add(sel)
view.run_command('insert', {"characters": ">"})
view.run_command('reindent', {"force_indent": True})
for sel in new_selections:
view.sel().add(sel)
def close_tag(self, data, is_xml):
data = Tag.clean_html(data).split('<')
data.reverse()
try:
i = 0
lenght = len(data)-1
while i < lenght:
tag = Tag.name(data[i], True, is_xml)
# if opening tag, close the tag
if tag:
if not Tag.is_closing(data[i]):
return '</'+Tag.name(data[i], True, is_xml)+''
# if closing tag, jump to opening tag
else:
i = i+1
skip = 0
while i < lenght:
if Tag.name(data[i], True, is_xml) == tag:
if not Tag.is_closing(data[i]):
if skip == 0:
break
else:
skip = skip-1
else:
skip = skip+1
i = i+1
i = i+1
return ''
except:
return '';