-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.py
187 lines (149 loc) · 5.48 KB
/
plugin.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
import os
from pathlib import Path
import sublime
import sublime_plugin
_history = []
class WindowInputHandler(sublime_plugin.ListInputHandler):
def name(self):
return "window_id"
def placeholder(self):
return "Choose a window"
def list_items(self):
def active_file(window):
view = window.active_view()
if not view:
return (None, None)
try:
file = Path(view.file_name())
return (file.parent, file.name)
except:
return (None, view.name())
def active_folder(folders, active_file_path):
if active_file_path:
for folder in folders:
try:
folder = Path(folder)
active_file_path.relative_to(folder)
return folder
except ValueError:
continue
return Path(folders[0])
def transform_folder(folder):
try:
home = "USERPROFILE" if os.name == "nt" else "HOME"
return f"~{os.sep}{folder.relative_to(os.getenv(home, ''))}"
except ValueError:
return folder
def create_item(window):
active_file_path, active_file_name = active_file(window)
folders = window.folders()
workspace = window.workspace_file_name()
if workspace:
title = Path(workspace).stem
kind = [sublime.KIND_ID_NAMESPACE, "P", "Project"]
if folders:
second_line = transform_folder(
active_folder(folders, active_file_path)
)
else:
second_line = "No folders in project yet!"
elif folders:
folder = active_folder(folders, active_file_path)
title = folder.name
kind = [sublime.KIND_ID_NAVIGATION, "F", "Folder"]
second_line = transform_folder(folder)
elif active_file_path:
title = active_file_name
kind = [sublime.KIND_ID_NAVIGATION, "f", "File"]
second_line = transform_folder(active_file_path)
else:
title = active_file_name
kind = [sublime.KIND_ID_AMBIGUOUS, "S", "Scratch"]
second_line = "Scratch Window"
return sublime.ListInputItem(
text=title or "untitled",
value=window.id(),
annotation=f"Window {window.id()}",
kind=kind,
details=[f"<i>{second_line}</i>"],
)
# add current window to the end of the selection list
return [create_item(window) for window in _history[1:]] + [create_item(_history[0])]
class CloseOtherWindowsCommand(sublime_plugin.ApplicationCommand):
def is_enabled(self):
return len(sublime.windows()) > 1
def run(self):
"""
Execute `close_other_windows` command
"""
window_id = sublime.active_window().id()
for window in sublime.windows():
if window.id() != window_id:
window.run_command("close_window")
class PromptCloseWindowCommand(sublime_plugin.ApplicationCommand):
def input_description(self):
return "Close Window"
def input(self, args):
if "window_id" not in args:
return WindowInputHandler()
return None
def run(self, window_id):
"""
Execute `prompt_close_window` command
:param window_id:
The window identifier of the window to close.
"""
for window in sublime.windows():
if window.id() == window_id:
window.run_command("close_window")
break
class SwitchWindowCommand(sublime_plugin.ApplicationCommand):
def input_description(self):
return "Switch Window"
def input(self, args):
if 'previous_window' not in args and args.get("window_id") is None:
return WindowInputHandler()
return None
def run(self, previous_window=False, window_id=None):
"""
Execute `switch_window` command
:param previous_window:
If ``true`` directly switch to previous window.
:param window_id:
The window identifier of the window to switch to.
"""
if previous_window:
if len(_history) >= 2:
_history[1].bring_to_front()
return
for window in sublime.windows():
if window.id() == window_id:
window.bring_to_front()
break
class SwitchWindowListener(sublime_plugin.EventListener):
def on_activated(self, view):
global _history
window = view.window()
if not window:
return
windows = sublime.windows()
# add missing windows to stack
for w in windows:
if w not in _history:
_history.append(w)
# remove closed windows from stack
for w in _history:
if w not in windows:
_history.remove(w)
# abort on empty stack
if not _history:
return
# abort if active window is already on top
if window == _history[0]:
return
# move active window to top
try:
_history.remove(window)
except:
pass
_history.insert(0, window)