forked from nim-lang/NimLime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NimCheck.py
305 lines (241 loc) · 9.37 KB
/
NimCheck.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 os.path
import re
from threading import Thread
import NimLime
import sublime
from sublime_plugin import ApplicationCommand, EventListener
from utils.misc import (
view_has_nim_syntax, send_self, busy_frames, get_next_method,
loop_status_msg, trim_region, NimLimeMixin, run_process,
NimLimeOutputMixin)
NimLime.add_module(__name__)
# Constants
ERROR_REGION_TAG = 'NimCheckError'
WARN_REGION_TAG = 'NimCheckWarn'
ERROR_REGION_MARK = 'dot'
ERROR_REGION_STYLE = sublime.DRAW_OUTLINED
ERROR_MSG_FORMAT = '({0},{1}): {2}: {3}'.format
MESSAGE_REGEX = re.compile(
(r"""
^
(.+) \( # File Name
(\d+) \,\s # Line Number
(\d+) \)\s* # Column Number
(\w+) \s*:\s* # Message Type
(.+) \n # Message Content
(.* \n\s* \^)? # Message Context
"""),
flags=re.MULTILINE | re.IGNORECASE | re.VERBOSE
)
# Load settings
def load():
global nim_executable
nim_executable = settings.get('nim.executable', 'nim')
settings = sublime.load_settings('NimLime.sublime-settings')
settings.add_on_change('nim.executable', load)
nim_executable = None
load()
# Commands
class NimClearErrors(NimLimeMixin, ApplicationCommand):
""" Clears error and warning marks generated by the Nim checker. """
settings_selector = 'check.clear_errors'
def run(self):
current_view = sublime.active_window().active_view()
current_view.erase_regions(ERROR_REGION_TAG)
current_view.erase_regions(WARN_REGION_TAG)
sublime.status_message('Cleared Nim Check Errors & Hints')
def is_visible(self):
view = sublime.active_window().active_view()
return ((settings.get('check.current_file.enabled') or
settings.get('check.on_save.enabled')) and
view_has_nim_syntax(view))
class NimCheckCurrentView(NimLimeOutputMixin, ApplicationCommand):
""" Checks the current Nim file for errors. """
settings_selector = 'check.current_file'
def load_settings(self):
super(NimCheckCurrentView, self).load_settings()
get = self.get_setting
self.highlight_errors = get('{0}.highlight_errors', True)
self.highlight_warnings = get('{0}.highlight_warnings', True)
self.include_context = get('{0}.list.include_context', True)
self.list_errors = get('{0}.list.show_errors', True)
self.list_warnings = get('{0}.list.show_warnings', True)
self.move_cursor = get('{0}.list.move_cursor', True)
@send_self
def run(self, show_error_list=True):
this = yield
frames = ["Running Nim Check" + f for f in busy_frames]
stop_status_loop = loop_status_msg(frames, 0.15)
window = sublime.active_window()
view = window.active_view()
view_name = os.path.split(view.file_name() or view.name())[1]
# Save view text
if view.is_dirty():
view.run_command('save')
# Run 'nim check' on the current view and retrieve the output.
output, returncode = yield Thread(
target=run_nimcheck,
args=(view.file_name(), this.send)
).start()
messages = parse_nimcheck_output(output)
yield stop_status_loop(get_next_method(this))
sublime.status_message("Nim Check Finished.")
self.highlight_and_list_messages(messages, window, view)
if self.send_output:
if self.raw_output:
content = output
else:
gen = (m[5] for m in messages if view_name == m[0])
content = "\n".join(gen)
self.write_to_output(content, window, view)
yield
def is_visible(self):
return (self.enabled and view_has_nim_syntax())
def highlight_and_list_messages(self, messages, window, view):
view_name = os.path.split(view.file_name() or view.name())[1]
# For listing
if self.list_errors or self.list_warnings:
quick_message_list = []
point_list = []
# For highlighting
if self.highlight_errors:
error_region_list = []
if self.highlight_warnings:
warn_region_list = []
for file_name, row, column, kind, message, all in messages:
if file_name != view_name:
continue
# For listing
if self.list_errors or self.list_warnings:
point = view.text_point(row, column)
point_list.append(point)
# For highlighting
message_point = view.text_point(row, column)
message_region = trim_region(view, view.line(message_point))
if kind == "Error":
# For listing
if self.list_errors:
if self.include_context:
quick_message_list.append(all.split('\n'))
else:
quick_message_list.append(all.split('\n')[0])
# For highlighting
if self.highlight_errors:
error_region_list.append(message_region)
else:
# For listing
if self.list_warnings:
if self.include_context:
quick_message_list.append(all.split('\n'))
else:
quick_message_list.append(all.split('\n')[0])
# For highlighting
if self.highlight_warnings:
warn_region_list.append(message_region)
if self.highlight_errors:
view.add_regions(
ERROR_REGION_TAG,
error_region_list,
'invalid.illegal',
ERROR_REGION_MARK,
ERROR_REGION_STYLE
)
if self.highlight_warnings:
view.add_regions(
WARN_REGION_TAG,
warn_region_list,
'invalid.deprecated',
ERROR_REGION_MARK,
ERROR_REGION_STYLE
)
if self.list_errors or self.list_warnings:
def goto_error(choice):
if choice != -1:
chosen_point = point_list[choice]
view.show(chosen_point)
if self.move_cursor:
view.sel().clear()
view.sel().add(sublime.Region(chosen_point))
if self.include_context:
window.show_quick_panel(
quick_message_list, goto_error, sublime.MONOSPACE_FONT
)
else:
window.show_quick_panel(quick_message_list, goto_error)
class NimCheckOnSaveListener(NimCheckCurrentView, EventListener):
settings_selector = 'check.on_save'
def on_post_save(self, view):
if self.enabled and view_has_nim_syntax(view):
self.run()
class NimCheckFile(NimLimeOutputMixin, ApplicationCommand):
""" Check an external nim file """
last_entry = ''
settings_selector = 'check.external_file'
def load_settings(self):
super(NimCheckFile, self).load_settings()
get = self.get_setting
self.remember_input = get("{0}.remember_input", True)
self.include_context = get("{0}.output.include_context", True)
@send_self
def run(self):
this = yield
active_window = sublime.active_window()
active_view = active_window.active_view()
# Retrieve user input
initial_text = ''
if self.remember_input:
initial_text = self.last_entry
path = yield active_window.show_input_panel(
"File to check?", initial_text, this.send, None, None
)
self.last_entry = path
# Run 'nim check' on the external file.
if os.path.isfile(path):
frames = ['Checking external file' + f for f in busy_frames]
stop_status_loop = loop_status_msg(frames, 0.25)
output, returncode = yield Thread(
target=run_nimcheck,
args=(path, this.send)
).start()
else:
sublime.error_message(
"File '{0}' does not exist, or isn't a file.".format(path)
)
yield
error_list = parse_nimcheck_output(output)
# Prepare output
error_output = '\n'.join(
[error[4] for error in error_list]
)
# Stop the status loop
yield stop_status_loop(get_next_method(this))
sublime.status_message("External file checked.")
# Print to the output view
fallback_view_name = path + " - Nim Check Output"
self.write_to_output(
error_output, active_window, active_view
)
yield
# Utility functions
def run_nimcheck(file_path, callback):
# Prepare the regex's
run_process(
"{0} check --verbosity:2 \"{1}\"".format(
nim_executable, file_path
),
callback
)
def parse_nimcheck_output(output):
# Retrieve and convert the matches
message_list = []
for match in MESSAGE_REGEX.finditer(output):
all = match.group(0)
file_name = match.group(1)
line = int(match.group(2)) - 1
column = int(match.group(3)) - 1
kind = match.group(4)
message = match.group(5)
message_list.append((file_name, line, column, kind, message, all))
# Sort the error list by line
message_list.sort(key=lambda item: item[1])
return message_list