Skip to content

Commit 4eb001b

Browse files
deathaxemath2001
authored andcommitted
Fix FmDelete command (#64)
* Fix FmDelete command Commit 5f1b64c intends to enable us to individually select files to delete from the quick panel. 1. The following changes are made to the new functionality: a) Don't close a related view, as the file won't be deleted, if removed from the quick panel. b) Don't refresh the sidebar folder as nothing is changed, if a file is removed from the quick panel. (Also avoids refreshing the sidbear twice) 2. Ensures the public `run()` to be the first method in the class. 3. Avoids calling `len(self.path)` multiple times. 4. Use list arithmetic to create the quick panel items. * Apply requested review changes OK, a `num_paths` variable is considered useless even though it avoids duplicated evaluations of `self.path`, ... ? Lets remove it and the other useless temporary variables, which are used once only. The first 2 quick panel items can easily be created in-place. The number of "format()" calls is reduced for cheaper item creation. There is no need to display the number of items to delete in both items. So we can use the description line of "Cancel All" command to describe how to remove individual items from the list of "to-delete" files.
1 parent d463c82 commit 4eb001b

1 file changed

Lines changed: 34 additions & 39 deletions

File tree

commands/delete.py

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,52 @@
55

66

77
class FmDeleteCommand(AppCommand):
8+
def run(self, paths=None, *args, **kwargs):
9+
self.settings = get_settings()
10+
self.window = get_window()
11+
self.view = get_view()
12+
13+
self.paths = paths or [self.view.file_name()]
14+
if get_settings().get("ask_for_confirmation_on_delete"):
15+
paths_to_display = [
16+
[
17+
"Confirm",
18+
"Send {0} items to trash".format(len(self.paths))
19+
if len(self.paths) > 1
20+
else "Send item to trash",
21+
],
22+
[
23+
"Cancel All",
24+
"Select an individual item to remove it from the deletion list",
25+
],
26+
]
27+
paths_to_display.extend(
28+
[os.path.basename(path), path] for path in self.paths
29+
)
30+
31+
self.window.show_quick_panel(paths_to_display, self.delete)
32+
33+
else:
34+
# index 0 is like clicking on the first option of the panel
35+
# ie. confirming the deletion
36+
self.delete(index=0)
37+
838
def delete(self, index):
939
if index == 0:
1040
for path in self.paths:
1141
view = self.window.find_open_file(path)
1242
if view is not None:
1343
close_view(view)
44+
1445
try:
1546
send2trash(path)
1647
except OSError as e:
1748
sublime.error_message("Unable to send to trash: {}".format(e))
1849
raise OSError("Unable to send {0!r} to trash: {1}".format(path, e))
19-
if index > 1:
20-
view = self.window.find_open_file(self.paths[index - 2])
21-
if view is not None:
22-
close_view(view)
2350

24-
# We substract two, because 0, 1 are populated by Confirm, Cancel
25-
self.paths.remove(self.paths[index - 2])
51+
refresh_sidebar(self.settings, self.window)
2652

53+
elif index > 1:
54+
self.paths.pop(index - 2)
2755
if self.paths:
28-
refresh_sidebar(self.settings, self.window)
2956
self.run(self.paths)
30-
31-
refresh_sidebar(self.settings, self.window)
32-
33-
def run(self, paths=None, *args, **kwargs):
34-
self.settings = get_settings()
35-
self.window = get_window()
36-
self.view = get_view()
37-
38-
self.paths = paths or [self.view.file_name()]
39-
if get_settings().get("ask_for_confirmation_on_delete") is not False:
40-
nitems = "{0} ".format(len(self.paths)) if len(self.paths) > 1 else ""
41-
extras = "s" if len(self.paths) > 1 else ""
42-
43-
confirm_title = "Confirm"
44-
confirm_subtitle = "Send {}item{} to trash".format(nitems, extras)
45-
cancel_title = "Cancel All (Select an individual item to remove it from the deletion list)"
46-
cancel_subtitle = "Cancel deletion of {}item{}".format(nitems, extras)
47-
48-
paths_to_display = [
49-
[confirm_title, confirm_subtitle],
50-
[cancel_title, cancel_subtitle],
51-
]
52-
for path in self.paths:
53-
paths_to_display.append([os.path.basename(path), path])
54-
55-
self.window.show_quick_panel(
56-
paths_to_display, self.delete,
57-
)
58-
else:
59-
# index 0 is like clicking on the first option of the panel
60-
# ie. confirming the deletion
61-
self.delete(index=0)

0 commit comments

Comments
 (0)