-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCeedlingModuleOperations.py
More file actions
115 lines (92 loc) · 3.54 KB
/
CeedlingModuleOperations.py
File metadata and controls
115 lines (92 loc) · 3.54 KB
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
import functools
import sublime
import sublime_plugin
from .CeedlingOpenFile import CeedlingPathBuilder
from .CeedlingSettings import CeedlingProjectSettings
class CeedlingCreateModuleCommand(sublime_plugin.WindowCommand):
def run(self, action="create"):
window = self.window
view = self.window.active_view()
if action == "create":
desc = "Module"
elif action == "stub":
desc = "Stub"
else:
return
window.show_input_panel(
"Enter new {} name".format(desc),
"",
functools.partial(self.on_done, view, action),
None,
None,
)
def on_done(self, view, module, text):
"""Handler for onDone event."""
window = view.window()
window.status_message("Creating {}: {}".format(module, text))
window.run_command(
"ceedling_exec", {"tasks": ["module:{}[{}]".format(module, text)]}
)
window.status_message("Created {}: {}".format(module, text))
# Windows/Linux need folder listing refresh
sublime.set_timeout_async(
lambda: window.run_command("refresh_folder_list"), 2000
)
class CeedlingDestroyModuleCommand(sublime_plugin.WindowCommand):
def run(self, **kwargs):
variables = self.window.extract_variables()
if variables.get("file_name") is None:
sublime.error_message("Nothing to destroy.")
return None
try:
self.conf = CeedlingProjectSettings(self.window)
self.pathbuilder = CeedlingPathBuilder(self.conf)
except OSError as e:
self.window.status_message("Ceedling: %s" % e)
return
base_name = self.pathbuilder.split_name(
variables.get("file_name", "")
).get("base")
if base_name is None:
sublime.error_message("Cannot destroy selected file.")
return None
if sublime.ok_cancel_dialog(
"Remove all test and source files for {}?".format(base_name),
ok_title="Destroy",
):
self._destroy(base_name)
else:
return None
def _destroy(self, module_name):
# Close all target module views without saving
for i in ("test", "source", "include"):
f = self.pathbuilder.build_path(i, module_name)
v = self.window.find_open_file(f)
if v is not None:
viewlist = self._find_clones(v)
viewlist.append(v)
for vi in viewlist:
vi.set_scratch(True)
vi.close()
self.window.run_command(
"ceedling_exec",
{"tasks": ["module:destroy[{}]".format(module_name)]},
)
# Windows/Linux need folder listing refresh
sublime.set_timeout_async(
lambda: self.window.run_command("refresh_folder_list"), 2000
)
def _find_clones(self, view):
"""Return view id of cloned windows."""
if int(sublime.version()) >= 4080:
return view.clones()
else:
clones = []
for window in sublime.windows():
for _view in window.views():
if (
_view.buffer_id() == view.buffer_id()
and view.id() != _view.id()
):
clones.append(_view)
return clones