Skip to content

Commit 288dac4

Browse files
committed
Use WindowCommands to reduce some hazzle
FileManager shouldn't abuse the ApplicationCommand class but use the WindowCommand which is designed to be used for window related commands. By doing so some of the hazzle in the different commands used to retrieve the active window and/or file can be omitted. It helps to make the commands a bit more pythonic. Note: 1. This commit is a suggestion and may some review before merging. 2. It may break compatibility with Sublime Text 2.
1 parent 6629b80 commit 288dac4

19 files changed

Lines changed: 202 additions & 286 deletions

FileManager.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
del sys.modules[module_name]
1515
prefix = None
1616

17-
from .libs.sublimefunctions import *
1817
from .commands.copy import FmCopyCommand
1918
from .commands.create import FmCreaterCommand, FmCreateCommand
2019
from .commands.create_from_selection import FmCreateFileFromSelectionCommand
@@ -31,7 +30,7 @@
3130

3231

3332
def plugin_loaded():
34-
settings = get_settings()
33+
settings = sublime.load_settings("FileManager.sublime-settings")
3534
# this use to be a supported setting, but we dropped it. (see #27)
3635
if settings.get("auto_close_empty_groups") is not None:
3736
# we could remove the setting automatically, and install the
@@ -66,7 +65,9 @@ def on_load(self, view):
6665
if snippet:
6766
view.run_command("insert_snippet", {"contents": snippet})
6867
settings.erase("fm_insert_snippet_on_load")
69-
if get_settings().get("save_after_creating"):
68+
if sublime.load_settings("FileManager.sublime-settings").get(
69+
"save_after_creating"
70+
):
7071
view.run_command("save")
7172
if settings.get("fm_reveal_in_sidebar"):
7273
view.window().run_command("reveal_in_side_bar")

commands/copy.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
# -*- encoding: utf-8 -*-
2-
from ..libs.sublimefunctions import *
3-
from .appcommand import AppCommand
2+
import os
43

4+
import sublime
55

6-
class FmCopyCommand(AppCommand):
7-
def run(self, which, paths=None):
8-
self.view = get_view()
9-
self.window = get_window()
6+
from .fmcommand import FmWindowCommand
107

11-
if paths is None:
12-
paths = [self.view.file_name()]
138

9+
class FmCopyCommand(FmWindowCommand):
10+
def run(self, which, paths=None):
1411
text = []
1512
folders = self.window.folders()
1613

17-
for path in paths:
14+
for path in paths or [self.window.active_view().file_name()]:
1815
if which == "name":
1916
text.append(os.path.basename(path))
2017
elif which == "absolute path":
@@ -30,4 +27,4 @@ def run(self, which, paths=None):
3027
text[-1] = text[-1][1:]
3128
break
3229

33-
copy("\n".join(bit.replace(os.path.sep, "/") for bit in text))
30+
sublime.set_clipboard("\n".join(bit.replace(os.path.sep, "/") for bit in text))

commands/create.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,62 @@
11
# -*- encoding: utf-8 -*-
2+
import os
3+
4+
import sublime
5+
26
from ..libs.input_for_path import InputForPath
3-
from ..libs.sublimefunctions import *
4-
from .appcommand import AppCommand
7+
from ..libs.sublimefunctions import (
8+
get_template,
9+
refresh_sidebar,
10+
transform_aliases,
11+
)
12+
from ..libs.pathhelper import user_friendly
13+
from .fmcommand import FmWindowCommand
514

615

7-
class FmCreaterCommand(AppCommand):
16+
class FmCreaterCommand(FmWindowCommand):
817
"""Create folder(s)/files that might be required and the
918
final ones if it doesn't exists. Finaly, opens the file"""
1019

