forked from rust-lang/rust-enhanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cargo_build.py
487 lines (371 loc) · 15.6 KB
/
cargo_build.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
"""Sublime commands for the cargo build system."""
import functools
import sublime
import sublime_plugin
from .rust import (rust_proc, rust_thread, opanel, util, messages,
cargo_settings, target_detect)
from .rust.cargo_config import *
# Maps command to an input string. Used to pre-populate the input panel with
# the last entered value.
LAST_EXTRA_ARGS = {}
class CargoExecCommand(sublime_plugin.WindowCommand):
"""cargo_exec Sublime command.
This takes the following arguments:
- `command`: The command name to run. Commands are defined in the
`cargo_settings` module. You can define your own custom command by
passing in `command_info`.
- `command_info`: Dictionary of values the defines how the cargo command
is constructed. See `cargo_settings.CARGO_COMMANDS`.
- `settings`: Dictionary of settings overriding anything set in the
Sublime project settings (see `cargo_settings` module).
"""
# The combined command info from `cargo_settings` and whatever the user
# passed in.
command_info = None
# Dictionary of initial settings passed in by the user.
initial_settings = None
# CargoSettings instance.
settings = None
# Directory where to run the command.
working_dir = None
# Path used for the settings key. This is typically `working_dir` except
# for `cargo script`, in which case it is the path to the .rs source file.
settings_path = None
def run(self, command=None, command_info=None, settings=None):
if command is None:
return self.window.run_command('build', {'select': True})
self.initial_settings = settings if settings else {}
self.settings = cargo_settings.CargoSettings(self.window)
self.settings.load()
if command == 'auto':
self._detect_auto_build()
else:
self.command_name = command
self.command_info = cargo_settings.CARGO_COMMANDS\
.get(command, {}).copy()
if command_info:
self.command_info.update(command_info)
self._determine_working_path(self._run_check_for_args)
def _detect_auto_build(self):
"""Handle the "auto" build variant, which automatically picks a build
command based on the current view."""
if not util.active_view_is_rust():
sublime.error_message(util.multiline_fix("""
Error: Could not determine what to build.
Open a Rust source file as the active Sublime view.
"""))
return
td = target_detect.TargetDetector(self.window)
view = self.window.active_view()
targets = td.determine_targets(view.file_name())
if len(targets) == 0:
sublime.error_message(util.multiline_fix("""
Error: Could not determine what to build.
Try using one of the explicit build variants.
"""))
return
elif len(targets) == 1:
self._auto_choice_made(targets, 0)
else:
# Can't determine a single target, let the user choose one.
display_items = [' '.join(x[1]) for x in targets]
on_done = functools.partial(self._auto_choice_made, targets)
self.window.show_quick_panel(display_items, on_done)
def _auto_choice_made(self, targets, index):
if index != -1:
src_path, cmd_line = targets[index]
actions = {
'--bin': 'run',
'--example': 'run',
'--lib': 'build',
'--bench': 'bench',
'--test': 'test',
}
cmd = actions[cmd_line[0]]
self.initial_settings['target'] = ' '.join(cmd_line)
self.run(command=cmd, settings=self.initial_settings)
def _determine_working_path(self, on_done):
"""Determine where Cargo should be run.
This may trigger some Sublime user interaction if necessary.
"""
working_dir = self.initial_settings.get('working_dir')
if working_dir:
self.working_dir = working_dir
self.settings_path = working_dir
return on_done()
script_path = self.initial_settings.get('script_path')
if script_path:
self.working_dir = os.path.dirname(script_path)
self.settings_path = script_path
return on_done()
default_path = self.settings.get_project_base('default_path')
if default_path:
self.settings_path = default_path
if os.path.isfile(default_path):
self.working_dir = os.path.dirname(default_path)
else:
self.working_dir = default_path
return on_done()
if self.command_info.get('requires_manifest', True):
cmd = CargoConfigPackage(self.window)
cmd.run(functools.partial(self._on_manifest_choice, on_done))
else:
# For now, assume you need a Rust file if not needing a manifest
# (for `cargo script`).
view = self.window.active_view()
if util.active_view_is_rust(view=view):
self.settings_path = view.file_name()
self.working_dir = os.path.dirname(self.settings_path)
return on_done()
else:
sublime.error_message(util.multiline_fix("""
Error: Could not determine what Rust source file to use.
Open a Rust source file as the active Sublime view."""))
return
def _on_manifest_choice(self, on_done, package_path):
self.settings_path = package_path
self.working_dir = package_path
on_done()
def _run_check_for_args(self):
if self.command_info.get('wants_run_args', False) and \
not self.initial_settings.get('extra_run_args'):
self.window.show_input_panel('Enter extra args:',
LAST_EXTRA_ARGS.get(self.command_name, ''),
self._on_extra_args, None, None)
else:
self._run()
def _on_extra_args(self, args):
LAST_EXTRA_ARGS[self.command_info['command']] = args
self.initial_settings['extra_run_args'] = args
self._run()
def _run(self):
t = CargoExecThread(self.window, self.settings,
self.command_name, self.command_info,
self.initial_settings,
self.settings_path, self.working_dir)
t.start()
class CargoExecThread(rust_thread.RustThread):
silently_interruptible = False
name = 'Cargo Exec'
def __init__(self, window, settings,
command_name, command_info,
initial_settings, settings_path, working_dir):
super(CargoExecThread, self).__init__(window)
self.settings = settings
self.command_name = command_name
self.command_info = command_info
self.initial_settings = initial_settings
self.settings_path = settings_path
self.working_dir = working_dir
def run(self):
cmd = self.settings.get_command(self.command_name,
self.command_info,
self.settings_path,
self.initial_settings)
if not cmd:
return
messages.clear_messages(self.window)
p = rust_proc.RustProc()
listener = opanel.OutputListener(self.window, self.working_dir,
self.command_name)
decode_json = util.get_setting('show_errors_inline', True) and \
self.command_info.get('allows_json', False)
try:
p.run(self.window, cmd['command'],
self.working_dir, listener,
env=cmd['env'],
decode_json=decode_json,
json_stop_pattern=self.command_info.get('json_stop_pattern'))
p.wait()
except rust_proc.ProcessTerminatedError:
return
# This is used by the test code. Due to the async nature of the on_load event,
# it can cause problems with the rapid loading of views.
ON_LOAD_MESSAGES_ENABLED = True
class CargoEventListener(sublime_plugin.EventListener):
"""Every time a new file is loaded, check if is a Rust file with messages,
and if so, display the messages.
"""
def on_load(self, view):
if ON_LOAD_MESSAGES_ENABLED and util.active_view_is_rust(view=view):
# For some reason, view.window() returns None here.
# Use set_timeout to give it time to attach to a window.
sublime.set_timeout(
lambda: messages.show_messages_for_view(view), 1)
def on_query_context(self, view, key, operator, operand, match_all):
# Used by the Escape-key keybinding to dismiss inline phantoms.
if key == 'rust_has_messages':
has_messages = view.window().id() in messages.WINDOW_MESSAGES
if operator == sublime.OP_EQUAL:
return operand == has_messages
elif operator == sublime.OP_NOT_EQUAL:
return operand != has_messages
return None
class NextPrevBase(sublime_plugin.WindowCommand):
def _has_inline(self):
return self.window.id() in messages.WINDOW_MESSAGES
class RustNextMessageCommand(NextPrevBase):
def run(self, levels='all'):
if self._has_inline():
messages.show_next_message(self.window, levels)
else:
self.window.run_command('next_result')
class RustPrevMessageCommand(NextPrevBase):
def run(self, levels='all'):
if self._has_inline():
messages.show_prev_message(self.window, levels)
else:
self.window.run_command('prev_result')
class RustCancelCommand(sublime_plugin.WindowCommand):
def run(self):
try:
t = rust_thread.THREADS[self.window.id()]
except KeyError:
pass
else:
t.terminate()
# Also call Sublime's cancel command, in case the user is using a
# normal Sublime build.
self.window.run_command('cancel_build')
class RustDismissMessagesCommand(sublime_plugin.WindowCommand):
"""Removes all inline messages."""
def run(self):
messages.clear_messages(self.window)
class RustListMessagesCommand(sublime_plugin.WindowCommand):
"""Shows a quick panel with a list of all messages."""
def run(self):
messages.list_messages(self.window)
# Patterns used to help find test function names.
# This is far from perfect, but should be good enough.
SPACE = r'[ \t]'
OPT_COMMENT = r"""(?:
(?: [ \t]* //.*)
| (?: [ \t]* /\*.*\*/ [ \t]* )
)?"""
IDENT = r"""(?:
[a-z A-Z] [a-z A-Z 0-9 _]*
| _ [a-z A-Z 0-9 _]+
)"""
TEST_PATTERN = r"""(?x)
{SPACE}* \# {SPACE}* \[ {SPACE}* {WHAT} {SPACE}* \] {SPACE}*
(?:
(?: {SPACE}* \#\[ [^]]+ \] {OPT_COMMENT} \n )
| (?: {OPT_COMMENT} \n )
)*
.* fn {SPACE}+ ({IDENT}+)
"""
def _target_to_test(what, view, on_done):
"""Helper used to determine build target from given view."""
td = target_detect.TargetDetector(view.window())
targets = td.determine_targets(view.file_name())
if len(targets) == 0:
sublime.error_message('Error: Could not determine target to %s.' % what)
elif len(targets) == 1:
on_done(' '.join(targets[0][1]))
else:
# Can't determine a single target, let the user choose one.
display_items = [' '.join(x[1]) for x in targets]
def quick_on_done(idx):
on_done(targets[idx][1])
view.window().show_quick_panel(display_items, quick_on_done)
def _pt_to_test_name(what, pt, view):
"""Helper used to convert Sublime point to a test/bench function name."""
fn_names = []
pat = TEST_PATTERN.format(WHAT=what, **globals())
regions = view.find_all(pat, 0, r'\1', fn_names)
if not regions:
sublime.error_message('Could not find a Rust %s function.' % what)
return None
# Assuming regions are in ascending order.
indices = [i for (i, r) in enumerate(regions) if r.a <= pt]
if not indices:
sublime.error_message('No %s functions found about the current point.' % what)
return None
return fn_names[indices[-1]]
def _cargo_test_pt(what, pt, view):
"""Helper used to run a test for a given point in the given view."""
def do_test(target):
test_fn_name = _pt_to_test_name(what, pt, view)
if test_fn_name:
view.window().run_command('cargo_exec', args={
'command': what,
'settings': {
'target': target,
'extra_run_args': '--exact ' + test_fn_name
}
})
_target_to_test(what, view, do_test)
class CargoHere(sublime_plugin.WindowCommand):
"""Base class for mouse-here commands.
Subclasses set `what` attribute.
"""
what = None
def run(self, event):
view = self.window.active_view()
if not view:
return
pt = view.window_to_text((event['x'], event['y']))
_cargo_test_pt(self.what, pt, view)
def want_event(self):
return True
class CargoTestHereCommand(CargoHere):
"""Determines the test name at the current mouse position, and runs just
that test."""
what = 'test'
class CargoBenchHereCommand(CargoHere):
"""Determines the benchmark at the current mouse position, and runs just
that benchmark."""
what = 'bench'
class CargoTestAtCursorCommand(sublime_plugin.TextCommand):
"""Determines the test name at the current cursor position, and runs just
that test."""
def run(self, edit):
pt = self.view.sel()[0].begin()
_cargo_test_pt('test', pt, self.view)
class CargoCurrentFile(sublime_plugin.WindowCommand):
"""Base class for current file commands.
Subclasses set `what` attribute.
"""
what = None
def run(self):
print('current file')
def _test_file(target):
print('target is %r' % target)
self.window.run_command('cargo_exec', args={
'command': self.what,
'settings': {
'target': target
}
})
view = self.window.active_view()
_target_to_test(self.what, view, _test_file)
class CargoTestCurrentFileCommand(CargoCurrentFile):
"""Runs all tests in the current file."""
what = 'test'
class CargoBenchCurrentFileCommand(CargoCurrentFile):
"""Runs all benchmarks in the current file."""
what = 'bench'
class CargoRunCurrentFileCommand(CargoCurrentFile):
"""Runs the current file."""
what = 'run'
class CargoBenchAtCursorCommand(sublime_plugin.TextCommand):
"""Determines the benchmark name at the current cursor position, and runs
just that benchmark."""
def run(self, edit):
pt = self.view.sel()[0].begin()
_cargo_test_pt('bench', pt, self.view)
class CargoMessageHover(sublime_plugin.ViewEventListener):
"""Displays a popup if `rust_phantom_style` is "popup" when the mouse
hovers over a message region.
Limitation: If you edit the file and shift the region, the hover feature
will not recognize the new region. This means that the popup will only
show in the old location.
"""
@classmethod
def is_applicable(cls, settings):
s = settings.get('syntax')
package_name = __package__.split('.')[0]
return s == 'Packages/%s/RustEnhanced.sublime-syntax' % (package_name,)
def on_hover(self, point, hover_zone):
if util.get_setting('rust_phantom_style', 'normal') == 'popup':
messages.message_popup(self.view, point, hover_zone)