-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
package.py
827 lines (708 loc) · 28.2 KB
/
package.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# Based on Default/exec.py
import codecs, collections, html, os, re, shutil, signal, subprocess, sys, threading, time
import sublime, sublime_plugin
from typing import Any, Dict, Tuple
ns = 'sublime-executor'
RE_REPLACE_GLOB = re.compile(r"\*\*|[\*\?\.\(\)\[\]\{\}\$\^\+\|]")
# Colors
FG_ANSI = {
30: 'black',
31: 'red',
32: 'green',
33: 'brown',
34: 'blue',
35: 'magenta',
36: 'cyan',
37: 'white',
39: 'default',
90: 'light_black',
91: 'light_red',
92: 'light_green',
93: 'light_brown',
94: 'light_blue',
95: 'light_magenta',
96: 'light_cyan',
97: 'light_white'
}
BG_ANSI = {
40: 'black',
41: 'red',
42: 'green',
43: 'brown',
44: 'blue',
45: 'magenta',
46: 'cyan',
47: 'white',
49: 'default',
100: 'light_black',
101: 'light_red',
102: 'light_green',
103: 'light_brown',
104: 'light_blue',
105: 'light_magenta',
106: 'light_cyan',
107: 'light_white'
}
SCOPES = {
'red': 'redish',
'green': 'greenish',
'brown': 'orangish',
'blue': 'bluish',
'magenta': 'pinkish', # purplish
'cyan': 'cyanish',
'light_red': 'redish',
'light_green': 'greenish',
'light_brown': 'orangish',
'light_blue': 'bluish',
'light_magenta': 'pinkish',
'light_cyan': 'cyanish'
}
RE_UNKNOWN_ESCAPES = re.compile(r"\x1b[^a-zA-Z]*[a-zA-Z]")
RE_COLOR_ESCAPES = re.compile(r"\x1b\[((?:;?\d+)*)m")
RE_NOTSPACE = re.compile(r"[^\s]+")
class State:
def __init__(self):
self.proc = None
self.status = None
self.next_cmd = None
self.recents = []
self.output_view = None
self.region_id = 0
states = collections.defaultdict(lambda: State())
def get_state(window = None):
if window is None:
window = sublime.active_window()
return states[window.id()]
def glob_to_re(s):
def replace_glob(match):
s = match.group(0)
if s == "**":
return ".*"
elif s == "*":
return "[^/]*"
elif s == "?":
return "[^/]"
else:
return "\\" + s
pattern = RE_REPLACE_GLOB.sub(replace_glob, s)
if pattern[0] != "/":
pattern = "(^|/)" + pattern
if pattern[-1] != "/":
pattern = pattern + "($|/)"
return re.compile(pattern)
def find_executables_impl(acc, folder, ignores):
if os.path.exists(folder):
local_ignores = ignores.copy()
gitignore = os.path.join(folder, ".gitignore")
if os.path.exists(gitignore):
with open(gitignore, 'rt') as f:
# https://git-scm.com/docs/gitignore
for line in f.readlines():
line = line.strip()
if not line:
pass
elif line.startswith("#"):
pass
elif line.startswith("!"):
pass # TODO negates the pattern; any matching file excluded by a previous pattern will become included again
else:
local_ignores.append(glob_to_re(line))
for name in os.listdir(folder):
path = os.path.join(folder, name)
matches = [p.pattern for p in local_ignores if re.search(p, path)]
if matches:
# print("Ignoring %s because of %s" % (path, matches))
pass
elif os.path.isfile(path):
if os.access(path, os.X_OK):
acc.append(path)
# print(path)
elif os.path.isdir(path):
find_executables_impl(acc, path, local_ignores)
def find_executables(window):
results = []
for folder in window.folders():
head, tail = os.path.split(folder)
executables = []
find_executables_impl(executables, folder, [re.compile("(^|/)\\.git($|/)")])
for e in executables:
results.append({"name": e[len(head) + 1:], "cmd": "./" + os.path.basename(e), "cwd": os.path.dirname(e)})
return results
def run_command(window, cmd, args):
state = get_state(window)
if state.proc:
state.next_cmd = (cmd, args)
state.proc.kill()
else:
window.run_command(cmd, args)
def refresh_status(view):
if view:
state = get_state(view.window())
if state.status:
view.set_status(ns, state.status)
else:
view.erase_status(ns)
def set_status(s, view):
if view:
state = get_state(view.window())
state.status = s
refresh_status(view)
class ExecutorEventListener(sublime_plugin.EventListener):
def on_activated_async(self, view):
refresh_status(view)
def on_pre_close_window(self, window):
state = get_state(window)
if state.proc:
state.proc.kill()
else:
del states[window.id()]
class ArgsInputHandler(sublime_plugin.TextInputHandler):
def placeholder(self):
return 'Additional arguments'
class SelectExecutableInputHandler(sublime_plugin.ListInputHandler):
def __init__(self, window, args):
start = time.perf_counter()
self.executables = find_executables(window)
self.args = args
# print("found %i items in %f ms" % (len(self.executables), (time.perf_counter() - start) * 1000))
def placeholder(self):
return 'Select executable to run'
def list_items(self):
if self.executables:
return [(cmd["name"], cmd) for cmd in self.executables]
else:
return [("No executables found", False)]
def next_input(self, args):
return ArgsInputHandler() if self.args else None
class ProcessListener:
def on_data(self, proc, data):
pass
def on_finished(self, proc):
pass
class AsyncProcess:
"""
Encapsulates subprocess.Popen, forwarding stdout to a supplied
ProcessListener (on a separate thread)
"""
def __init__(self, cmd, shell_cmd, env, listener, path="", shell=False):
""" "path" and "shell" are options in build systems """
if not shell_cmd and not cmd:
raise ValueError("shell_cmd or cmd is required")
if shell_cmd and not isinstance(shell_cmd, str):
raise ValueError("shell_cmd must be a string")
self.shell_cmd = shell_cmd
self.listener = listener
self.killed = False
self.start_time = time.time()
# Hide the console window on Windows
startupinfo = None
if os.name == "nt":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# Set temporary PATH to locate executable in cmd
if path:
old_path = os.environ["PATH"]
# The user decides in the build system whether he wants to append
# $PATH or tuck it at the front: "$PATH;C:\\new\\path",
# "C:\\new\\path;$PATH"
os.environ["PATH"] = os.path.expandvars(path)
proc_env = os.environ.copy()
proc_env.update(env)
for k, v in proc_env.items():
proc_env[k] = os.path.expandvars(v)
if sys.platform == "win32":
preexec_fn = None
else:
preexec_fn = os.setsid
if shell_cmd:
if sys.platform == "win32":
# Use shell=True on Windows, so shell_cmd is passed through
# with the correct escaping
cmd = shell_cmd
shell = True
elif sys.platform == "darwin":
# Use a login shell on OSX, otherwise the users expected env
# vars won't be setup
cmd = ["/usr/bin/env", "bash", "-l", "-c", shell_cmd]
shell = False
elif sys.platform == "linux":
# Explicitly use /bin/bash on Linux, to keep Linux and OSX as
# similar as possible. A login shell is explicitly not used for
# linux, as it's not required
cmd = ["/usr/bin/env", "bash", "-c", shell_cmd]
shell = False
self.proc = subprocess.Popen(
cmd,
bufsize=0,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE,
startupinfo=startupinfo,
env=proc_env,
preexec_fn=preexec_fn,
shell=shell)
if path:
os.environ["PATH"] = old_path
self.stdout_thread = threading.Thread(
target=self.read_fileno,
args=(self.proc.stdout, True)
)
def start(self):
self.stdout_thread.start()
def kill(self):
if not self.killed:
print("[ Executor ] Killing " + self.shell_cmd)
self.killed = True
if sys.platform == "win32":
# terminate would not kill process opened by the shell cmd.exe,
# it will only kill cmd.exe leaving the child running
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen(
"taskkill /PID %d /T /F" % self.proc.pid,
startupinfo=startupinfo)
else:
os.killpg(self.proc.pid, signal.SIGTERM)
self.proc.terminate()
def poll(self):
return self.proc.poll() is None
def exit_code(self):
return self.proc.poll()
def read_fileno(self, file, execute_finished):
decoder = \
codecs.getincrementaldecoder(self.listener.encoding)('replace')
while True:
data = decoder.decode(file.read(2**16))
data = data.replace('\r\n', '\n').replace('\r', '\n')
if len(data) > 0 and not self.killed:
self.listener.on_data(self, data)
else:
if execute_finished:
self.listener.on_finished(self)
break
class CommandInputHandler(sublime_plugin.TextInputHandler):
def placeholder(self):
return 'Shell command to run'
class ExecutorExecuteShellCommand(sublime_plugin.WindowCommand):
def run(self, command, dir = None):
window = self.window
if dir:
dir = os.path.abspath(os.path.expandvars(os.path.expanduser(dir)))
elif len(window.folders()) > 0:
dir = window.folders()[0]
elif (view := window.active_view()) and (file := view.file_name()):
dir = os.path.dirname(file)
else:
dir = os.path.expanduser("~")
cmd = {"name": command,
"cmd": command,
"cwd": dir}
run_command(window, "executor_impl", {"select_executable": cmd, "args": []})
def input(self, args):
if "command" not in args:
return CommandInputHandler()
class ExecutorImplCommand(sublime_plugin.WindowCommand, ProcessListener):
OUTPUT_LIMIT = 2 ** 27
def __init__(self, window):
super().__init__(window)
self.errs_by_file = {}
self.annotation_sets_by_buffer = {}
self.show_errors_inline = True
def settings(self):
return sublime.load_settings("Preferences.sublime-settings")
def use_output_view(self):
settings = self.settings()
return settings.get("executor_output_view", False)
def get_output_view(self):
window = self.window
state = get_state(window)
output_view = state.output_view
use_output_view = self.use_output_view()
if output_view is None or not output_view.is_valid() or ((output_view.element() is None) != use_output_view):
if use_output_view:
active_group = window.active_group()
active_view = window.active_view()
output_view = window.new_file()
output_view.set_scratch(True)
group = window.num_groups() - 1
index = len(window.views_in_group(group))
window.set_view_index(output_view, group, index)
self.init_output_view(output_view)
window.focus_group(active_group)
window.focus_view(active_view)
else:
output_view = window.find_output_panel('exec') or window.create_output_panel('exec')
state.output_view = output_view
return output_view
def init_output_view(self, view):
state = get_state(self.window)
settings = view.settings()
settings.set("result_file_regex", self.file_regex)
settings.set("result_line_regex", self.line_regex)
settings.set("result_base_dir", self.working_dir)
settings.set("word_wrap", self.word_wrap)
settings.set("line_numbers", False)
settings.set("gutter", False)
settings.set("scroll_past_end", False)
view.assign_syntax(self.syntax)
if self.use_output_view():
view.set_name("▶️ [ RUN ] " + self.name)
else:
# Call create_output_panel a second time after assigning the above
# settings, so that it'll be picked up as a result buffer
self.window.create_output_panel("exec")
def run(self,
select_executable,
args,
file_regex="",
line_regex="",
encoding="utf-8",
env={},
quiet=False,
kill_previous=False,
update_annotations_only=False,
word_wrap=None,
syntax="Packages/Text/Plain text.tmLanguage",
# Catches "path" and "shell"
**kwargs):
if update_annotations_only:
if self.show_errors_inline:
self.update_annotations()
return
state = get_state(self.window)
state.command = self
if kill_previous and state.proc:
state.proc.kill()
name = select_executable["name"] + (" " + args if args else "")
self.name = name
shell_cmd = select_executable["cmd"] + (" " + args if args else "")
self.shell_cmd = shell_cmd
working_dir = select_executable.get("cwd")
cmd = {"name": name,
"cmd": shell_cmd,
"cwd": working_dir}
if cmd in state.recents:
state.recents.remove(cmd)
state.recents.insert(0, cmd)
settings = self.window.active_view().settings()
show_panel_on_build = settings.get("show_panel_on_build", True)
reuse_output_view = settings.get("executor_reuse_output_view", True)
self.file_regex = file_regex or settings.get("executor_file_regex", "")
self.line_regex = line_regex or settings.get("executor_line_regex", "")
self.working_dir = settings.get("executor_base_dir", working_dir)
self.word_wrap = word_wrap if word_wrap is not None else settings.get("executor_word_wrap", True)
self.syntax = syntax
# Default the to the current files directory if no working directory
# was given
if (working_dir == "" and
self.window.active_view() and
self.window.active_view().file_name()):
working_dir = os.path.dirname(self.window.active_view().file_name())
# Try not to call get_output_panel until the regexes are assigned
if not reuse_output_view:
state.output_view = None
state.output_view = self.get_output_view()
state.output_view.run_command("executor_clear_output_impl")
self.init_output_view(state.output_view)
self.encoding = encoding
self.quiet = quiet
state.proc = None
if not self.quiet:
if shell_cmd:
print("[ Executor ] Running " + shell_cmd)
elif cmd:
cmd_string = cmd
if not isinstance(cmd, str):
cmd_string = " ".join(cmd)
print("[ Executor ] Running " + cmd_string)
sublime.status_message("Building")
if show_panel_on_build:
if self.use_output_view():
group, index = self.window.get_view_index(state.output_view)
# print(f"group {group} index {index} active view {self.window.active_view_in_group(group)} output_view {state.output_view}")
if self.window.active_view_in_group(group) != state.output_view:
self.window.focus_view(state.output_view)
else:
self.window.run_command("show_panel", {"panel": "output.exec"})
self.hide_annotations()
self.show_errors_inline = settings.get("show_errors_inline", True)
merged_env = env.copy()
if self.window.active_view():
user_env = self.window.active_view().settings().get('build_env')
if user_env:
merged_env.update(user_env)
# Change to the working dir, rather than spawning the process with it,
# so that emitted working dir relative path names make sense
if working_dir != "":
os.chdir(working_dir)
self.output_size = 0
self.should_update_annotations = False
self.write("[ RUN ] \"%s\" in %s\n" % (shell_cmd, working_dir))
max_len = 50
cmd_name = cmd["name"] if len(cmd["name"]) <= max_len + 3 else cmd["name"][:max_len] + "..."
set_status("▶️ " + cmd_name, self.window.active_view())
try:
# Forward kwargs to AsyncProcess
state.proc = AsyncProcess(cmd, shell_cmd, merged_env, self, **kwargs)
state.proc.start()
except Exception as e:
self.write(str(e) + "\n")
if not self.quiet:
self.write("[ EXCEPTION ]\n")
set_status(None, window.active_view())
def write(self, characters):
if self.window.active_view().settings().get("executor_show_panel_on_output", False):
self.window.run_command("show_panel", {"panel": "output.exec"})
state = states[self.window.id()]
view = self.get_output_view()
decolorized = ""
original_pos = 0
decolorized_pos = 0
fg = "default"
bg = "default"
regions = []
def iteration(start, end, group):
nonlocal decolorized, original_pos, decolorized_pos, fg, bg, regions
text = characters[original_pos:start]
text = RE_UNKNOWN_ESCAPES.sub("", text)
decolorized += text
if len(text) > 0 and (fg != "default" or bg != "default"):
regions.append({"text": text,
"start": decolorized_pos,
"end": decolorized_pos + len(text),
"fg": fg,
"bg": bg})
digits = re.findall(r"\d+", group) or ["0"]
for digit in digits:
digit = int(digit)
if digit in FG_ANSI:
fg = FG_ANSI[digit]
if digit in BG_ANSI:
bg = BG_ANSI[digit]
if digit == 0:
fg = 'default'
bg = 'default'
original_pos = end
decolorized_pos += len(text)
for m in RE_COLOR_ESCAPES.finditer(characters):
iteration(m.start(), m.end(), m.group(1))
iteration(len(characters), len(characters), "")
insertion_point = view.size()
view.run_command('append', {'characters': decolorized, 'force': True, 'scroll_to_end': True})
for region in regions:
if scope := SCOPES.get(region['bg'], None) or SCOPES.get(region['fg'], None):
for m in RE_NOTSPACE.finditer(region['text']):
start = insertion_point + region['start'] + m.start()
end = start + len(m.group(0))
state.region_id += 1
view.add_regions(
"executor#{}".format(state.region_id),
[sublime.Region(start, end)],
'region.' + scope)
# Updating annotations is expensive, so batch it to the main thread
def annotations_check():
errs = self.get_output_view().find_all_results_with_text()
errs_by_file = {}
for file, line, column, text in errs:
if file not in errs_by_file:
errs_by_file[file] = []
errs_by_file[file].append((line, column, text))
self.errs_by_file = errs_by_file
self.update_annotations()
self.should_update_annotations = False
if not self.should_update_annotations:
if self.show_errors_inline and characters.find('\n') >= 0:
self.should_update_annotations = True
sublime.set_timeout(lambda: annotations_check())
def on_data(self, _proc, data):
# Truncate past the limit
if self.output_size >= self.OUTPUT_LIMIT:
return
self.write(data)
self.output_size += len(data)
if self.output_size >= self.OUTPUT_LIMIT:
self.write('[Output Truncated]\n')
def on_finished(self, proc):
status = None
print("[ Executor ] Finished " + proc.shell_cmd)
if proc.killed:
status = "CANCEL"
self.write("[ CANCEL ]\n")
elif not self.quiet:
elapsed = time.time() - proc.start_time
if elapsed < 1:
elapsed_str = "%.0fms" % (elapsed * 1000)
else:
elapsed_str = "%.1fs" % (elapsed)
exit_code = proc.exit_code()
if exit_code == 0 or exit_code is None:
status = "DONE"
self.write("[ DONE ] in %s\n" % elapsed_str)
else:
status = "FAIL"
self.write("[ FAIL ] with code %d in %s\n" % (exit_code, elapsed_str))
if not self.window.is_valid():
del states[self.window.id()]
else:
set_status(None, self.window.active_view())
state = get_state(self.window)
state.proc = None
if self.use_output_view():
self.get_output_view().set_name("[ %s ] %s" % (status, self.name))
if cmd := state.next_cmd:
(cmd, args) = cmd
state.next_cmd = None
self.window.run_command(cmd, args)
def update_annotations(self):
stylesheet = '''
<style>
#annotation-error {
background-color: color(var(--background) blend(#fff 95%));
}
html.dark #annotation-error {
background-color: color(var(--background) blend(#fff 95%));
}
html.light #annotation-error {
background-color: color(var(--background) blend(#000 85%));
}
a {
text-decoration: inherit;
}
</style>
'''
for file, errs in self.errs_by_file.items():
view = self.window.find_open_file(file)
if view:
selection_set = []
content_set = []
line_err_set = []
for line, column, text in errs:
pt = view.text_point(line - 1, column - 1)
if (line_err_set and
line == line_err_set[len(line_err_set) - 1][0]):
line_err_set[len(line_err_set) - 1][1] += (
"<br>" + html.escape(text, quote=False))
else:
pt_b = pt + 1
if view.classify(pt) & sublime.CLASS_WORD_START:
pt_b = view.find_by_class(
pt,
forward=True,
classes=(sublime.CLASS_WORD_END))
if pt_b <= pt:
pt_b = pt + 1
selection_set.append(
sublime.Region(pt, pt_b))
line_err_set.append(
[line, html.escape(text, quote=False)])
for text in line_err_set:
content_set.append(
'<body>' + stylesheet +
'<div class="error" id=annotation-error>' +
'<span class="content">' + text[1] + '</span></div>' +
'</body>')
view.add_regions(
"exec",
selection_set,
scope="invalid",
annotations=content_set,
flags=(sublime.DRAW_SQUIGGLY_UNDERLINE |
sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE),
on_close=self.hide_annotations)
def hide_annotations(self):
for window in sublime.windows():
for file, errs in self.errs_by_file.items():
view = window.find_open_file(file)
if view:
view.erase_regions("exec")
view.hide_popup()
view = sublime.active_window().active_view()
if view:
view.erase_regions("exec")
view.hide_popup()
self.errs_by_file = {}
self.annotation_sets_by_buffer = {}
self.show_errors_inline = False
class ExecutorExecuteWithArgsCommand(sublime_plugin.WindowCommand):
def run(self, select_executable, args):
if select_executable:
run_command(self.window, "executor_impl", {"select_executable": select_executable, "args": args})
def input(self, args):
return SelectExecutableInputHandler(self.window, True)
class ExecutorExecuteCommand(sublime_plugin.WindowCommand):
def run(self, select_executable):
if select_executable:
run_command(self.window, "executor_impl", {"select_executable": select_executable, "args": ""})
def input(self, args):
return SelectExecutableInputHandler(self.window, False)
class SelectRecentInputHandler(sublime_plugin.ListInputHandler):
def placeholder(self):
return 'Select executable to run'
def list_items(self):
state = get_state()
return [(cmd["name"], cmd) for cmd in state.recents]
class ExecutorRepeatRecentCommand(sublime_plugin.WindowCommand):
def run(self, select_recent):
run_command(self.window, "executor_impl", {"select_executable": select_recent, "args": ""})
def input(self, args):
return SelectRecentInputHandler()
def is_enabled(self):
state = get_state()
return bool(state.recents)
class ExecutorRepeatLastCommand(sublime_plugin.WindowCommand):
def run(self):
state = get_state(self.window)
run_command(self.window, "executor_impl", {"select_executable": state.recents[0], "args": ""})
def is_enabled(self):
state = get_state(self.window)
return len(state.recents) >= 1
class ExecutorCancelCommand(sublime_plugin.WindowCommand):
def run(self):
state = get_state(self.window)
if state.proc:
state.proc.kill()
def is_enabled(self):
state = get_state(self.window)
return state.proc != None
class ExecutorClearOutputImplCommand(sublime_plugin.TextCommand):
def run(self, edit):
state = get_state(self.view.window())
state.command.hide_annotations()
self.view.erase(edit, sublime.Region(0, self.view.size()))
class ExecutorClearOutputCommand(sublime_plugin.WindowCommand):
def run(self):
state = get_state(self.window)
state.output_view.run_command("executor_clear_output_impl")
def is_enabled(self):
state = get_state(self.window)
return bool(state.output_view)
class ExecutorToggleBottomGroupCommand(sublime_plugin.WindowCommand):
def run(self, visible = None):
window = self.window
layout = window.layout()
cols = len(layout['cols'])
rows = len(layout['rows'])
is_visible = rows > 2 and layout['cells'][-1] == [0, rows - 2, cols - 1, rows - 1]
active_group = window.active_group()
active_view = window.active_view()
if is_visible and visible != True:
# hide
coeff = layout['rows'][-2]
new_rows = [(row / coeff) for row in (layout['rows'][:-1])]
new_cells = layout['cells'][:-1]
window.set_layout({'cells': new_cells, 'rows': new_rows, 'cols': layout['cols']})
window.focus_group(active_group)
window.focus_view(active_view)
elif not is_visible and visible != False:
# show
settings = sublime.load_settings("Preferences.sublime-settings")
coeff = 1.0 - settings.get('executor_bottom_group_ratio', 0.33)
new_rows = [row * coeff for row in layout['rows']] + [1.0]
new_cells = layout['cells'] + [[0, rows - 1, cols - 1, rows]]
window.set_layout({'cells': new_cells, 'rows': new_rows, 'cols': layout['cols']})
window.focus_group(active_group)
window.focus_view(active_view)
def plugin_unloaded():
for state in states.values():
if state.proc:
state.proc.kill()