forked from DisposaBoy/GoSublime
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gslint.py
278 lines (237 loc) · 5.85 KB
/
gslint.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
from gosubl import gs
from gosubl import gsq
from gosubl import gsshell
from gosubl import mg9
import os
import re
import sublime
import sublime_plugin
import threading
import time
DOMAIN = 'GsLint'
CL_DOMAIN = 'GsCompLint'
class FileRef(object):
def __init__(self, view):
self.view = view
self.src = ''
self.tm = 0.0
self.state = 0
self.reports = {}
class Report(object):
def __init__(self, row, col, msg):
self.row = row
self.col = col
self.msg = msg
class GsLintThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.daemon = True
self.sem = threading.Semaphore()
self.s = set()
self.q = gs.queue.Queue()
def putq(self, fn):
with self.sem:
if fn in self.s:
return False
self.s.add(fn)
self.q.put(fn)
return True
def popq(self):
fn = self.q.get()
with self.sem:
self.s.discard(fn)
return fn
def run(self):
while True:
fn = self.popq()
fr = ref(fn, False)
if fr:
reports = {}
res, _ = mg9.bcall('lint', {
'dir': (os.path.dirname(fn) if fn else ''),
'fn': fn,
'src': fr.src,
'filter': gs.setting('lint_filter', []),
})
res = gs.dval(res, {})
for r in gs.dval(res.get('reports'), []):
if fn and fn != '<stdin>' and r.get('Fn') != fn:
continue
kind = r.get('Kind', '')
row = r.get('Row', 0)
col = r.get('Col', 0)
msg = r.get('Message', '')
msg = '%s: %s' % (kind, msg)
if row >= 0 and msg:
reports[row] = Report(row, col, msg)
fr = ref(fn, False)
if fr:
with sem:
fr.state = 1
fr.reports = reports
file_refs[fn] = fr
def highlight(fr):
sel = gs.sel(fr.view).begin()
row, _ = fr.view.rowcol(sel)
if fr.state == 1:
fr.state = 0
cleanup(fr.view)
regions = []
regions0 = []
domain0 = DOMAIN+'-zero'
for r in fr.reports.values():
line = fr.view.line(fr.view.text_point(r.row, 0))
pos = line.begin() + r.col
if pos >= line.end():
pos = line.end()
if pos == line.begin():
regions0.append(sublime.Region(pos, pos))
else:
regions.append(sublime.Region(pos, pos))
if regions:
fr.view.add_regions(DOMAIN, regions, 'comment', 'dot', sublime.DRAW_EMPTY_AS_OVERWRITE)
else:
fr.view.erase_regions(DOMAIN)
if regions0:
fr.view.add_regions(domain0, regions0, 'comment', 'dot', sublime.HIDDEN)
else:
fr.view.erase_regions(domain0)
msg = ''
reps = fr.reports.copy()
l = len(reps)
if l > 0:
msg = '%s (%d)' % (DOMAIN, l)
r = reps.get(row)
if r and r.msg:
msg = '%s: %s' % (msg, r.msg)
if fr.state != 0:
msg = u'\u231B %s' % msg
fr.view.set_status(DOMAIN, msg)
def cleanup(view):
view.set_status(DOMAIN, '')
view.erase_regions(DOMAIN)
view.erase_regions(DOMAIN+'-zero')
def watch():
global file_refs
global th
view = gs.active_valid_go_view()
if view is not None and (view.file_name() and gs.setting('comp_lint_enabled') is True):
fn = view.file_name()
fr = ref(fn)
with sem:
if fr:
fr.view = view
highlight(fr)
sublime.set_timeout(watch, 500)
return
if gs.setting('gslint_enabled') is not True:
if view:
with sem:
for fn in file_refs:
fr = file_refs[fn]
cleanup(fr.view)
file_refs = {}
sublime.set_timeout(watch, 2000)
return
if view and not view.is_loading():
fn = view.file_name()
fr = ref(fn)
with sem:
if fr:
# always use the active view (e.g in split-panes)
fr.view = view
highlight(fr)
else:
fr = FileRef(view)
file_refs[fn] = fr
if fr.state == 0:
src = view.substr(sublime.Region(0, view.size()))
if src != fr.src:
fr.src = src
fr.tm = time.time()
if fr.tm > 0.0:
timeout = int(gs.setting('gslint_timeout', 500))
delta = int((time.time() - fr.tm) * 1000.0)
if delta >= timeout:
fr.tm = 0.0
fr.state = -1
if not th:
th = GsLintThread()
th.start()
th.putq(fn)
sublime.set_timeout(watch, 500)
def ref(fn, validate=True):
with sem:
if validate:
for fn in list(file_refs.keys()):
fr = file_refs[fn]
if not fr.view.window() or fn != fr.view.file_name():
del file_refs[fn]
return file_refs.get(fn)
def delref(fn):
with sem:
if fn in file_refs:
del file_refs[fn]
def do_comp_lint(dirname, fn):
fr = ref(fn, False)
reports = {}
if not fr:
return
fn = gs.apath(fn, dirname)
bindir, _ = gs.temp_dir('bin')
local_env = {
'GOBIN': bindir,
}
pat = r'%s:(\d+)(?:[:](\d+))?\W+(.+)\s*' % re.escape(os.path.basename(fn))
pat = re.compile(pat, re.IGNORECASE)
for c in gs.setting('comp_lint_commands'):
try:
cmd = c.get('cmd')
if not cmd:
continue
cmd_domain = ' '.join(cmd)
shell = c.get('shell') is True
env = {} if c.get('global') is True else local_env
out, err, _ = gsshell.run(cmd=cmd, shell=shell, cwd=dirname, env=env)
if err:
gs.notice(DOMAIN, err)
out = out.replace('\r', '').replace('\n ', '\\n ').replace('\n\t', '\\n\t')
for m in pat.findall(out):
try:
row, col, msg = m
row = int(row)-1
col = int(col)-1 if col else 0
msg = msg.replace('\\n', '\n').strip()
if row >= 0 and msg:
msg = '%s: %s' % (cmd_domain, msg)
if reports.get(row):
reports[row].msg = '%s\n%s' % (reports[row].msg, msg)
reports[row].col = max(reports[row].col, col)
else:
reports[row] = Report(row, col, msg)
except:
pass
except:
gs.notice(DOMAIN, gs.traceback())
def cb():
fr.reports = reports
fr.state = 1
highlight(fr)
sublime.set_timeout(cb, 0)
class GsCompLintCommand(sublime_plugin.TextCommand):
def run(self, edit):
if gs.setting('comp_lint_enabled') is not True:
return
fn = self.view.file_name()
fn = os.path.abspath(fn)
if fn:
dirname = gs.basedir_or_cwd(fn)
file_refs[fn] = FileRef(self.view)
gsq.dispatch(CL_DOMAIN, lambda: do_comp_lint(dirname, fn), '')
try:
th
except:
th = None
sem = threading.Semaphore()
file_refs = {}
watch()