-
Notifications
You must be signed in to change notification settings - Fork 10
/
sublimesbt.py
380 lines (250 loc) · 9.69 KB
/
sublimesbt.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
import sublime_plugin
import sublime
import re
try:
from .project import Project
from .sbtrunner import SbtRunner
from .sbtview import SbtView
from .errorview import ErrorView
from .outputmon import BuildOutputMonitor
from .util import delayed, maybe
except(ValueError):
from project import Project
from sbtrunner import SbtRunner
from sbtview import SbtView
from errorview import ErrorView
from outputmon import BuildOutputMonitor
from util import delayed, maybe
class SbtWindowCommand(sublime_plugin.WindowCommand):
def __init__(self, *args):
super(SbtWindowCommand, self).__init__(*args)
self._project = Project(self.window)
self._runner = SbtRunner(self.window)
self._sbt_view = SbtView(self.window)
self._error_view = ErrorView(self.window)
self._error_reporter = self._project.error_reporter
self._error_report = self._project.error_report
self._monitor_compile_output = BuildOutputMonitor(self._project)
def is_sbt_project(self):
return self._project.is_sbt_project()
def is_play_project(self):
return self._project.is_play_project()
def is_sbt_running(self):
return self._runner.is_sbt_running()
def start_sbt(self, command=None):
self._runner.start_sbt(command,
on_start=self._sbt_view.start,
on_stop=self._sbt_view.finish,
on_stdout=self._on_stdout,
on_stderr=self._on_stderr)
def stop_sbt(self):
self._runner.stop_sbt()
def kill_sbt(self):
self._runner.kill_sbt()
def show_sbt(self):
self._sbt_view.show()
def hide_sbt(self):
self._sbt_view.hide()
def focus_sbt(self):
self._sbt_view.focus()
def take_input(self):
return self._sbt_view.take_input()
def send_to_sbt(self, cmd):
self.window.run_command('clear_sbt_errors')
self._runner.send_to_sbt(cmd)
@delayed(0)
def show_error(self, error):
self._error_report.focus_error(error)
self._error_reporter.show_errors()
self._error_view.show_error(error)
self.goto_error(error)
@delayed(0)
def goto_error(self, error):
self.window.open_file(error.encoded_position(), sublime.ENCODED_POSITION)
def show_error_output(self):
self._error_view.show()
def setting(self, name):
return self._project.setting(name)
def _on_stdout(self, output):
self._monitor_compile_output(output)
self._show_output(output)
def _on_stderr(self, output):
self._show_output(output)
@delayed(0)
def _show_output(self, output):
output = self._work_around_JLine_bug(output)
self._sbt_view.show_output(output)
# If we have a single character, space, CR then we are probably seeing a
# JLine bug which has inserted the space, CR at column 80 of a prompt
# line. Delete the space and CR so that it doesn't mess up the display
# of the prompt line (ie. hide the stuff before the CR). Just remove
# the space, CR pair.
def _work_around_JLine_bug(self, output):
return re.sub(r'^(.) \r$', r'\1', output)
class StartSbtCommand(SbtWindowCommand):
def run(self):
self.start_sbt()
def is_enabled(self):
return self.is_sbt_project() and not self.is_sbt_running()
class StopSbtCommand(SbtWindowCommand):
def run(self):
self.stop_sbt()
def is_enabled(self):
return self.is_sbt_running()
class KillSbtCommand(SbtWindowCommand):
def run(self):
self.kill_sbt()
def is_enabled(self):
return self.is_sbt_running()
class ShowSbtCommand(SbtWindowCommand):
def run(self):
self.show_sbt()
self.focus_sbt()
def is_enabled(self):
return self.is_sbt_project()
class SbtSubmitCommand(SbtWindowCommand):
def run(self):
self.send_to_sbt(self.take_input() + '\n')
def is_enabled(self):
return self.is_sbt_running()
class SbtCommand(SbtWindowCommand):
def run(self, command):
if self.is_sbt_running():
self.send_to_sbt(command + '\n')
else:
self.start_sbt(command)
def is_enabled(self):
return self.is_sbt_project()
class SbtTestCommand(SbtCommand):
def run(self):
super(SbtTestCommand, self).run(self.test_command())
def test_command(self):
return self.setting('test_command')
class SbtContinuousTestCommand(SbtTestCommand):
def test_command(self):
return '~ ' + super(SbtContinuousTestCommand, self).test_command()
test_only_arg = '*'
class SbtTestOnlyCommand(SbtCommand):
base_command = 'test-only'
def run(self):
self.window.show_input_panel(self.prompt(), test_only_arg,
self.test_only, None, None)
def test_only(self, arg):
global test_only_arg
test_only_arg = arg
super(SbtTestOnlyCommand, self).run(self.test_command(arg))
def prompt(self):
return 'SBT: %s' % self.base_command
def test_command(self, arg):
return '%s %s' % (self.base_command, arg)
class SbtContinuousTestOnlyCommand(SbtTestOnlyCommand):
def test_command(self, arg):
return '~ ' + super(SbtContinuousTestOnlyCommand, self).test_command(arg)
class SbtTestQuickCommand(SbtTestOnlyCommand):
base_command = 'test-quick'
class SbtContinuousTestQuickCommand(SbtTestQuickCommand):
def test_command(self, arg):
return '~ ' + super(SbtContinuousTestQuickCommand, self).test_command(arg)
class SbtRunCommand(SbtCommand):
def run(self):
super(SbtRunCommand, self).run(self.setting('run_command'))
class SbtReloadCommand(SbtCommand):
def run(self):
super(SbtReloadCommand, self).run('reload')
def is_enabled(self):
return self.is_sbt_running()
class SbtErrorsCommand(SbtWindowCommand):
def is_enabled(self):
return self.is_sbt_project() and self._error_report.has_errors()
class ClearSbtErrorsCommand(SbtErrorsCommand):
def run(self):
self._error_reporter.clear()
self._error_view.clear()
class ListSbtErrorsCommand(SbtErrorsCommand):
def run(self):
errors = list(self._error_report.all_errors())
list_items = [e.list_item() for e in errors]
def goto_error(index):
if index >= 0:
self.show_error(errors[index])
self.window.show_quick_panel(list_items, goto_error)
class NextSbtErrorCommand(SbtErrorsCommand):
def run(self):
self.show_error(self._error_report.next_error())
class ShowSbtErrorOutputCommand(SbtErrorsCommand):
def run(self):
self.show_error_output()
class SbtEotCommand(SbtWindowCommand):
def run(self):
if sublime.platform() == 'windows':
self.send_to_sbt('\032')
else:
self.send_to_sbt('\004')
def is_enabled(self):
return self.is_sbt_running()
class SbtDeleteLeftCommand(SbtWindowCommand):
def run(self):
self._sbt_view.delete_left()
class SbtDeleteBolCommand(SbtWindowCommand):
def run(self):
self._sbt_view.delete_bol()
class SbtDeleteWordLeftCommand(SbtWindowCommand):
def run(self):
self._sbt_view.delete_word_left()
class SbtDeleteWordRightCommand(SbtWindowCommand):
def run(self):
self._sbt_view.delete_word_right()
class SbtShowHistoryCommand(SbtWindowCommand):
def run(self, editable=False):
def get_command(index):
if index >= 0:
cmd = self._runner.get_history()[index]
if editable:
self.window.show_input_panel("Command:", cmd,
self.run_command,
None, None)
else:
self.run_command(cmd)
history = self._runner.get_history()
if history == []:
sublime.error_message('There is no SBT command history to display.')
else:
self.window.show_quick_panel(history, get_command)
def run_command(self, cmd):
self.send_to_sbt (cmd + '\n')
def is_enabled(self):
return self.is_sbt_running()
class SbtClearHistoryCommand(SbtWindowCommand):
def run(self):
self._runner.clear_history()
class SbtListener(sublime_plugin.EventListener):
def on_clone(self, view):
for reporter in maybe(self._reporter(view)):
reporter.show_errors_in(view.file_name())
def on_load(self, view):
for reporter in maybe(self._reporter(view)):
reporter.show_errors_in(view.file_name())
def on_post_save(self, view):
for reporter in maybe(self._reporter(view)):
reporter.hide_errors_in(view.file_name())
def on_modified(self, view):
for reporter in maybe(self._reporter(view)):
reporter.show_errors_in(view.file_name())
def on_selection_modified(self, view):
if SbtView.is_sbt_view(view):
SbtView(view.window()).update_writability()
else:
for reporter in maybe(self._reporter(view)):
reporter.update_status_now()
def on_activated(self, view):
for reporter in maybe(self._reporter(view)):
reporter.show_errors_in(view.file_name())
def on_query_context(self, view, key, operator, operand, match_all):
if key == "in_sbt_view":
if SbtView.is_sbt_view(view):
return SbtRunner.is_sbt_running_for(view.window())
else:
return False
def _reporter(self, view):
for window in maybe(view.window()):
return Project(window).error_reporter