Skip to content

Commit

Permalink
Refactor "File => Rename" command, thonny#1446
Browse files Browse the repository at this point in the history
  • Loading branch information
aivarannamaa committed Nov 26, 2020
1 parent 00687d8 commit ee4189d
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions thonny/editors.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ def is_modified(self):
def save_file_enabled(self):
return self.is_modified() or not self.get_filename()

def save_file(self, ask_filename=False, save_copy=False):
def save_file(self, ask_filename=False, save_copy=False, node=None):
if self._filename is not None and not ask_filename:
save_filename = self._filename
get_workbench().event_generate("Save", editor=self, filename=save_filename)
else:
save_filename = self.ask_new_path()
save_filename = self.ask_new_path(node)

if not save_filename:
return None
Expand Down Expand Up @@ -349,8 +349,9 @@ def write_remote_file(self, save_filename, content_bytes, save_copy):
else:
return False

def ask_new_path(self):
node = choose_node_for_file_operations(self.winfo_toplevel(), "Where to save to?")
def ask_new_path(self, node=None):
if node is None:
node = choose_node_for_file_operations(self.winfo_toplevel(), "Where to save to?")
if not node:
return None

Expand Down Expand Up @@ -648,11 +649,11 @@ def _init_commands(self):
)

get_workbench().add_command(
"rename_file",
"move_rename_file",
"file",
tr("Rename..."),
self._cmd_rename_file,
tester=self._cmd_rename_file_enabled,
tr("Move / rename..."),
self._cmd_move_rename_file,
tester=self._cmd_move_rename_file_enabled,
group=10,
)

Expand Down Expand Up @@ -823,11 +824,11 @@ def _cmd_save_all_files_enabled(self):
return True
return False

def _cmd_save_file_as(self):
def _cmd_save_file_as(self, node=None):
if not self.get_current_editor():
return

self.get_current_editor().save_file(ask_filename=True)
self.get_current_editor().save_file(ask_filename=True, node=node)
self.update_editor_title(self.get_current_editor())
get_workbench().update_title()

Expand All @@ -841,17 +842,37 @@ def _cmd_save_copy(self):
def _cmd_save_file_as_enabled(self):
return self.get_current_editor() is not None

def _cmd_rename_file(self):
def _cmd_move_rename_file(self):
editor = self.get_current_editor()
old_filename = editor.get_filename()
assert old_filename is not None

self._cmd_save_file_as()
if is_remote_path(old_filename):
node = "remote"
else:
node = "local"

self._cmd_save_file_as(node=node)

if editor.get_filename() != old_filename:
os.remove(old_filename)
if is_remote_path(old_filename):
remote_path = extract_target_path(old_filename)
get_runner().send_command_and_wait(
InlineCommand(
"delete", paths=[remote_path], description=tr("Deleting" + remote_path)
),
dialog_title=tr("Deleting"),
)
get_workbench().event_generate(
"RemoteFileOperation", path=remote_path, operation="delete"
)
else:
os.remove(old_filename)
get_workbench().event_generate(
"LocalFileOperation", path=old_filename, operation="delete"
)

def _cmd_rename_file_enabled(self):
def _cmd_move_rename_file_enabled(self):
return self.get_current_editor() and self.get_current_editor().get_filename() is not None

def close_single_untitled_unmodified_editor(self):
Expand Down

0 comments on commit ee4189d

Please sign in to comment.