-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
tag_lint.py
executable file
·305 lines (263 loc) · 9.31 KB
/
tag_lint.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import sublime, sublime_plugin
from time import time, sleep
import threading
import _thread as thread
from Tag import Tag
import re
Tag = Tag.Tag()
def plugin_loaded():
global s, Pref, tag_lint, tag_lint_run
s = sublime.load_settings('Tag Package.sublime-settings')
Pref = Pref();
Pref.load()
s.clear_on_change('reload')
s.add_on_change('reload', lambda:Pref.load())
tag_lint = TagLint();
tag_lint_run = tag_lint.run
if not 'running_tag_lint_loop' in globals():
global running_tag_lint_loop
running_tag_lint_loop = True
thread.start_new_thread(tag_lint_loop, ())
class Pref:
def load(self):
Pref.view = False
Pref.modified = False
Pref.elapsed_time = 0.4
Pref.time = time()
Pref.wait_time = 0.8
Pref.running = False
Pref.enable_live_tag_linting = s.get('enable_live_tag_linting', True)
Pref.hard_highlight = ['', 'html', 'htm', 'php', 'tpl', 'md', 'txt', 'jsx', 'js']
Pref.enable_live_tag_linting_document_types = [item.lower() for item in s.get('enable_live_tag_linting_document_types', '')]
Pref.statuses = 0
Pref.message_line = -1
Pref.selection_last_line = -1
Pref.message = ''
Pref.view_size = 0
class TagLint(sublime_plugin.EventListener):
def on_activated(self, view):
if not view.settings().get('is_widget') and not view.is_scratch():
Pref.view = view
Pref.selection_last_line = -1
def on_load(self, view):
if not view.settings().get('is_widget') and not view.is_scratch():
Pref.modified = True
Pref.view = view
sublime.set_timeout(lambda:self.run(True), 0)
def on_modified(self, view):
if not view.settings().get('is_widget') and not view.is_scratch():
Pref.modified = True
Pref.time = time()
def on_selection_modified(self, view):
if Pref.enable_live_tag_linting:
sel = view.sel()
if sel and Pref.message_line != -1 and Pref.message != '':
line = view.rowcol(sel[0].end())[0]
if Pref.selection_last_line != line:
Pref.selection_last_line = line
if line == Pref.message_line:
view.set_status('TagLint', Pref.message)
else:
Pref.statuses += 1
sublime.set_timeout(lambda:self.clear_status(view, False), 7000);
#view.erase_status('TagLint')
# else:
# view.erase_status('TagLint')
def on_close(self, view):
Pref.view = False
Pref.modified = True
def guess_view(self):
if sublime.active_window() and sublime.active_window().active_view():
Pref.view = sublime.active_window().active_view()
def run(self, asap = False, from_command = False):
now = time()
if asap == False and (now - Pref.time < Pref.wait_time):
return
if (Pref.enable_live_tag_linting or from_command) and Pref.modified and not Pref.running:
Pref.modified = False
if from_command:
Pref.view = sublime.active_window().active_view()
if Pref.view:
view = Pref.view
Pref.view_size = view.size()
if Pref.view_size > 10485760:
return
if Pref.view.settings().get('is_widget') or Pref.view.is_scratch():
return
file_ext = ('name.'+(view.file_name() or '')).split('.')
file_ext.reverse()
file_ext = file_ext.pop(0).lower()
if not from_command and file_ext not in Pref.enable_live_tag_linting_document_types:
return
Pref.running = True
is_xml = Tag.view_is_xml(view)
if from_command:
if view.sel():
region = view.sel()[0]
if region.empty():
region = sublime.Region(0, view.size())
else:
region = sublime.Region(0, view.size())
else:
region = sublime.Region(0, view.size())
original_position = region.begin()
content = view.substr(region)
TagLintThread(view, content, original_position, is_xml, from_command).start()
else:
self.guess_view()
def display(self, view, message, invalid_tag_located_at, from_command):
if view is not None:
view.erase_regions("TagLint")
if invalid_tag_located_at > -1:
invalid_tag_located_at_start = invalid_tag_located_at
invalid_tag_located_at_end = invalid_tag_located_at+1
size = view.size()
while invalid_tag_located_at_end < size:
end = view.substr(sublime.Region(invalid_tag_located_at_end, invalid_tag_located_at_end+1))
if end == '>':
invalid_tag_located_at_end += 1
break
elif end == '<':
break;
invalid_tag_located_at_end += 1
if invalid_tag_located_at_start - invalid_tag_located_at_end > 100:
break
region = sublime.Region(invalid_tag_located_at_start, invalid_tag_located_at_end)
line, col = view.rowcol(region.a);
string = re.split('\s|>', view.substr(region))[0]
if len(string) > 1 and string[1] == '/':
mas = 2
else:
mas = 1
region = sublime.Region(region.a+mas, region.a+len(string))
view.add_regions("TagLint", [region], 'variable.parameter', 'dot', sublime.PERSISTENT | sublime.DRAW_SQUIGGLY_UNDERLINE | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_EMPTY_AS_OVERWRITE)
Pref.message_line = line
Pref.message = message
view.set_status('TagLint', Pref.message+' in Line '+str(Pref.message_line+1)+' ')
Pref.statuses += 1
sublime.set_timeout(lambda:self.clear_status(view, from_command), 7000);
if from_command:
view.show_at_center(region)
else:
Pref.message_line = -1
Pref.message = ''
if from_command:
view.set_status('TagLint', 'No errors found')
Pref.statuses += 1
sublime.set_timeout(lambda:self.clear_status(view, from_command), 7000);
else:
view.erase_status('TagLint')
else:
Pref.message_line = -1
Pref.message = ''
Pref.running = False
def clear_status(self, view, from_command):
Pref.statuses -= 1
if view is not None and Pref.statuses == 0:
view.erase_status('TagLint')
if from_command and Pref.enable_live_tag_linting == False:
view.erase_regions("TagLint")
class TagLintThread(threading.Thread):
def __init__(self, view, content, original_position, is_xml, from_command):
threading.Thread.__init__(self)
self.view = view
self.content = content
self.original_position = original_position
self.is_xml = is_xml
self.message = ''
self.invalid_tag_located_at = -1
self.from_command = from_command
def run(self):
begin = time()
content = self.content
original_position = self.original_position
is_xml = self.is_xml
# remove unparseable content
content = Tag.clean_html(content)
# linting: opening tags
data = content.split('<')
position = original_position+len(data.pop(0))
invalid_tag_located_at = -1
i = 0
lenght = len(data)
first_at = 0
while i < lenght:
tag = Tag.name(data[i], False, is_xml)
if tag and tag != 'html' and tag != 'body' and tag != 'head':
# if opening tag, then check if closing tag exists
if not Tag.is_closing(data[i]):
# print tag+' is opening '
if first_at == 0:
first_at = position
a = i+1
skip = 0
while a < lenght:
inner_tag_name = Tag.name(data[a], False, is_xml)
# check if same tag was found
if inner_tag_name and inner_tag_name == tag:
# check if tag is closing
if Tag.is_closing(data[a]):
if skip == 0:
break
else:
skip = skip-1
else:
skip = skip+1
a = a+1
if a >= lenght:
self.message = '"'+tag+'" tag is not closing'
invalid_tag_located_at = position
break
position += len(data[i])+1
i = i+1
# linting: closing tags
if invalid_tag_located_at == -1:
position = original_position+len(content);
data = content.split('<')
data.reverse()
i = 0
lenght = len(data)-1
while i < lenght:
tag = Tag.name(data[i], False, is_xml)
if tag and tag != 'html' and tag != 'body' and tag != 'head':
# if closing tag, check if opening tag exists
if Tag.is_closing(data[i]):
# print tag+' is closing '
a = i+1
skip = 0
while a < lenght:
inner_tag_name = Tag.name(data[a], False, is_xml)
if inner_tag_name and inner_tag_name == tag:
# check if tag is opening
if not Tag.is_closing(data[a]):
if skip == 0:
break
else:
skip = skip-1
else:
skip = skip+1
a = a+1
if a >= lenght:
self.message = '"'+tag+'" tag is not opening'
invalid_tag_located_at = position-(len(data[i])+1)
if invalid_tag_located_at < first_at:
invalid_tag_located_at = -1
break
position -= len(data[i])+1
i = i+1
elapsed_time = time() - begin;
# print 'Benchmark: '+str(elapsed_time)
self.invalid_tag_located_at = invalid_tag_located_at
sublime.set_timeout(lambda:tag_lint.display(self.view, self.message, self.invalid_tag_located_at, self.from_command), 0)
def tag_lint_loop():
while True:
# sleep time is adaptive, if takes more than 0.4 to calculate the word count
# sleep_time becomes elapsed_time*3
if Pref.running == False:
sublime.set_timeout(lambda:tag_lint_run(), 0)
sleep((Pref.elapsed_time*3 if Pref.elapsed_time > 0.4 else 0.4))
class TagLintCommand(sublime_plugin.WindowCommand):
def run(self):
Pref.modified = True
Pref.running = False
tag_lint_run(True, True);