From d4e1fc526725ac207a4d58f7ed4aa45cf020f4b9 Mon Sep 17 00:00:00 2001 From: Cibin Mathew Date: Tue, 9 Aug 2022 01:17:14 +0530 Subject: [PATCH] warn overwrite on copy command --- AdvancedNewFile.sublime-settings | 4 ++++ advanced_new_file/anf_util.py | 2 ++ advanced_new_file/commands/copy_file_command.py | 9 +++++++++ advanced_new_file/commands/move_file_command.py | 4 ++-- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/AdvancedNewFile.sublime-settings b/AdvancedNewFile.sublime-settings index b4fcb94..a257ba8 100644 --- a/AdvancedNewFile.sublime-settings +++ b/AdvancedNewFile.sublime-settings @@ -165,6 +165,10 @@ // the move command. "warn_overwrite_on_move": false, + // If a warning should be displayed when trying to overwrite an existing file using + // the copy command. + "warn_overwrite_on_copy": false, + // Same as `default_root` for new file commands. In addition to the valid values listed // for `default_root`, "default_root" will use the value for that setting. "new_file_default_root": "default_root", diff --git a/advanced_new_file/anf_util.py b/advanced_new_file/anf_util.py index cfb837b..c29b746 100644 --- a/advanced_new_file/anf_util.py +++ b/advanced_new_file/anf_util.py @@ -35,6 +35,7 @@ COPY_DEFAULT_SETTING = "copy_default" CUT_TO_FILE_DEFAULT_SETTING = "cut_to_file_default" CURRENT_FALLBACK_TO_PROJECT_SETTING = "current_fallback_to_project" +WARN_OVERWRITE_ON_COPY_SETTING = "warn_overwrite_on_copy" WARN_OVERWRITE_ON_MOVE_SETTING = "warn_overwrite_on_move" NEW_FILE_DEFAULT_ROOT_SETTING = "new_file_default_root" RENAME_FILE_DEFAULT_ROOT_SETTING = "rename_file_default_root" @@ -77,6 +78,7 @@ COPY_DEFAULT_SETTING, CUT_TO_FILE_DEFAULT_SETTING, CURRENT_FALLBACK_TO_PROJECT_SETTING, + WARN_OVERWRITE_ON_COPY_SETTING, WARN_OVERWRITE_ON_MOVE_SETTING, NEW_FILE_DEFAULT_ROOT_SETTING, RENAME_FILE_DEFAULT_ROOT_SETTING, diff --git a/advanced_new_file/commands/copy_file_command.py b/advanced_new_file/commands/copy_file_command.py index 85f93b8..456986a 100644 --- a/advanced_new_file/commands/copy_file_command.py +++ b/advanced_new_file/commands/copy_file_command.py @@ -38,6 +38,13 @@ def entered_file_action(self, path): if copy_success: self.open_file(new_file) + def _try_prompt_if_dest_exists(self, target): + if self.settings.get(WARN_OVERWRITE_ON_COPY_SETTING, False): + if (os.path.exists(target)): + return sublime.ok_cancel_dialog(target + " already exists. " + + "Copy will overwrite " + + "existing file. Continue?") + def _copy_file(self, path): if os.path.isdir(path) or re.search(r"(/|\\)$", path): # use original name if a directory path has been passed in. @@ -45,6 +52,8 @@ def _copy_file(self, path): window = self.window copied = True + if not self._try_prompt_if_dest_exists(path): + return (False, path) if self.get_argument_name(): self.copy_from_argument(path) elif self.view is not None: diff --git a/advanced_new_file/commands/move_file_command.py b/advanced_new_file/commands/move_file_command.py index 11fcdb7..049d1ca 100644 --- a/advanced_new_file/commands/move_file_command.py +++ b/advanced_new_file/commands/move_file_command.py @@ -88,8 +88,8 @@ def move_from_view(self, source_view, target): def _try_prompt_if_dest_exists(self, target): if self.settings.get(WARN_OVERWRITE_ON_MOVE_SETTING, False): if (os.path.exists(target)): - return sublime.ok_cancel_dialog(target + "already exists. " + - "move will overwrite " + + return sublime.ok_cancel_dialog(target + " already exists. " + + "Move will overwrite " + "existing file. Continue?") return True