Skip to content

Commit 7a8df72

Browse files
committed
Add a command to view the rendered build results
1 parent 91a2f80 commit 7a8df72

5 files changed

+99
-27
lines changed

RustEnhanced.sublime-commands

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
"caption": "Rust: Open Debug Log",
5757
"command": "rust_open_log"
5858
},
59+
{
60+
"caption": "Rust: Open Last Build Output As View",
61+
"command": "rust_show_build_output"
62+
},
5963
{
6064
"caption": "Rust: Popup Message At Cursor",
6165
"command": "rust_message_popup"

SyntaxCheckPlugin.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class RustSyntaxCheckThread(rust_thread.RustThread, rust_proc.ProcListener):
6767
def __init__(self, view):
6868
self.view = view
6969
self.window = view.window()
70+
self.rendered = []
7071
super(RustSyntaxCheckThread, self).__init__(view.window())
7172

7273
def run(self):
@@ -190,23 +191,37 @@ def get_rustc_messages(self):
190191
#########################################################################
191192

192193
def on_begin(self, proc):
193-
pass
194+
self.rendered.append('[Running: %s]' % (' '.join(proc.cmd),))
194195

195196
def on_data(self, proc, data):
196197
log.log(self.window, data)
198+
self.rendered.append(data)
197199

198200
def on_error(self, proc, message):
199201
log.critical(self.window, 'Rust Error: %s', message)
202+
self.rendered.append(message)
200203

201204
def on_json(self, proc, obj):
202-
messages.add_rust_messages(self.window, self.msg_rel_path, obj,
205+
try:
206+
message = obj['message']
207+
except KeyError:
208+
return
209+
messages.add_rust_messages(self.window, self.msg_rel_path, message,
203210
self.current_target_src, msg_cb=None)
204211
if messages.has_message_for_path(self.window,
205212
self.triggered_file_name):
206213
self.this_view_found = True
214+
try:
215+
self.rendered.append(message['rendered'])
216+
except KeyError:
217+
pass
207218

208219
def on_finished(self, proc, rc):
209220
log.log(self.window, 'On-save check finished.')
221+
# TODO: Also put message in self.rendered about [Finished in …]
222+
# TODO: Figure out how to share all this code between here and opanel
223+
win_info = messages.get_or_init_window_info(self.window)
224+
win_info['rendered'] = ''.join(self.rendered)
210225

211226
def on_terminated(self, proc):
212227
log.log(self.window, 'Process Interrupted')

cargo_build.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,10 @@ def on_load_async(self):
243243
class NextPrevBase(sublime_plugin.WindowCommand):
244244

245245
def _has_inline(self):
246-
return self.window.id() in messages.WINDOW_MESSAGES
246+
try:
247+
return messages.WINDOW_MESSAGES[self.window.id()]['has_inline']
248+
except KeyError:
249+
return False
247250

248251

249252
class RustNextMessageCommand(NextPrevBase):
@@ -521,6 +524,22 @@ def on_selection_modified_async(self):
521524
messages.update_status(view)
522525

523526

527+
class RustShowBuildOutput(sublime_plugin.WindowCommand):
528+
529+
"""Opens a view with the rustc-rendered compiler output."""
530+
531+
def run(self):
532+
view = self.window.new_file()
533+
view.set_scratch(True)
534+
view.set_name('Rust Enhanced Build Output')
535+
view.assign_syntax('Cargo.sublime-syntax')
536+
win_info = messages.get_or_init_window_info(self.window)
537+
output = win_info['rendered']
538+
if output == '':
539+
output = "No output available for this window."
540+
view.run_command('append', {'characters': output})
541+
542+
524543
class RustEventListener(sublime_plugin.EventListener):
525544

526545
def on_activated_async(self, view):

rust/messages.py

+44-21
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
# Value is a dictionary: {
2222
# 'paths': {path: [MessageBatch, ...]},
2323
# 'batch_index': (path_idx, message_idx),
24-
# 'hidden': bool
24+
# 'hidden': bool,
25+
# 'rendered': string,
26+
# 'has_inline': False,
2527
# }
2628
# `paths` is an OrderedDict to handle next/prev message.
2729
# `path` is the absolute path to the file.
2830
# `hidden` indicates that all messages have been dismissed.
31+
# `rendered` is the rustc-rendered output
32+
# 'has_inline' is a boolean indicating if inline messages were added
2933
WINDOW_MESSAGES = {}
3034

3135

@@ -302,9 +306,8 @@ def _draw_region_highlights(view, batch):
302306

303307
def batches_at_point(view, point, hover_zone):
304308
"""Return a list of message batches at the given point."""
305-
try:
306-
winfo = WINDOW_MESSAGES[view.window().id()]
307-
except KeyError:
309+
winfo = get_window_info_for_view(view)
310+
if winfo is None:
308311
return
309312
if winfo['hidden']:
310313
return
@@ -615,9 +618,8 @@ def redraw_all_open_views(window):
615618

616619
def show_messages_for_view(view):
617620
"""Adds all phantoms and region outlines for a view."""
618-
try:
619-
winfo = WINDOW_MESSAGES[view.window().id()]
620-
except KeyError:
621+
winfo = get_window_info_for_view(view)
622+
if winfo is None:
621623
return
622624
if winfo['hidden']:
623625
return
@@ -628,9 +630,8 @@ def show_messages_for_view(view):
628630

629631

630632
def draw_regions_if_missing(view):
631-
try:
632-
winfo = WINDOW_MESSAGES[view.window().id()]
633-
except KeyError:
633+
winfo = get_window_info_for_view(view)
634+
if winfo is None:
634635
return
635636
if winfo['hidden']:
636637
return
@@ -1180,16 +1181,9 @@ def _save_batches(window, batches, msg_cb):
11801181
- Displays phantoms if a view is already open.
11811182
- Calls `msg_cb` for each individual message.
11821183
"""
1183-
wid = window.id()
1184-
try:
1185-
path_to_batches = WINDOW_MESSAGES[wid]['paths']
1186-
except KeyError:
1187-
path_to_batches = collections.OrderedDict()
1188-
WINDOW_MESSAGES[wid] = {
1189-
'paths': path_to_batches,
1190-
'batch_index': (-1, -1),
1191-
'hidden': False,
1192-
}
1184+
win_info = get_or_init_window_info(window)
1185+
win_info['has_inline'] = True
1186+
path_to_batches = win_info['paths']
11931187

11941188
for batch in batches:
11951189
path_batches = path_to_batches.setdefault(batch.path(), [])
@@ -1198,7 +1192,7 @@ def _save_batches(window, batches, msg_cb):
11981192
path_batches.append(batch)
11991193
for i, msg in enumerate(batch):
12001194
msg.region_key = 'rust-%i' % (num + i,)
1201-
if not WINDOW_MESSAGES[wid]['hidden']:
1195+
if not win_info['hidden']:
12021196
views = util.open_views_for_file(window, batch.path())
12031197
if views:
12041198
# Phantoms seem to be attached to the buffer.
@@ -1208,3 +1202,32 @@ def _save_batches(window, batches, msg_cb):
12081202
if msg_cb:
12091203
for msg in batch:
12101204
msg_cb(msg)
1205+
1206+
1207+
def get_or_init_window_info(window):
1208+
"""Returns the window info for the given window, creating it if it hasn't been set."""
1209+
wid = window.id()
1210+
try:
1211+
return WINDOW_MESSAGES[wid]
1212+
except KeyError:
1213+
win_info = {
1214+
'paths': collections.OrderedDict(),
1215+
'batch_index': (-1, -1),
1216+
'hidden': False,
1217+
'rendered': '',
1218+
'has_inline': False,
1219+
}
1220+
WINDOW_MESSAGES[wid] = win_info
1221+
return win_info
1222+
1223+
1224+
def get_window_info_for_view(view):
1225+
"""Returns the window info for the given view, or None if not available."""
1226+
window = view.window()
1227+
if window is None:
1228+
# I'm not entire sure why this happens sometimes.
1229+
return None
1230+
try:
1231+
return WINDOW_MESSAGES[window.id()]
1232+
except KeyError:
1233+
return None

rust/opanel.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def __init__(self, window, base_path, command_name, rustc_version):
7171
self.base_path = base_path
7272
self.command_name = command_name
7373
self.rustc_version = rustc_version
74+
self.rendered = []
7475

7576
def on_begin(self, proc):
7677
self.output_view = create_output_panel(self.window, self.base_path)
@@ -114,9 +115,16 @@ def on_error(self, proc, message):
114115
self._append(message)
115116

116117
def on_json(self, proc, obj):
117-
if 'message' in obj:
118-
messages.add_rust_messages(self.window, self.base_path, obj['message'],
119-
None, self.msg_cb)
118+
try:
119+
message = obj['message']
120+
except KeyError:
121+
return
122+
messages.add_rust_messages(self.window, self.base_path, message,
123+
None, self.msg_cb)
124+
try:
125+
self.rendered.append(message['rendered'])
126+
except KeyError:
127+
pass
120128

121129
def msg_cb(self, message):
122130
"""Display the message in the output panel. Also marks the message
@@ -155,6 +163,8 @@ def on_finished(self, proc, rc):
155163
# Tell Sublime to find all of the lines with pattern from
156164
# result_file_regex.
157165
self.output_view.find_all_results()
166+
win_info = messages.get_or_init_window_info(self.window)
167+
win_info['rendered'] = ''.join(self.rendered)
158168

159169
def on_terminated(self, proc):
160170
self._append('[Build interrupted]')
@@ -163,6 +173,7 @@ def _append(self, message, nl=True):
163173
if nl:
164174
message += '\n'
165175
_append(self.output_view, message)
176+
self.rendered.append(message)
166177

167178
def _display_debug(self, proc):
168179
# Display some information to help the user debug any build problems.

0 commit comments

Comments
 (0)