forked from SublimeHaskell/SublimeHaskell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parseoutput.py
350 lines (273 loc) · 11.4 KB
/
parseoutput.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import os
import re
import sublime
import sublime_plugin
import time
from sys import version
from threading import Thread
from collections import defaultdict
PyV3 = version[0] == "3"
if int(sublime.version()) < 3000:
from sublime_haskell_common import log, are_paths_equal, call_and_wait, get_setting_async, show_status_message_process, show_status_message
else:
from SublimeHaskell.sublime_haskell_common import log, are_paths_equal, call_and_wait, get_setting_async, show_status_message_process, show_status_message
ERROR_PANEL_NAME = 'haskell_error_checker'
# This regex matches an unindented line, followed by zero or more
# indented, non-empty lines.
# It also eats whitespace before the first line.
# The first line is divided into a filename, a line number, and a column.
output_regex = re.compile(
r'\s*^(\S*):(\d+):(\d+):(.*$(?:\n^[ \t].*$)*)',
re.MULTILINE)
# Extract the filename, line, column, and description from an error message:
result_file_regex = r'^(\S*?): line (\d+), column (\d+):$'
# Global list of errors. Used e.g. for jumping to the next one.
# Properly assigned being a defaultdict in clear_error_marks().
# Structure: ERRORS[filename][m.line] = OutputMessage()
ERRORS = {}
def filename_of_path(p):
"""Returns everything after the last slash or backslash."""
# Not using os.path here because we don't know/care here if
# we have forward or backslashes on Windows.
return re.match(r'(.*[/\\])?(.*)', p).groups()[1]
class OutputMessage(object):
"Describe an error or warning message produced by GHC."
def __init__(self, filename, line, column, message, level):
self.filename = filename
self.line = int(line)
self.column = int(column)
self.message = message.replace(os.linesep, "\n")
self.level = level
def __unicode__(self):
# must match result_file_regex
return u'{0}: line {1}, column {2}:\n {3}'.format(
self.filename,
self.line,
self.column,
self.message)
def __str__(self):
return self.__unicode__()
def __repr__(self):
return '<OutputMessage {0}:{1}:{2}: {3}>'.format(
filename_of_path(self.filename),
self.line,
self.column,
self.message[:10] + '..')
def find_region_in_view(self, view):
"Return the Region referred to by this error message."
# Convert line and column count to zero-based indices:
point = view.text_point(self.line - 1, 0)
# Return the whole line:
region = view.line(point)
region = trim_region(view, region)
return region
def clear_error_marks():
global ERRORS
listdict = lambda: defaultdict(list)
ERRORS = defaultdict(listdict)
def set_global_error_messages(messages):
global ERRORS
clear_error_marks()
for m in messages:
ERRORS[m.filename][m.line].append(m)
def run_build_thread(view, cabal_project_dir, msg, cmd, on_done):
run_chain_build_thread(view, cabal_project_dir, msg, [cmd], on_done)
def run_chain_build_thread(view, cabal_project_dir, msg, cmds, on_done):
show_status_message_process(msg, priority = 3)
thread = Thread(
target=wait_for_chain_to_complete,
args=(view, cabal_project_dir, msg, cmds, on_done))
thread.start()
def wait_for_build_to_complete(view, cabal_project_dir, msg, cmd, on_done):
"""Run a command, wait for it to complete, then parse and display
the resulting errors."""
wait_for_chain_to_complete(view, cabal_project_dir, msg, [cmd], on_done)
def wait_for_chain_to_complete(view, cabal_project_dir, msg, cmds, on_done):
"""Chains several commands, wait for them to complete, then parse and display
the resulting errors."""
# First hide error panel to show that something is going on
sublime.set_timeout(lambda: hide_output(view), 0)
# run and wait commands, fail on first fail
for cmd in cmds:
exit_code, stdout, stderr = call_and_wait(
cmd,
cwd=cabal_project_dir)
if exit_code != 0:
break
errmsg = stderr if stderr else stdout
# Notify UI thread that commands are done
sublime.set_timeout(on_done, 0)
parse_output_messages_and_show(view, msg, cabal_project_dir, exit_code, errmsg)
def format_output_messages(messages):
"""Formats list of messages"""
if PyV3:
return '\n'.join(str(x) for x in messages)
else:
return u'\n'.join(unicode(x) for x in messages)
def show_output_result_text(view, msg, text, exit_code, base_dir):
"""Shows text (formatted messages) in output with build result"""
success = exit_code == 0
success_message = 'SUCCEEDED' if success else 'FAILED'
output = u'Build {0}\n\n{1}'.format(success_message, text.strip())
show_status_message_process(msg, success)
# Show panel if there is any text to show (without the part that we add)
if text:
if get_setting_async('show_output_window'):
sublime.set_timeout(lambda: write_output(view, output, base_dir), 0)
def parse_output_messages_and_show(view, msg, base_dir, exit_code, stderr):
"""Parse errors and display resulting errors"""
# stderr/stdout can contain unicode characters
# already done in call_and_wait
# stderr = stderr.decode('utf-8')
# The process has terminated; parse and display the output:
parsed_messages = parse_output_messages(base_dir, stderr)
# The unparseable part (for other errors)
unparsable = output_regex.sub('', stderr).strip()
# Set global error list
set_global_error_messages(parsed_messages)
# If we couldn't parse any messages, just show the stderr
# Otherwise the parsed errors and the unparsable stderr remainder
outputs = []
if parsed_messages:
outputs += [format_output_messages(parsed_messages)]
if unparsable:
outputs += ["\nREMAINING STDERR:\n", unparsable]
output_text = '\n'.join(outputs)
show_output_result_text(view, msg, output_text, exit_code, base_dir)
sublime.set_timeout(lambda: mark_messages_in_views(parsed_messages), 0)
def mark_messages_in_views(errors):
"Mark the regions in open views where errors were found."
begin_time = time.clock()
# Mark each diagnostic in each open view in all windows:
for w in sublime.windows():
for v in w.views():
view_filename = v.file_name()
# Unsaved files have no file name
if view_filename is None:
continue
errors_in_view = list(filter(
lambda x: are_paths_equal(view_filename, x.filename),
errors))
mark_messages_in_view(errors_in_view, v)
end_time = time.clock()
log('total time to mark {0} diagnostics: {1} seconds'.format(
len(errors), end_time - begin_time))
message_levels = {
'hint': {
'style': 'comment.warning',
'icon': 'light_x_bright'
},
'warning': {
'style': 'comment.warning',
'icon': 'grey_x_light_shadow'
},
'error': {
'style': 'invalid',
'icon': 'grey_x'
}
}
# These next and previous commands were shamelessly copied
# from the great SublimeClang plugin.
class SublimeHaskellNextError(sublime_plugin.TextCommand):
def run(self, edit):
log("SublimeHaskellNextError")
v = self.view
fn = v.file_name().encode("utf-8")
line, column = v.rowcol(v.sel()[0].a)
line += 1
gotoline = -1
if fn in ERRORS:
for errLine in sorted(ERRORS[fn].keys()):
if errLine > line:
gotoline = errLine
break
# No next line: Wrap around if possible
if gotoline == -1 and len(ERRORS[fn]) > 0:
gotoline = sorted(ERRORS[fn].keys())[0]
if gotoline != -1:
v.window().open_file("%s:%d" % (fn, gotoline), sublime.ENCODED_POSITION)
else:
sublime.status_message("No more errors or warnings!")
class SublimeHaskellPreviousError(sublime_plugin.TextCommand):
def run(self, edit):
v = self.view
fn = v.file_name().encode("utf-8")
line, column = v.rowcol(v.sel()[0].a)
line += 1
gotoline = -1
if fn in ERRORS:
for errLine in sorted(ERRORS[fn].keys(), key = lambda x: -x):
if errLine < line:
gotoline = errLine
break
# No previous line: Wrap around if possible
if gotoline == -1 and len(ERRORS[fn]) > 0:
gotoline = sorted(ERRORS[fn].keys())[-1]
if gotoline != -1:
v.window().open_file("%s:%d" % (fn, gotoline), sublime.ENCODED_POSITION)
else:
sublime.status_message("No more errors or warnings!")
def region_key(name):
return 'subhs-{0}s'.format(name)
def mark_messages_in_view(messages, view):
# Regions by level
regions = {}
for k in message_levels.keys():
regions[k] = []
for m in messages:
regions[m.level].append(m.find_region_in_view(view))
for nm, lev in message_levels.items():
view.erase_regions(region_key(nm))
view.add_regions(
region_key(nm),
regions[nm],
lev['style'],
lev['icon'],
sublime.DRAW_OUTLINED)
def write_output(view, text, cabal_project_dir):
"Write text to Sublime's output panel."
output_view = view.window().get_output_panel(ERROR_PANEL_NAME)
output_view.set_read_only(False)
# Configure Sublime's error message parsing:
output_view.settings().set("result_file_regex", result_file_regex)
output_view.settings().set("result_base_dir", cabal_project_dir)
# Write to the output buffer:
output_view.run_command('sublime_haskell_output_text', {
'text': text })
# Set the selection to the beginning of the view so that "next result" works:
output_view.sel().clear()
output_view.sel().add(sublime.Region(0))
output_view.set_read_only(True)
# Show the results panel:
view.window().run_command('show_panel', {'panel': 'output.' + ERROR_PANEL_NAME})
def hide_output(view):
view.window().run_command('hide_panel', {'panel': 'output.' + ERROR_PANEL_NAME})
def parse_output_messages(base_dir, text):
"Parse text into a list of OutputMessage objects."
matches = output_regex.finditer(text)
def to_error(m):
filename, line, column, messy_details = m.groups()
return OutputMessage(
# Record the absolute, normalized path.
os.path.normpath(os.path.join(base_dir, filename)),
line,
column,
messy_details.strip(),
'warning' if 'warning' in messy_details.lower() else 'error')
return list(map(to_error, matches))
def trim_region(view, region):
"Return the specified Region, but without leading or trailing whitespace."
text = view.substr(region)
# Regions may be selected backwards, so b could be less than a.
a = min(region.a, region.b)
b = max(region.a, region.b)
# Figure out how much to move the endpoints to lose the space.
# If the region is entirely whitespace, give up and return it unchanged.
if text.isspace():
return region
else:
text_trimmed_on_left = text.lstrip()
text_trimmed = text_trimmed_on_left.rstrip()
a += len(text) - len(text_trimmed_on_left)
b -= len(text_trimmed_on_left) - len(text_trimmed)
return sublime.Region(a, b)