/advanced_new_file/commands/move_file_command.py

https://github.com/skuroda/Sublime-AdvancedNewFile · Python · 143 lines · 115 code · 27 blank · 1 comment · 25 complexity · 7f53ddc075ec98ba4e1caeac330cdc2b MD5 · raw file

  1. import os
  2. import re
  3. import shutil
  4. import sublime_plugin
  5. from .duplicate_file_base import DuplicateFileBase
  6. from ..anf_util import *
  7. from ..vcs.git.git_command_base import GitCommandBase
  8. class AdvancedNewFileMove(DuplicateFileBase, GitCommandBase):
  9. def __init__(self, window):
  10. super(AdvancedNewFileMove, self).__init__(window)
  11. def get_default_setting(self):
  12. return RENAME_DEFAULT_SETTING
  13. def input_panel_caption(self):
  14. caption = 'Enter a new path for current file'
  15. if self.is_python:
  16. caption = '%s (creates __init__.py in new dirs)' % caption
  17. return caption
  18. def _git_mv(self, from_filepath, to_filepath):
  19. path, filename = os.path.split(from_filepath)
  20. args = ["mv", filename, to_filepath]
  21. result = self.run_command(args, path)
  22. if result != 0:
  23. sublime.error_message("Git move of %s to %s failed" %
  24. (from_filepath, to_filepath))
  25. def entered_file_action(self, path):
  26. attempt_open = True
  27. path = self.try_append_extension(path)
  28. directory = os.path.dirname(path)
  29. if not os.path.exists(directory):
  30. try:
  31. self.create_folder(directory)
  32. except OSError as e:
  33. attempt_open = False
  34. sublime.error_message("Cannot create '" + path + "'." +
  35. " See console for details")
  36. print("Exception: %s '%s'" % (e.strerror, e.filename))
  37. if attempt_open:
  38. self._rename_file(path)
  39. def get_append_extension_setting(self):
  40. return APPEND_EXTENSION_ON_MOVE_SETTING
  41. def _rename_file(self, file_path):
  42. if os.path.isdir(file_path) or re.search(r"(/|\\)$", file_path):
  43. # use original name if a directory path has been passed in.
  44. file_path = os.path.join(file_path, self.original_name)
  45. window = self.window
  46. rename_filename = self.get_argument_name()
  47. if not self._try_prompt_if_dest_exists(file_path):
  48. return
  49. if rename_filename:
  50. self.move_from_argument(rename_filename, file_path)
  51. elif self.view is not None:
  52. self.move_from_view(self.view, file_path)
  53. else:
  54. sublime.error_message("Unable to move file. No file to move.")
  55. def move_from_argument(self, source, target):
  56. file_view = self._find_open_file(source)
  57. if file_view is not None:
  58. self.view.run_command("save")
  59. window.focus_view(file_view)
  60. window.run_command("close")
  61. self._move_action(source, target)
  62. if file_view is not None:
  63. self.open_file(target)
  64. def move_from_view(self, source_view, target):
  65. source = source_view.file_name()
  66. if source is None:
  67. self.move_file_from_buffer(source_view, target)
  68. else:
  69. self.move_file_from_disk(source, target)
  70. self.open_file(target)
  71. def _try_prompt_if_dest_exists(self, target):
  72. if self.settings.get(WARN_OVERWRITE_ON_MOVE_SETTING, False):
  73. if (os.path.exists(target)):
  74. return sublime.ok_cancel_dialog(target + "already exists. " +
  75. "move will overwrite " +
  76. "existing file. Continue?")
  77. return True
  78. def move_file_from_disk(self, source, target):
  79. window = self.window
  80. self.view.run_command("save")
  81. window.focus_view(self.view)
  82. window.run_command("close")
  83. self._move_action(source, target)
  84. def move_file_from_buffer(self, source_view, target):
  85. window = self.window
  86. content = self.view.substr(sublime.Region(0, self.view.size()))
  87. self.view.set_scratch(True)
  88. window.focus_view(self.view)
  89. window.run_command("close")
  90. with open(target, "w") as file_obj:
  91. file_obj.write(content)
  92. def _move_action(self, from_file, to_file):
  93. tracked_by_git = self.file_tracked_by_git(from_file)
  94. if tracked_by_git and self.settings.get(VCS_MANAGEMENT_SETTING):
  95. self._git_mv(from_file, to_file)
  96. else:
  97. shutil.move(from_file, to_file)
  98. def get_status_prefix(self):
  99. return "Moving file to"
  100. def get_default_root_setting(self):
  101. return RENAME_FILE_DEFAULT_ROOT_SETTING
  102. def generate_initial_path(self):
  103. if self.settings.get("autofill_path_the_existing"):
  104. file_path = self.window.active_view().file_name()[1:]
  105. base, _ = self.split_path(file_path)
  106. return file_path[len(base):]
  107. else:
  108. return super(self.__class__, self).generate_initial_path()
  109. class AdvancedNewFileMoveAtCommand(sublime_plugin.WindowCommand):
  110. def run(self, files):
  111. if len(files) != 1:
  112. return
  113. self.window.run_command("advanced_new_file_move",
  114. {"rename_file": files[0]})
  115. def is_visible(self, files):
  116. return len(files) == 1