1120
def run(self, abspath, input_path):
1221
input_path = user_friendly(input_path)
1322
if input_path[-1] == "/":
14-
return makedirs(abspath, exist_ok=True)
23+
return os.makedirs(abspath, exist_ok=True)
1524
if not os.path.isfile(abspath):
16-
makedirs(os.path.dirname(abspath), exist_ok=True)
25+
os.makedirs(os.path.dirname(abspath), exist_ok=True)
1726
with open(abspath, "w") as fp:
1827
pass
1928
template = get_template(abspath)
2029
else:
2130
template = None
22-
window = get_window()
23-
view = window.open_file(abspath)
24-
settings = view.settings()
31+
32+
settings = self.window.open_file(abspath).settings()
2533
if template:
2634
settings.set("fm_insert_snippet_on_load", template)
27-
refresh_sidebar(settings, window)
28-
if get_settings().get("reveal_in_sidebar"):
35+
36+
refresh_sidebar(settings, self.window)
37+
38+
if self.settings.get("reveal_in_sidebar"):
2939
settings.set("fm_reveal_in_sidebar", True)
30-
sublime.set_timeout_async(
31-
lambda: window.run_command("reveal_in_side_bar"), 500
40+
sublime.set_timeout(
41+
lambda: self.window.run_command("reveal_in_side_bar"), 500
3242
)
3343

3444

35-
class FmCreateCommand(AppCommand):
45+
class FmCreateCommand(FmWindowCommand):
3646
def run(
3747
self,
3848
paths=None,
3949
initial_text="",
4050
start_with_browser=False,
4151
no_browser_action=False,
4252
):
43-
self.settings = get_settings()
44-
self.window = sublime.active_window()
53+
view = self.window.active_view()
54+
4555
self.index_folder_separator = self.settings.get("index_folder_" + "separator")
4656
self.default_index = self.settings.get("default_index")
4757

4858
self.folders = self.window.folders()
4959

50-
self.view = get_view()
51-
5260
self.know_where_to_create_from = paths is not None
5361

5462
if paths is not None:
@@ -64,8 +72,8 @@ def run(
6472
# it is going to be interactive, so it'll be
6573
# understood from the input itself
6674
create_from = None
67-
elif self.view.file_name() is not None:
68-
create_from = os.path.dirname(self.view.file_name())
75+
elif view.file_name() is not None:
76+
create_from = os.path.dirname(view.file_name())
6977
self.know_where_to_create_from = True
7078
else:
7179
# from home
@@ -97,10 +105,12 @@ def on_change(self, input_path, path_to_create_choosed_from_browsing):
97105
splited_input = input_path.split(self.index_folder_separator, 1)
98106
if len(splited_input) == 1:
99107
index = self.default_index
100-
elif isdigit(splited_input[0]):
101-
index = int(splited_input[0])
102108
else:
103-
return None, input_path
109+
try:
110+
index = int(splited_input[0])
111+
except ValueError:
112+
return None, input_path
113+
104114
return self.folders[index], splited_input[-1]
105115
return "~", input_path
106116

commands/create_from_selection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sublime
66
import sublime_plugin
77

8-
from ..libs.sublimefunctions import *
8+
from ..libs.pathhelper import computer_friendly, user_friendly
99

1010
""" This command has been inspired at 90% by the open_url_context command
1111
AND the vintage open_file_under_selection. Thanks John!"""
@@ -139,7 +139,7 @@ def is_visible(self, event=None):
139139
if event is None:
140140
return False
141141
return (
142-
get_settings().get("show_create_from_selection_command")
142+
self.settings.get("show_create_from_selection_command")
143143
and self.view.file_name() is not None
144144
and self.get_path(event) is not None
145145
)

commands/delete.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# -*- encoding: utf-8 -*-
2-
from ..libs.sublimefunctions import *
2+
import os
3+
4+
import sublime
5+
6+
from ..libs.sublimefunctions import refresh_sidebar
37
from ..libs.send2trash import send2trash
4-
from .appcommand import AppCommand
8+
from .fmcommand import FmWindowCommand
59

610

7-
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()
11+
class FmDeleteCommand(FmWindowCommand):
12+
def run(self, paths=None):
13+
self.paths = paths or [self.window.active_view().file_name()]
1214

13-
self.paths = paths or [self.view.file_name()]
14-
if get_settings().get("ask_for_confirmation_on_delete"):
15+
if self.settings.get("ask_for_confirmation_on_delete"):
1516
paths_to_display = [
1617
[
1718
"Confirm",
@@ -41,7 +42,8 @@ def delete(self, index):
4142
for window in sublime.windows():
4243
view = window.find_open_file(path)
4344
while view is not None:
44-
close_view(view, dont_prompt_save=True)
45+
view.set_scratch(True)
46+
view.close()
4547
view = window.find_open_file(path)
4648

4749
try:

commands/duplicate.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
# -*- encoding: utf-8 -*-
2+
import os
23
import shutil
4+
5+
import sublime
6+
37
from ..libs.input_for_path import InputForPath
4-
from ..libs.sublimefunctions import *
8+
from ..libs.sublimefunctions import refresh_sidebar, yes_no_cancel_panel
9+
from ..libs.pathhelper import user_friendly
510
from ..libs.send2trash import send2trash
6-
from .appcommand import AppCommand
11+
from .fmcommand import FmWindowCommand
712

813

9-
class FmDuplicateCommand(AppCommand):
14+
class FmDuplicateCommand(FmWindowCommand):
1015
def run(self, paths=None):
11-
self.settings = get_settings()
12-
13-
self.window = get_window()
14-
1516
if paths is None:
1617
self.origin = self.window.active_view().file_name()
1718
else:
1819
self.origin = paths[0]
1920

2021
initial_path = user_friendly(self.origin)
2122

22-
args = {}
23-
args["caption"] = "Duplicate to: "
24-
args["initial_text"] = initial_path
25-
args["on_done"] = self.duplicate
26-
args["on_change"] = None
27-
args["on_cancel"] = None
28-
args["create_from"] = ""
29-
args["with_files"] = False
30-
args["pick_first"] = self.settings.get("pick_first")
31-
args["case_sensitive"] = self.settings.get("case_sensitive")
32-
args["log_in_status_bar"] = self.settings.get("log_in_status_bar")
33-
args["log_template"] = "Duplicating at {0}"
34-
35-
self.input = InputForPath(**args)
23+
self.input = InputForPath(
24+
caption="Duplicate to: ",
25+
initial_text=initial_path,
26+
on_done=self.duplicate,
27+
on_change=None,
28+
on_cancel=None,
29+
create_from="",
30+
with_files=False,
31+
pick_first=self.settings.get("pick_first"),
32+
case_sensitive=self.settings.get("case_sensitive"),
33+
log_in_status_bar=self.settings.get("log_in_status_bar"),
34+
log_template="Duplicating at {0}",
35+
)
3636

3737
head = len(os.path.dirname(initial_path)) + 1
3838
filename = len(os.path.splitext(os.path.basename(initial_path))[0])

commands/editto.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,38 @@
11
# -*- encoding: utf-8 -*-
2-
from ..libs.sublimefunctions import *
3-
from .appcommand import AppCommand
2+
from .fmcommand import FmWindowCommand
43

54

6-
class FmEditToTheRightCommand(AppCommand):
5+
class FmEditToTheRightCommand(FmWindowCommand):
76
def run(self, files=None):
8-
v = get_view()
9-
w = get_window()
10-
11-
if files is None:
12-
files = [v.file_name()]
13-
14-
w.set_layout(
7+
self.window.set_layout(
158
{
169
"cols": [0.0, 0.5, 1.0],
1710
"rows": [0.0, 1.0],
1811
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]],
1912
}
2013
)
21-
for i, file in enumerate(files, 1):
22-
w.set_view_index(w.open_file(file), 1, 0)
23-
w.focus_group(1)
14+
for file in files or [self.window.active_view().file_name()]:
15+
self.window.set_view_index(self.window.open_file(file), 1, 0)
16+
17+
self.window.focus_group(1)
2418

2519
def is_enabled(self, files=None):
26-
return (files is None or len(files) >= 1) and get_window().active_group() != 1
20+
return (files is None or len(files) >= 1) and self.window.active_group() != 1
2721

2822

29-
class FmEditToTheLeftCommand(AppCommand):
23+
class FmEditToTheLeftCommand(FmWindowCommand):
3024
def run(self, files=None):
31-
v = get_view()
32-
w = get_window()
33-
34-
if files is None:
35-
files = [v.file_name()]
36-
37-
w.set_layout(
25+
self.window.set_layout(
3826
{
3927
"cols": [0.0, 0.5, 1.0],
4028
"rows": [0.0, 1.0],
4129
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]],
4230
}
4331
)
44-
for file in files:
45-
w.set_view_index(w.open_file(file), 0, 0)
32+
for file in files or [self.window.active_view().file_name()]:
33+
self.window.set_view_index(self.window.open_file(file), 0, 0)
4634

47-
w.focus_group(0)
35+
self.window.focus_group(0)
4836

4937
def is_enabled(self, files=None):
50-
return (files is None or len(files) >= 1) and get_window().active_group() != 0
38+
return (files is None or len(files) >= 1) and self.window.active_group() != 1

commands/find_in_files.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# -*- encoding: utf-8 -*-
2-
from ..libs.sublimefunctions import *
3-
from .appcommand import AppCommand
2+
import os
43

4+
from .fmcommand import FmWindowCommand
55

6-
class FmFindInFilesCommand(AppCommand):
6+
7+
class FmFindInFilesCommand(FmWindowCommand):
78
def run(self, paths=None):
8-
if paths is None:
9-
paths = [get_view().file_name()]
109
valid_paths = set()
11-
for i, path in enumerate(paths):
10+
for path in paths or self.windows.active_view().file_name():
1211
if os.path.isfile(path):
1312
valid_paths.add(os.path.dirname(path))
1413
else:
1514
valid_paths.add(path)
16-
get_window().run_command(
15+
16+
self.window.run_command(
1717
"show_panel", {"panel": "find_in_files", "where": ", ".join(valid_paths)}
1818
)

0 commit comments

Comments
 (0)