forked from lunixbochs/sublimelint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublimelint.py
177 lines (139 loc) · 4.45 KB
/
sublimelint.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
# sublimelint.py
# SublimeLint is a code linting support framework for Sublime Text 2
#
# Project: https://github.com/lunixbochs/sublimelint
# License: MIT
import sublime
import sublime_plugin
import os
import thread
import time
import json
from lint.modules import Modules
from lint.linter import Linter
from lint.highlight import HighlightSet
import lint.persist as persist
cwd = os.getcwd()
class SublimeLint(sublime_plugin.EventListener):
def __init__(self, *args, **kwargs):
sublime_plugin.EventListener.__init__(self, *args, **kwargs)
self.loaded = set()
self.linted = set()
self.modules = Modules(cwd, 'languages').load_all()
self.last_syntax = {}
persist.queue.start(self.lint)
# this gives us a chance to lint the active view on fresh install
window = sublime.active_window()
if window:
sublime.set_timeout(
lambda: self.on_activated(window.active_view()), 100
)
self.start = time.time()
def lint(self, view_id):
view = Linter.get_view(view_id)
sections = {}
for sel, _ in Linter.get_selectors(view_id):
sections[sel] = []
for result in view.find_by_selector(sel):
sections[sel].append(
(view.rowcol(result.a)[0], result.a, result.b)
)
if view is not None:
filename = view.file_name()
code = Linter.text(view)
thread.start_new_thread(Linter.lint_view, (view_id, filename, code, sections, self.finish))
def finish(self, view, linters):
errors = {}
highlights = HighlightSet()
for linter in linters:
if linter.highlight:
highlights.add(linter.highlight)
if linter.errors:
errors.update(linter.errors)
highlights.clear(view)
highlights.draw(view)
persist.errors[view.id()] = errors
self.on_selection_modified(view)
# helpers
def hit(self, view):
self.linted.add(view.id())
if view.size() == 0:
for l in Linter.get_linters(view.id()):
l.clear()
return
persist.queue.hit(view)
def check_syntax(self, view, lint=False):
vid = view.id()
syntax = view.settings().get('syntax')
# syntax either has never been set or just changed
if not vid in self.last_syntax or self.last_syntax[vid] != syntax:
self.last_syntax[vid] = syntax
# assign a linter, then maybe trigger a lint if we get one
if Linter.assign(view) and lint:
self.hit(view)
# callins
def on_modified(self, view):
self.check_syntax(view)
self.hit(view)
def on_load(self, view):
self.on_new(view)
def on_activated(self, view):
sublime.set_timeout(lambda: self.check_syntax(view, True), 50)
view_id = view.id()
if not view_id in self.linted:
if not view_id in self.loaded:
# it seems on_activated can be called before loaded on first start
if time.time() - self.start < 5: return
self.on_new(view)
self.hit(view)
def on_open_settings(self, view):
# handle opening user preferences file
if view.file_name():
filename = view.file_name()
dirname = os.path.basename(os.path.dirname(filename))
if filename != 'SublimeLint.sublime-settings':
return
if dirname.lower() == 'sublimelint':
return
settings = persist.settings
edit = view.begin_edit()
view.replace(edit, sublime.Region(0, view.size()),
json.dumps({'user': settings}, indent=4)
)
view.end_edit(edit)
def on_new(self, view):
self.on_open_settings(view)
vid = view.id()
self.loaded.add(vid)
self.last_syntax[vid] = view.settings().get('syntax')
Linter.assign(view)
def on_post_save(self, view):
# this will reload submodules if they are saved with sublime text
for name, module in self.modules.modules.items():
if os.name == 'posix' and (
os.stat(module.__file__).st_ino == os.stat(view.file_name()).st_ino
) or module.__file__ == view.file_name():
self.modules.reload(module)
Linter.reload(name)
break
# linting here doesn't matter, because we lint on load and on modify
# self.hit(view)
def on_selection_modified(self, view):
vid = view.id()
lineno = view.rowcol(view.sel()[0].end())[0]
view.erase_status('sublimelint')
if vid in persist.errors:
errors = persist.errors[vid]
if errors:
plural = 's' if len(errors) > 1 else ''
if lineno in errors:
status = ''
if plural:
num = sorted(list(errors)).index(lineno) + 1
status += '%i/%i errors: ' % (num, len(errors))
# sublime statusbar can't hold unicode
status += '; '.join(set(errors[lineno])).encode('ascii', 'replace')
else:
status = '%i error%s' % (len(errors), plural)
view.set_status('sublimelint', status)
persist.queue.delay()