-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstellation.py
302 lines (213 loc) · 9.56 KB
/
constellation.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
"""
Collect ST3 projects/workspaces into "Constellations"
When you open or close a constellation, it opens or closes all of the included projects.
"""
import sublime
import sublime_plugin
import os
import subprocess
import json
from .util import input_handlers as collect
from .util.api import API
from .util import constants as c
def plugin_loaded():
API.load_state()
def plugin_unloaded():
API.save_state()
class _BaseApplicationCommand(sublime_plugin.ApplicationCommand, API):
pass
# TODO: I won't pretend to completely grok when and why ST3's api uses the various arg-passing conventions it's using. May be worth a refactor when this is better understood.
class OpenConstellationsInfoCommand(_BaseApplicationCommand):
def is_enabled(self, *args):
return False
def description(self, *args):
return "Open Constellations: {:}/{:}".format(
len(self._open_constellations), len(self.active_constellations())
)
# TODO: can remove below when a pure python or Windows-safe fallback for 'find' is implemented
class WindowsUserQuestionCommand(_BaseApplicationCommand):
def is_enabled(self, *args):
return False
def description(self, *args):
return "Help wanted (see github) on these:"
class OpenConstellationsCommand(_BaseApplicationCommand):
def is_visible(self, index=None, **args):
return index < len(self._open_constellations)
def is_enabled(self, index=None, **args):
return True
def description(self, index=None, **args):
const = relevant = None
if index < len(self._open_constellations):
relevant = self.open_constellations()
const = relevant[index]
return (
" {} [{}]".format(
const, "∗" * len(self.constellations[const]["projects"])
)
if index < len(relevant)
else "Oops. You shouldn't be seeing this. Better find someone who works here."
)
def run(self, index=None, **args):
if index < len(self._open_constellations):
relevant = self.open_constellations()
const = relevant[index]
return CloseConstellationCommand().run(const)
class _ExistingConstellationCommand(_BaseApplicationCommand):
_list_class = collect.SelectConstellationList
def input(self, constellation):
return self._list_class()
def is_enabled(self, constellation=None, **kwargs):
return True if len(self._list_class._generator(self)) else False
class _OpenConstellationCommand(_ExistingConstellationCommand):
_list_class = collect.SelectOpenConstellationList
class _ClosedConstellationCommand(_ExistingConstellationCommand):
_list_class = collect.SelectClosedConstellationList
class _ActiveConstellationCommand(_ExistingConstellationCommand):
_list_class = collect.SelectActiveConstellationList
class ManageConstellationsInfoCommand(_BaseApplicationCommand):
def is_enabled(self, *args):
return False
def description(self, *args):
return "Constellations"
class CreateConstellationCommand(_BaseApplicationCommand):
def input(self, args):
return collect.InputConstellationName()
def run(self, name):
self.add_constellation(name)
self.open_constellation(name)
class CreateConstellationFromOpenProjectCommand(_BaseApplicationCommand):
def input(self, args):
return collect.OpenProjectList(exclude=self.projects_in_constellations())
def run(self, project):
name = os.path.splitext(os.path.basename(project))[0]
self.add_constellation(name)
self.open_constellation(name)
# add project to eponymous constellation
self.add_to(name, project, already_open=True)
class CreateConstellationFromProjectFileCommand(_BaseApplicationCommand):
def input(self, args):
return collect.SearchProjectList()
def run(self, project):
name = os.path.splitext(os.path.basename(project))[0]
self.add_constellation(name)
self.open_constellation(name)
# add project to eponymous constellation
self.add_to(name, project)
class CreateConstellationFromWorkspaceFileCommand(_BaseApplicationCommand):
def input(self, args):
return collect.UpgradeWorkspaceList()
def run(self, project):
name = os.path.splitext(os.path.basename(project))[0]
self.add_constellation(name)
self.open_constellation(name)
# add project to eponymous constellation
self.add_to(name, project)
class DestroyConstellationCommand(_ActiveConstellationCommand):
def run(self, constellation):
if constellation not in self.constellations:
return
if constellation in self._open_constellations:
self.close_constellation(constellation)
self.remove_constellation(constellation)
class RenameConstellationCommand(_ActiveConstellationCommand):
def input(self, args):
return collect.RenameList()
def run(self, constellation, new_name):
if constellation not in self.constellations:
return
self.rename_constellation(constellation, new_name)
class OpenConstellationCommand(_ClosedConstellationCommand):
def run(self, constellation):
self.open_constellation(constellation)
class CloseConstellationCommand(_OpenConstellationCommand):
def run(self, constellation):
if constellation not in self._open_constellations:
return
for project in self.projects_for(constellation):
for window in sublime.windows():
if window.project_file_name() == project:
window.run_command("close_workspace")
if window.id() != sublime.active_window().id():
window.run_command("close_window")
self.close_constellation(constellation)
class ManageProjectsInfoCommand(_BaseApplicationCommand):
def is_enabled(self, *args):
return False
def description(self, *args):
return "Constellation Projects"
class AddProjectCommand(_ActiveConstellationCommand):
already_open = True
def input(self, args):
return collect.ProjectList()
def run(self, constellation, project):
self.add_to(constellation, project, already_open=self.already_open)
# We have opinions, and one of those is that workspaces are annoying to work with directly. Because of this opinion, the only way we're going to support working with them is by explicitly upgrading it to a project (but then replacing the project file with a link back to whatever project the workspace was in) so you get the benefits of having a workspace, but we don't have to have arcane methods of working with them.
class UpgradeWorkspaceCommand(AddProjectCommand):
already_open = False
def is_enabled(self, *args):
# TODO: remove below when there's a fallback
if sublime.platform() == "windows":
return False
search_root = self.search_path
return (
True
if search_root and len(search_root) and os.path.exists(search_root)
else False
)
def input(self, args):
return collect.FoundWorkspaceList()
def run(self, constellation, workspace_path):
if not workspace_path:
# print(c.LOG_TEMPLATE, "Nothing found to upgrade")
return
workspace = None
# load the workspace
with open(workspace_path, "r") as infile:
workspace = json.load(infile)
project = os.path.join(os.path.dirname(workspace_path), workspace["project"])
workspace_project = workspace_path.replace(
".sublime-workspace", ".sublime-project"
)
if len(workspace["project"]) and os.path.exists(project):
# this seems to be set to a real value, so we'll make a link to the sublime-project
subprocess.Popen(
"ln -f '{:}' '{:}'".format(project, workspace_project), shell=True
)
elif not os.path.exists(workspace_project):
# some projects might have a null value, or maybe the file got deleted, so we'll just make an empty project
with open(workspace_project, "w") as outfile:
json.dump({}, outfile, indent=1)
else:
pass
# print(
# c.LOG_TEMPLATE,
# "I really hope we never get here",
# workspace,
# workspace_project,
# workspace_path,
# project,
# )
workspace["project"] = workspace_project
with open(workspace_path, "w") as outfile:
json.dump(workspace, outfile, indent=1)
super().run(constellation, workspace_project)
# at this point, we need to have taken the workspace file, added a link adjacent to the project file, and replaced the project key in the workspace file with the pointer to the adjacent file
class FindProjectCommand(AddProjectCommand):
already_open = False
def is_enabled(self, *args):
# TODO: remove below when there's a fallback
if sublime.platform() == "windows":
return False
search_root = self.search_path
return (
True
if search_root and len(search_root) and os.path.exists(search_root)
else False
)
def input(self, args):
return collect.FoundProjectList()
class RemoveProjectCommand(_ActiveConstellationCommand):
def input(self, args):
return collect.ConstellationProjectList()
def run(self, constellation, project):
self.remove_from(constellation, project)