forked from fbzhong/sublime-jslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jslint.py
213 lines (173 loc) · 6.43 KB
/
jslint.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
import os
import re
import sublime
import sublime_plugin
from statusprocess import *
from asyncprocess import *
RESULT_VIEW_NAME = 'jslint_result_view'
SETTINGS_FILE = "sublime-jslint.sublime-settings"
class ShowJslintResultCommand(sublime_plugin.WindowCommand):
"""show jslint result"""
def run(self):
self.window.run_command("show_panel", {"panel": "output."+RESULT_VIEW_NAME})
class JslintCommand(sublime_plugin.WindowCommand):
def run(self):
s = sublime.load_settings(SETTINGS_FILE)
file_path = self.window.active_view().file_name()
file_name = os.path.basename(file_path)
self.debug = s.get('debug', False)
self.buffered_data = ''
self.file_path = file_path
self.file_name = file_name
self.is_running = True
self.tests_panel_showed = False
self.ignored_error_count = 0
self.ignore_errors = s.get('ignore_errors', [])
self.use_node_jslint = s.get('use_node_jslint', False)
self.init_tests_panel()
if (self.use_node_jslint):
cmd = 'jslint ' + s.get('node_jslint_options', '') + ' "' + file_path + '"'
else:
if len(s.get('jslint_jar', '')) > 0:
jslint_jar = s.get('jslint_jar')
else:
jslint_jar = sublime.packages_path() + '/sublime-jslint/jslint4java-2.0.1.jar'
cmd = 'java -jar "' + jslint_jar + '" ' + s.get('jslint_options', '') + ' "' + file_path + '"'
if self.debug:
print "DEBUG: " + str(cmd)
AsyncProcess(cmd, self)
StatusProcess('Starting JSLint for file ' + file_name, self)
JsLintEventListener.disabled = True
def init_tests_panel(self):
if not hasattr(self, 'output_view'):
self.output_view = self.window.get_output_panel(RESULT_VIEW_NAME)
self.output_view.set_name(RESULT_VIEW_NAME)
self.clear_test_view()
self.output_view.settings().set("file_path", self.file_path)
def show_tests_panel(self):
if self.tests_panel_showed:
return
self.window.run_command("show_panel", {"panel": "output."+RESULT_VIEW_NAME})
self.tests_panel_showed = True
def clear_test_view(self):
self.output_view.set_read_only(False)
edit = self.output_view.begin_edit()
self.output_view.erase(edit, sublime.Region(0, self.output_view.size()))
self.output_view.end_edit(edit)
self.output_view.set_read_only(True)
def append_data(self, proc, data, end=False):
self.buffered_data = self.buffered_data + data.decode("utf-8")
data = self.buffered_data.replace(self.file_path, self.file_name).replace('\r\n', '\n').replace('\r', '\n')
if end == False:
rsep_pos = data.rfind('\n')
if rsep_pos == -1:
# not found full line.
return
self.buffered_data = data[rsep_pos+1:]
data = data[:rsep_pos+1]
# ignore error.
text = data
if (len(self.ignore_errors) > 0) and (not self.use_node_jslint):
text = ''
for line in data.split('\n'):
if len(line) == 0:
continue
ignored = False
for rule in self.ignore_errors:
if re.search(rule, line):
ignored = True
self.ignored_error_count += 1
if self.debug:
print "text match line "
print "rule = " + rule
print "line = " + line
print "---------"
break
if ignored == False:
text += line + '\n'
self.show_tests_panel()
selection_was_at_end = (len(self.output_view.sel()) == 1 and self.output_view.sel()[0] == sublime.Region(self.output_view.size()))
self.output_view.set_read_only(False)
edit = self.output_view.begin_edit()
self.output_view.insert(edit, self.output_view.size(), text)
if end and not self.use_node_jslint:
text = '\njslint: ignored ' + str(self.ignored_error_count) + ' errors.\n'
self.output_view.insert(edit, self.output_view.size(), text)
# if selection_was_at_end:
# self.output_view.show(self.output_view.size())
self.output_view.end_edit(edit)
self.output_view.set_read_only(True)
# if end:
# self.output_view.run_command("goto_line", {"line": 1})
def update_status(self, msg, progress):
sublime.status_message(msg + " " + progress)
def proc_terminated(self, proc):
if proc.returncode == 0:
msg = self.file_name + ' lint free!'
else:
msg = ''
self.append_data(proc, msg, True)
JsLintEventListener.disabled = False
class JsLintEventListener(sublime_plugin.EventListener):
"""jslint event"""
disabled = False
def __init__(self):
self.previous_resion = None
self.file_view = None
def on_post_save(self, view):
s = sublime.load_settings(SETTINGS_FILE)
if s.get('run_on_save', False) == False:
return
if view.file_name().endswith('.js') == False:
return
# run jslint.
sublime.active_window().run_command("jslint")
def on_deactivated(self, view):
if view.name() != RESULT_VIEW_NAME:
return
self.previous_resion = None
if self.file_view:
self.file_view.erase_regions(RESULT_VIEW_NAME)
def on_selection_modified(self, view):
if JsLintEventListener.disabled:
return
if view.name() != RESULT_VIEW_NAME:
return
region = view.line(view.sel()[0])
s = sublime.load_settings(SETTINGS_FILE)
# make sure call once.
if self.previous_resion == region:
return
self.previous_resion = region
# extract line from jslint result.
if (s.get('use_node_jslint', False)):
pattern_position = "\\/\\/ Line (\d+), Pos (\d+)$"
text = view.substr(region)
text = re.findall(pattern_position, text)
if len(text) > 0:
line = int(text[0][0])
col = int(text[0][1])
else:
text = view.substr(region).split(':')
if len(text) < 4 or text[0] != 'jslint' or re.match('\d+', text[2]) == None or re.match('\d+', text[3]) == None:
return
line = int(text[2])
col = int(text[3])
# hightligh view line.
view.add_regions(RESULT_VIEW_NAME, [region], "comment")
# find the file view.
file_path = view.settings().get('file_path')
window = sublime.active_window()
file_view = None
for v in window.views():
if v.file_name() == file_path:
file_view = v
break
if file_view == None:
return
self.file_view = file_view
window.focus_view(file_view)
file_view.run_command("goto_line", {"line": line})
file_region = file_view.line(file_view.sel()[0])
# highlight file_view line
file_view.add_regions(RESULT_VIEW_NAME, [file_region], "string")