-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolRunner.py
238 lines (175 loc) · 7.68 KB
/
ToolRunner.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
from functools import partial
import sublime
import sublime_plugin
from .lib import debug, manager, settings, util
from .lib.command import Command
class ToolRunner(sublime_plugin.WindowCommand):
def run(self, tool=None, group=None, profile=None, default_profile=False, **kwargs):
command = Command(self.window, kwargs)
if tool is not None:
command.run_tool(tool)
elif group is not None:
if default_profile:
profile = settings.get_setting("default_profiles").get(group)
if profile is not None:
command.run_profile(group, profile)
else:
self._ask_profile_and_run_command(
group, partial(self._on_ask_profile_done, command)
)
else:
self._ask_type_to_run(partial(self._on_ask_type_done, command))
def _ask_type_to_run(self, callback):
self.window.show_quick_panel(["Tool", "Group"], callback, 0, 0, None)
def _on_ask_type_done(self, command, selected_index):
if selected_index == 0:
sublime.set_timeout(
partial(
self._ask_tool_to_run, partial(self._on_ask_tool_done, command)
),
0,
)
elif selected_index == 1:
sublime.set_timeout(
partial(
self._ask_group_and_profile_to_run,
partial(self._on_ask_group_done, command),
),
0,
)
def _ask_tool_to_run(self, callback):
tool_list = []
tool_selection_list = []
def_tool_list = settings.get_tools()
if len(def_tool_list) <= 0:
sublime.error_message("There are no tools configured")
return
debug.log("Creating Tools item list for Quick Panel", def_tool_list)
for single_tool in def_tool_list:
debug.log("Appending ", single_tool)
tool_name = single_tool.get("name", single_tool.get("cmd"))
tool_list.append(tool_name)
desc = single_tool.get("desc")
if desc is not None:
tool_selection_list.append(desc + " (" + tool_name + ")")
else:
tool_selection_list.append(tool_name)
callback = partial(callback, tool_list)
self.window.show_quick_panel(tool_selection_list, callback, 0, 0, None)
def _on_ask_tool_done(self, command, tool_list, selected_index):
tool_selected = tool_list[selected_index]
if selected_index > -1:
command.run_tool(tool_selected)
def _ask_group_and_profile_to_run(self, callback):
group_list = [single_group["name"] for single_group in settings.get_groups()]
if len(group_list) <= 0:
sublime.error_message("There are no groups configured")
else:
callback = partial(callback, group_list)
self.window.show_quick_panel(group_list, callback, 0, 0, None)
def _on_ask_group_done(self, command, group_list, selected_index):
group_selected = group_list[selected_index]
if selected_index >= 0:
callback = partial(self._on_ask_profile_done, command)
sublime.set_timeout(
partial(self._ask_profile_and_run_command, group_selected, callback), 0
)
def _ask_profile_and_run_command(self, group_selected, callback):
profiles = settings.get_profiles(group_selected)
if len(profiles) <= 0:
sublime.error_message("This group has no profiles configured")
return
profile_list = [profile["name"] for profile in profiles]
self.window.show_quick_panel(
profile_list, partial(callback, group_selected, profile_list), 0, 0, None
)
def _on_ask_profile_done(
self, command, group_selected, profile_list, selected_index
):
if selected_index >= 0:
selected_profile = profile_list[selected_index]
command.run_profile(group_selected, selected_profile)
class ToolRunnerCancelCurrent(sublime_plugin.WindowCommand):
def run(self):
manager.cancel_command_for_view_id(self.window.active_view().id())
class ToolRunnerFocusOutput(sublime_plugin.WindowCommand):
def run(self):
source_view = self.window.active_view()
target_view = manager.get_target_view_for_source_view(source_view)
if target_view is not None:
manager.ensure_visible_view(target_view, focus=True)
else:
util.notify("This view don't have an output")
class ToolRunnerFocusSource(sublime_plugin.WindowCommand):
def run(self):
target_view = self.window.active_view()
source_view = manager.get_source_view_for_target_view(target_view)
if source_view is not None:
manager.ensure_visible_view(source_view, focus=True)
else:
util.notify("This view is not an output")
class ToolRunnerSwitchDefaultProfile(sublime_plugin.WindowCommand):
def run(self, profile_group=None):
debug.log("Switching command for profile group: " + str(profile_group))
if profile_group is None:
self.ask_group_and_switch_profile()
else:
self.switch_profile(profile_group)
def ask_group_and_switch_profile(self):
self.groups = [group["name"] for group in settings.get_groups()]
if len(self.groups) <= 0:
sublime.error_message("There are no groups configured")
return
self.window.show_quick_panel(
self.groups,
partial(self.on_ask_group_done, self.switch_profile),
0,
0,
None,
)
def on_ask_group_done(self, callback, selected_index):
if selected_index < 0:
return
group_selected = self.groups[selected_index]
if selected_index > -1:
sublime.set_timeout(partial(callback, group_selected), 0)
def switch_profile(self, profile_group):
profiles = settings.get_profiles(profile_group)
self.profile_group = profile_group
self.profile_list = [profile["name"] for profile in profiles]
self.window.show_quick_panel(self.profile_list, self.on_ask_profile, 0, 0, None)
def on_ask_profile(self, selected_index):
if selected_index > -1:
selected_profile_name = self.profile_list[selected_index]
current_settings = settings.get_setting("default_profiles", {})
current_settings[self.profile_group] = selected_profile_name
settings.set_setting("default_profiles", current_settings)
self.profile_list = None
self.groups = None
class ToolRunnerOpenSettings(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
sublime_plugin.WindowCommand.__init__(self, *args, **kwargs)
def run(self, scope=None):
settings.open_settings(self.window, scope)
class ToolRunnerListener(sublime_plugin.EventListener):
def on_close(self, view):
manager.remove_source_view(view)
manager.remove_target_view(view)
def on_post_save(self, view):
# debug.log("Saved view: %s" % view.id())
source_view = manager.get_source_view_for_target_view(view)
if source_view is None:
# debug.log("The view %s is not an output view" % view.id())
return
manager.remove_target_view(view)
view.set_scratch(False)
view.set_read_only(False)
def plugin_loaded():
debug.log("Plugin Loading")
settings.on_loaded()
debug.log("Plugin Loaded")
if settings.get_setting("devel"):
debug.forget_modules()
def plugin_unloaded():
settings.on_unloaded()
debug.log("Plugin Unloaded")