forked from facelessuser/FavoriteFiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavorite_files.py
496 lines (413 loc) · 13 KB
/
favorite_files.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
"""
Favorite Files.
Licensed under MIT
Copyright (c) 2012 - 2015 Isaac Muse <[email protected]>
"""
import sublime
import sublime_plugin
import os
from FavoriteFiles.favorites import Favorites
from FavoriteFiles.lib.notify import error
Favs = None
class FavoriteFilesCleanOrphansCommand(sublime_plugin.WindowCommand):
"""Clean out favorties that no longer exist."""
def run(self):
"""Run the command."""
# Clean out all dead links
if not Favs.load(clean=True, win_id=self.window.id()):
Favs.load(force=True, clean=True, win_id=self.window.id())
class FavoriteFilesEditAliasCommand(sublime_plugin.WindowCommand):
"""Open the selected favorite(s)."""
def edit_alias(self, value, group=False):
"""Edit alias."""
if value == -1:
return
if value >= 0:
if value < self.num_files or (group and value < self.num_files + 1):
# Open global file, file in group, or all files in group
name = self.files[value][0]
self.current_index = value
self.window.show_input_panel("Alias:", name, self.apply_alias, None, None)
else:
# Decend into group
value -= self.num_files
self.group_name = self.groups[value][0].replace("Group: ", "", 1)
self.files = Favs.all_files(group_name=self.group_name)
self.num_files = len(self.files)
self.groups = []
self.num_groups = 0
# Show files in group
if self.num_files:
self.window.show_quick_panel(
self.files,
lambda x: self.edit_alias(x, group=True)
)
else:
error("No favorites found! Try adding some.")
def apply_alias(self, value):
"""Apply alias."""
if value is not None:
Favs.set_alias(value, self.current_index, self.group_name)
def run(self):
"""Run the command."""
if not Favs.load(win_id=self.window.id()):
self.files = Favs.all_files()
self.num_files = len(self.files)
self.groups = Favs.all_groups()
self.num_groups = len(self.groups)
# initialize group to None, will be set if descending into a group
self.group_name = None
if self.num_files + self.num_groups > 0:
self.window.show_quick_panel(
self.files + self.groups,
self.edit_alias
)
else:
error("No favorites found! Try adding some.")
class FavoriteFilesOpenCommand(sublime_plugin.WindowCommand):
"""Open the selected favorite(s)."""
def open_file(self, value, group=False):
"""Open the file(s)."""
if value == -1:
return
if value >= 0:
active_group = self.window.active_group()
if value < self.num_files or (group and value < self.num_files + 1):
# Open global file, file in group, or all files in group
names = []
if group:
if value == 0:
# Open all files in group
names = [self.files[x][1] for x in range(0, self.num_files)]
else:
# Open file in group
names.append(self.files[value - 1][1])
else:
# Open global file
names.append(self.files[value][1])
# Iterate through file list ensure they load in proper view index order
count = 0
focus_view = None
for n in names:
if os.path.exists(n):
view = self.window.open_file(n)
if view is not None:
focus_view = view
if active_group >= 0:
self.window.set_view_index(view, active_group, count)
count += 1
else:
error("The following file does not exist:\n%s" % n)
if focus_view is not None:
# Horrible ugly hack to ensure opened file gets focus
def fn(focus_view):
"""Ensure focus of view."""
self.window.focus_view(focus_view)
self.window.show_quick_panel(["None"], None)
self.window.run_command("hide_overlay")
sublime.set_timeout(lambda: fn(focus_view), 500)
else:
# Decend into group
value -= self.num_files
self.files = Favs.all_files(group_name=self.groups[value][0].replace("Group: ", "", 1))
self.num_files = len(self.files)
self.groups = []
self.num_groups = 0
# Show files in group
if self.num_files:
self.window.show_quick_panel(
[["Open Group", ""]] + self.files,
lambda x: self.open_file(x, group=True)
)
else:
error("No favorites found! Try adding some.")
def run(self):
"""Run the command."""
if not Favs.load(win_id=self.window.id()):
self.files = Favs.all_files()
self.num_files = len(self.files)
self.groups = Favs.all_groups()
self.num_groups = len(self.groups)
if self.num_files + self.num_groups > 0:
self.window.show_quick_panel(
self.files + self.groups,
self.open_file
)
else:
error("No favorites found! Try adding some.")
class FavoriteFilesAddCommand(sublime_plugin.WindowCommand):
"""Add favorite(s) to the global group or the specified group."""
def prompt_for_alias(self, name, group_name=None):
"""Prompt for an alias for the favorite file."""
index = None
files = Favs.obj.files['groups'][group_name] if group_name is not None else Favs.obj.files['files']
for idx, f in enumerate(files):
if f['alias'] == name:
index = idx
break
if index is not None:
self.current_index = index
self.group_name = group_name
self.window.show_input_panel("Alias:", name, self.apply_alias, None, None)
def apply_alias(self, value):
"""Apply alias."""
if value is not None:
Favs.set_alias(value, self.current_index, self.group_name)
def add(self, names, group_name=None):
"""Add favorites."""
disk_omit_count = 0
added = 0
# Iterate names and add them to group/global if not already added
for n in names:
if Favs.file_index(n, group_name=group_name) is None:
if os.path.exists(n):
Favs.set(n, group_name=group_name)
added += 1
else:
# File does not exist on disk; cannot add
disk_omit_count += 1
if added:
# Save if files were added
Favs.save(True)
if len(names) == 1 and settings().get('always_ask_alias', False):
self.prompt_for_alias(os.path.basename(names[0]), group_name)
if disk_omit_count:
# Alert that files could be added
if disk_omit_count == 1:
message = "1 file does not exist on disk!"
else:
message = "%d file(s) do not exist on disk!" % disk_omit_count
error(message)
def create_group(self, value):
"""Create the specified group."""
repeat = False
if value == "":
# Require an actual name
error("Please provide a valid group name.")
repeat = True
elif Favs.group_exists(value):
# Do not allow duplicates
error("Group \"%s\" already exists." % value)
repeat = True
else:
# Add group
Favs.add_group(value)
self.add(self.name, value)
if repeat:
# Ask again if name was not sufficient
v = self.window.show_input_panel(
"Create Group: ",
"New Group",
self.create_group,
None,
None
)
v.run_command("select_all")
def select_group(self, value, replace=False):
"""Add favorite to the group."""
if value >= 0:
group_name = self.groups[value][0].replace("Group: ", "", 1)
if replace:
# Start with empty group for "Replace Group" selection
Favs.add_group(group_name)
# Add favorites
self.add(self.name, group_name)
def show_groups(self, replace=False):
"""Prompt user with stored groups."""
# Show availabe groups
self.groups = Favs.all_groups()
self.window.show_quick_panel(
self.groups,
lambda x: self.select_group(x, replace=replace)
)
def group_answer(self, value):
"""Handle the user's 'group' options answer."""
if value >= 0:
if value == 0:
# No group; add file to favorites
self.add(self.name)
elif value == 1:
# Request new group name
v = self.window.show_input_panel(
"Create Group: ",
"New Group",
self.create_group,
None,
None
)
v.run_command("select_all")
elif value == 2:
# "Add to Group"
self.show_groups()
elif value == 3:
# "Replace Group"
self.show_groups(replace=True)
def group_prompt(self):
"""Prompt user with 'group' options."""
# Default options
self.group = ["No Group", "Create Group"]
if Favs.group_count() > 0:
# Options if groups already exit
self.group += ["Add to Group", "Replace Group"]
self.window.show_quick_panel(
self.group,
self.group_answer
)
def file_answer(self, value):
"""Handle the user's 'add' option selection."""
if value >= 0:
view = self.window.active_view()
if view is not None:
if value == 0:
# Single file
name = view.file_name()
if name is not None:
self.name.append(name)
self.group_prompt()
if value == 1:
# All files in window
views = self.window.views()
if len(views) > 0:
for v in views:
name = v.file_name()
if name is not None:
self.name.append(name)
if len(self.name) > 0:
self.group_prompt()
if value == 2:
# All files in layout group
group = self.window.get_view_index(view)[0]
views = self.window.views_in_group(group)
if len(views) > 0:
for v in views:
name = v.file_name()
if name is not None:
self.name.append(name)
if len(self.name) > 0:
self.group_prompt()
def file_prompt(self, view_code):
"""Prompt the user for 'add' options."""
# Add current active file
options = ["Add Current File to Favorites"]
if view_code > 0:
# Add all files in window
options.append("Add All Files to Favorites")
if view_code > 1:
# Add all files in layout group
options.append("Add All Files to in Active Group to Favorites")
# Preset file options
self.window.show_quick_panel(
options,
self.file_answer
)
def run(self):
"""Run the command."""
view = self.window.active_view()
self.name = []
if view is not None:
view_code = 0
views = self.window.views()
# If there is more than one view open allow saving all views
# TODO: Widget views probably show up here too, maybe look into exclduing them
if len(views) > 1:
view_code = 1
# See if there is more than one group; if so allow saving of a specific group
if self.window.num_groups() > 1:
group = self.window.get_view_index(view)[0]
group_views = self.window.views_in_group(group)
if len(group_views) > 1:
view_code = 2
self.file_prompt(view_code)
else:
# Only single file open, proceed without file options
name = view.file_name()
if name is not None:
self.name.append(name)
self.group_prompt()
class FavoriteFilesRemoveCommand(sublime_plugin.WindowCommand):
"""Remove the file favorites from the tracked list."""
def remove(self, value, group=False, group_name=None):
"""Remove the favorite(s)."""
if value >= 0:
# Remove file from global, file from group list, or entire group
if value < self.num_files or (group and value < self.num_files + 1):
name = None
if group:
if group_name is None:
return
if value == 0:
# Remove group
Favs.remove_group(group_name)
Favs.save(True)
return
else:
# Remove group file
name = self.files[value - 1][1]
else:
# Remove global file
name = self.files[value][1]
# Remove file and save
Favs.remove(name, group_name=group_name)
Favs.save(True)
else:
# Decend into group
value -= self.num_files
group_name = self.groups[value][0].replace("Group: ", "", 1)
self.files = Favs.all_files(group_name=group_name)
self.num_files = len(self.files)
self.groups = []
self.num_groups = 0
# Show group files
if self.num_files:
self.window.show_quick_panel(
[["Remove Group", ""]] + self.files,
lambda x: self.remove(x, group=True, group_name=group_name)
)
else:
error("No favorites found! Try adding some.")
def run(self):
"""Run the command."""
if not Favs.load(win_id=self.window.id()):
# Present both files and groups for removal
self.files = Favs.all_files()
self.num_files = len(self.files)
self.groups = Favs.all_groups()
self.num_groups = len(self.groups)
# Show panel
if self.num_files + self.num_groups > 0:
self.window.show_quick_panel(
self.files + self.groups,
self.remove
)
else:
error("No favorites to remove!")
class FavoriteFilesTogglePerProjectCommand(sublime_plugin.WindowCommand):
"""Toggle per project favorites."""
def run(self):
"""Run the command."""
win_id = self.window.id()
# Try and toggle back to global first
if not Favs.toggle_global(win_id):
return
# Try and toggle per project
if Favs.toggle_per_projects(win_id):
error('Could not find a project file!')
else:
Favs.open(win_id=self.window.id())
def is_enabled(self):
"""Check if command is enabled."""
return settings().get("enable_per_projects", False)
def check_st_version():
"""Check the Sublime version."""
if int(sublime.version()) < 3080:
window = sublime.active_window()
if window is not None:
window.run_command('open_file', {"file": "${packages}/FavoriteFiles/messages/upgrade-st-3080.md"})
def settings():
"""Return settings file."""
return sublime.load_settings("FavoriteFiles.sublime-settings")
def plugin_loaded():
"""Setup plugin."""
global Favs
Favs = Favorites(os.path.join(sublime.packages_path(), 'User', 'favorite_files_list.json'))
check_st_version()