Skip to content

Commit c5d9f9a

Browse files
committed
Fix bug where moving file from buffer would fail
1 parent 69e2bea commit c5d9f9a

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

AdvancedNewFile.sublime-settings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
// folders exist as part of the project the home directory will be used.
146146
"relative_fallback_index": 0,
147147

148-
// Setting to control if the extension will be automatically applied to renamed files.
148+
// Setting to control if the extension will be automatically applied to copied files.
149149
"append_extension_on_copy": true,
150150

151151
// Same as `rename_default`, but applied to copy command.

advanced_new_file/commands/move_file_command.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,47 @@ def _rename_file(self, file_path):
5757
window = self.window
5858
rename_filename = self.get_argument_name()
5959
if rename_filename:
60-
file_view = self._find_open_file(rename_filename)
61-
if file_view is not None:
62-
self.view.run_command("save")
63-
window.focus_view(file_view)
64-
window.run_command("close")
65-
66-
self._move_action(rename_filename, file_path)
67-
68-
if file_view is not None:
69-
self.open_file(file_path)
70-
71-
elif self.view is not None and self.view.file_name() is not None:
72-
filename = self.view.file_name()
73-
if filename:
74-
self.view.run_command("save")
75-
window.focus_view(self.view)
76-
window.run_command("close")
77-
self._move_action(filename, file_path)
78-
else:
79-
content = self.view.substr(sublime.Region(0, self.view.size()))
80-
self.view.set_scratch(True)
81-
window.focus_view(self.view)
82-
window.run_command("close")
83-
with open(file_path, "w") as file_obj:
84-
file_obj.write(content)
85-
self.open_file(file_path)
60+
self.move_from_argument(rename_filename, file_path)
61+
elif self.view is not None:
62+
self.move_from_view(self.view, file_path)
8663
else:
8764
sublime.error_message("Unable to move file. No file to move.")
8865

66+
def move_from_argument(self, source, target):
67+
file_view = self._find_open_file(source)
68+
if file_view is not None:
69+
self.view.run_command("save")
70+
window.focus_view(file_view)
71+
window.run_command("close")
72+
73+
self._move_action(source, target)
74+
75+
if file_view is not None:
76+
self.open_file(target)
77+
78+
def move_from_view(self, source_view, target):
79+
source = source_view.file_name()
80+
if source is None:
81+
self.move_file_from_buffer(source_view, target)
82+
else:
83+
self.move_file_from_disk(source, target)
84+
self.open_file(target)
85+
86+
def move_file_from_disk(self, source, target):
87+
self.view.run_command("save")
88+
window.focus_view(self.view)
89+
window.run_command("close")
90+
self._move_action(filename, file_path)
91+
92+
def move_file_from_buffer(self, source_view, target):
93+
window = self.window
94+
content = self.view.substr(sublime.Region(0, self.view.size()))
95+
self.view.set_scratch(True)
96+
window.focus_view(self.view)
97+
window.run_command("close")
98+
with open(target, "w") as file_obj:
99+
file_obj.write(content)
100+
89101
def _move_action(self, from_file, to_file):
90102
tracked_by_git = self.file_tracked_by_git(from_file)
91103
if tracked_by_git and self.settings.get(VCS_MANAGEMENT_SETTING):

0 commit comments

Comments
 (0)