-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFile Navigator.py
431 lines (323 loc) · 11.5 KB
/
File Navigator.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
import sublime, sublime_plugin
import time, os.path, shutil, subprocess
try:
from Default.send2trash import send2trash
from .file_navigator.tools import history_items, list_items, show_input_panel, show_quick_panel
except (ImportError, ValueError):
from file_navigator.tools import history_items, list_items, show_input_panel, show_quick_panel
import sys
package_dir = sublime.packages_path() + os.sep + "Default"
if package_dir not in sys.path:
sys.path += [package_dir]
from send2trash import send2trash
CHOOSE_ROOT = 0
NAVIGATOR = 1
NAVIGATOR_PASTE = 2
DO_FILE = 3
DO_DIR = 4
class FileNavigatorListener(sublime_plugin.EventListener):
def on_activated(self, view):
if FileNavigatorCommand.active and view.window() and view.window().id() != FileNavigatorCommand.window_id:
FileNavigatorCommand.reset()
if FileNavigatorCommand.active and FileNavigatorCommand.view_id != view.id():
FileNavigatorCommand.view_id = view.id()
def on_query_context(self, view, key, operator, operand, match_all):
# Check if file navigator is active
if FileNavigatorCommand.active and FileNavigatorCommand.view_id == view.id():
if key in ["file_navigator_do_directory", "file_navigator_toggel_hidden_files"]:
return True
class FileNavigatorCommand(sublime_plugin.WindowCommand):
active = False
cwd = None
view_id = None
window_id = None
navigator_status = None
block_do_directory = False
keep_settings = False
show_hidden_files = False
@classmethod
def reset(self):
self.active = False
cwd = None
view_id = None
window_id = None
def run(self, path = None, do_dir = None):
# Hide overlay before the next run
self.window.run_command("hide_overlay")
self.cls = FileNavigatorCommand
self.cls.active = True
self.cls.window_id = self.window.id()
self.item_buffer = []
if not path:
self.choose_root()
elif do_dir:
self.do_dictionary(path)
else:
self.navigator(path)
def choose_root(self, roots = None):
# Set navigator status
self.cls.navigator_status = CHOOSE_ROOT
# Find roots
roots = roots if roots else self.find_root()
# Sort roots
roots = sorted(roots)
# Find command prefix
prefix = os.path.commonprefix(roots).rpartition(os.sep)[0]
items = [[item[len(prefix)+1:], os.path.dirname(item)] for item in roots]
# items = [[os.path.basename(item), os.path.dirname(item)] for item in roots]
def on_done(i):
if i < 0:
self.cls.reset()
else:
self.navigator(roots[i])
if not items:
sublime.status_message("No Root Elements")
elif len(items) == 1:
on_done(0)
else:
show_quick_panel(self.window, items, on_done)
def find_root(self):
items = []
# Search for root of the different views
for view in self.window.views():
if view.file_name():
item = os.path.dirname(view.file_name())
if os.path.isdir(item) and item not in items:
items += [item]
# Search for the root for the window
for item in self.window.folders():
if item and os.path.isdir(item) and item not in items:
items += [item]
return items
def navigator(self, path):
items = [{"name": ".."}]
if self.item_buffer:
items += [{"name": "Paste"}]
if len(self.item_buffer) == 1:
items += [{"name": "Paste As ..."}]
# List items in folder
items += list_items(path, len(self.item_buffer) > 0, self.cls.show_hidden_files)
# block_do_directory on paste
if self.item_buffer:
self.cls.navigator_status = NAVIGATOR_PASTE
else:
self.cls.navigator_status = NAVIGATOR
# Set current working directory
self.cls.cwd = path
def on_done(i):
if i < 0:
if not self.cls.keep_settings:
self.cls.reset()
self.cls.keep_settings = False
# Enclosing Directory
elif i == 0:
self.navigator(os.path.dirname(path))
# Paste item if item buffer
elif i == 1 and self.item_buffer:
self.do_paste(path);
elif i == 2 and len(self.item_buffer) == 1:
self.do_paste_as(path);
# Restart navigator if the selected item is a dir, or file action on selecting a file
elif items[i]["is_dir"]:
self.navigator(items[i]["path"])
else:
self.do_file(items[i]["path"])
if items:
show_quick_panel(self.window, [item["name"] for item in items], on_done)
else:
sublime.status_message("No Items in %s!!!" % path)
def do_dictionary(self, path):
# Set navigator status
self.cls.navigator_status = DO_DIR
def on_done(i):
if i < 0:
if not self.cls.keep_settings:
self.cls.reset()
self.cls.keep_settings = False
# Go back to directory
elif i == 0:
self.navigator(path)
elif i == 1:
self.do_new_directory(path)
elif i == 2:
self.do_new_file(path)
elif i == 3:
self.do_open_folder(path)
elif i == 4:
self.do_rename(path)
elif i == 5:
self.do_copy(path)
elif i == 6:
self.do_move(path)
elif i == 7:
self.do_delete(path)
# Save dir_name
dir_name = os.path.basename(path)
items = [["Back", "Go back to Directory content"]]
items += [["New Directory", "Create a new Directory in \"%s\"" % dir_name]]
items += [["New File", "Create a new File in \"%s\"" % dir_name]]
items += [["Open", "Open \"%s\" outside of Sublime Text" % dir_name]]
items += [["Rename", "Rename \"%s\"" % dir_name]]
items += [["Copy To ...", "Copy \"%s\" to a different location" % dir_name]]
items += [["Move To ...", "Move \"%s\" to a different location" % dir_name]]
items += [["Delete", "Delete \"%s\"" % dir_name]]
show_quick_panel(self.window, items, on_done)
def do_new_file(self, path):
# Reset FileNavigatorCommand
self.cls.reset()
def on_done(file_name):
file_path = os.path.join(path, file_name)
if os.path.exists(file_path):
sublime.error_message('File already exists:\n%s' % file_path)
else:
with open(file_path, 'w') as f:
pass
self.window.open_file(file_path)
show_input_panel(self.window, "New file name:", '', on_done)
def do_new_directory(self, path):
# Reset FileNavigatorCommand
self.cls.reset()
def on_done(dir_name):
# Reset FileNavigatorCommand
FileNavigatorCommand.reset()
dir_path = os.path.join(path, dir_name)
if os.path.exists(dir_path):
sublime.error_message('Directory already exists:\n%s' % dir_path)
else:
os.mkdir(dir_path)
show_input_panel(self.window, "New directory name:", '', on_done)
def do_file(self, path):
# Set navigator status
self.cls.navigator_status = DO_FILE
def on_done(i):
if i < 0:
if not self.cls.keep_settings:
self.cls.reset()
self.cls.keep_settings = False
# Go back to directory
elif i == 0:
self.navigator(os.path.dirname(path))
elif i == 1:
self.do_open(path)
elif i == 2:
self.do_rename(path)
elif i == 3:
self.do_copy(path)
elif i == 4:
self.do_move(path)
elif i == 5:
self.do_delete(path)
# Save dir_name
file_name = os.path.basename(path)
items = [["..", "Enclosing Folder"]]
items += [["Open", "Open \"%s\" in Sublime Text" % file_name]]
items += [["Rename", "Rename \"%s\"" % file_name]]
items += [["Copy To ...", "Copy \"%s\" to a different location" % file_name]]
items += [["Move To ...", "Move \"%s\" to a different location" % file_name]]
items += [["Delete", "Delete \"%s\"" % file_name]]
show_quick_panel(self.window, items, on_done)
def do_open(self, path):
# Reset FileNavigatorCommand
self.cls.reset()
self.window.open_file(path)
def do_open_folder(self, path):
# Reset FileNavigatorCommand
self.cls.reset()
if sublime.platform() == "windows":
subprocess.call(["explorer", path])
elif sublime.platform() == "osx":
subprocess.call(["open", path])
elif sublime.platform() == "linux":
subprocess.call(["xdg-open", path])
def do_rename(self, path):
# Reset FileNavigatorCommand
self.cls.reset()
# Save source name
source_name = os.path.basename(path)
def on_done(target_name):
target_path = path[:-len(source_name)] + target_name
shutil.move(path, target_path)
sublime.status_message("%s renamed to %s" % (source_name, target_name))
show_input_panel(self.window, "Rename:", source_name, on_done)
def do_delete(self, path):
# Reset FileNavigatorCommand
self.cls.reset()
# Save source name
source_name = os.path.basename(path)
if sublime.ok_cancel_dialog("Delete %s?" % source_name, "Delete"):
send2trash(path)
sublime.status_message("%s deleted" % source_name)
def do_move(self, path):
roots = self.find_root()
# add history_items
roots += [item["path"] for item in history_items()]
self.item_buffer = [{"file_path": path, "file_name": os.path.basename(path), "type": "move"}]
self.choose_root(list(set(roots)))
def do_copy(self, path):
roots = self.find_root()
# add history_items
roots += [item["path"] for item in history_items()]
self.item_buffer = [{"file_path": path, "file_name": os.path.basename(path), "type": "copy"}]
self.choose_root(list(set(roots)))
def do_paste_as(self, path):
# Save source name
source_name = self.item_buffer[0]["file_name"]
def on_done(target_name):
self.item_buffer[0]["file_name"] = target_name
self.do_paste(path)
show_input_panel(self.window, "Paste As:", source_name, on_done)
def do_paste(self, path):
# Reset FileNavigatorCommand
self.cls.reset()
# Load settings
s = sublime.load_settings("File Navigator.sublime-settings")
cache_timeout = s.get("cache_timeout", 24)
# add history_items
items = history_items()
items += [{"path": path, "rtime": int(time.time())}]
# Add history items
s = sublime.load_settings("File Navigator.history")
s.set("items", items)
sublime.save_settings("File Navigator.history")
for item in self.item_buffer:
try:
if item["type"] == "move":
shutil.move(item["file_path"], os.path.join(path, item["file_name"]))
elif item["type"] == "copy":
if os.path.isdir(item["file_path"]):
shutil.copytree(item["file_path"], os.path.join(path, item["file_name"]))
else:
shutil.copy(item["file_path"], os.path.join(path, item["file_name"]))
except Exception as e:
pass
sublime.status_message("%d paste in %s" % (len(self.item_buffer), path))
class FileNavigatorToggelHiddenFilesCommand(sublime_plugin.WindowCommand):
def run(self):
if FileNavigatorCommand.navigator_status == NAVIGATOR:
# Set show_hidden_files for the next run
FileNavigatorCommand.show_hidden_files = not FileNavigatorCommand.show_hidden_files
sublime.status_message("Show all available files" if FileNavigatorCommand.show_hidden_files else "Hide system/hidden files")
FileNavigatorCommand.keep_settings = True
self.window.run_command("file_navigator", {"path": FileNavigatorCommand.cwd})
class FileNavigatorDoDirectory(sublime_plugin.WindowCommand):
def run(self):
if FileNavigatorCommand.navigator_status == CHOOSE_ROOT:
pass
elif FileNavigatorCommand.navigator_status == NAVIGATOR:
FileNavigatorCommand.keep_settings = True
self.window.run_command("file_navigator", {"do_dir": True, "path": FileNavigatorCommand.cwd})
elif FileNavigatorCommand.navigator_status in [DO_DIR, DO_FILE]:
FileNavigatorCommand.keep_settings = True
self.window.run_command("file_navigator", {"path": FileNavigatorCommand.cwd})
class FileNavigatorResetHistory(sublime_plugin.WindowCommand):
def is_enabled(self):
try:
return os.path.isfile(os.path.join(os.path.join(sublime.cache_path(), "File Navigator", "History.json")))
except Exception as e:
return False
def run(self):
try:
if sublime.ok_cancel_dialog("Delete File Navigator History?", "Delete"):
os.remove(os.path.join(sublime.cache_path(), "FileNavigator", "History.json"))
except Exception as e:
pass