Skip to content

Commit b3fdbff

Browse files
committed
Add setting to automatically append extensions when using the move command.
1 parent 7fc38c4 commit b3fdbff

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

AdvancedNewFile.sublime-settings

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,8 @@
128128
// true, a file named "foo bar" would be created. In addition, setting this value
129129
// to true will allow for curly brace expansion. Currently, only comma separated
130130
// entries are supported.
131-
"shell_input": false
131+
"shell_input": false,
132+
133+
// Setting to control if the extension will be automatically applied to renamed files.
134+
"append_extension_on_move": false
132135
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ An object containing information to use for templates when creating new files. T
164164

165165
Setting this value to true will allow you to escape characters as you normally would when using a shell. For example, given the input string "foo\ bar", false would result in a file named " bar" in the foo directory. With the value set to true, a file named "foo bar" would be created. In addition, setting this value to true will allow for curly brace expansion. Currently, only comma separated entries are supported.
166166

167+
`append_extension_on_move`:
168+
169+
Setting to control if the extension will be automatically applied to renamed files.
170+
167171
### Project Specific Settings
168172
All of the above settings can also be specified as part of the project specific settings. These values override any previous values set by higher level settings, with aliases being an exception. Alias settings will be merged with higher level configurations for alias. In addition, if the same alias exist for both default/user settings and project settings, the project setting will take precedence.
169173

advanced_new_file/anf_util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
VCS_MANAGEMENT_SETTING = "vcs_management"
2929
FILE_TEMPLATES_SETTING = "file_templates"
3030
SHELL_INPUT_SETTING = "shell_input"
31+
APPEND_EXTENSION_ON_MOVE_SETTING = "append_extension_on_move"
3132

3233
SETTINGS = [
3334
ALIAS_SETTING,
@@ -55,7 +56,8 @@
5556
RENAME_DEFAULT_SETTING,
5657
VCS_MANAGEMENT_SETTING,
5758
FILE_TEMPLATES_SETTING,
58-
SHELL_INPUT_SETTING
59+
SHELL_INPUT_SETTING,
60+
APPEND_EXTENSION_ON_MOVE_SETTING
5961
]
6062

6163
NIX_ROOT_REGEX = r"^/"

advanced_new_file/commands/move_file_command.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def _git_mv(self, from_filepath, to_filepath):
5656

5757
def entered_file_action(self, path):
5858
attempt_open = True
59+
path = self.try_append_extension(path)
60+
5961
directory = os.path.dirname(path)
6062
if not os.path.exists(directory):
6163
try:
@@ -69,6 +71,22 @@ def entered_file_action(self, path):
6971
if attempt_open:
7072
self._rename_file(path)
7173

74+
def is_copy_original_name(self, path):
75+
return (os.path.isdir(path) or
76+
os.path.basename(path) == "")
77+
78+
def try_append_extension(self, path):
79+
if self.settings.get(APPEND_EXTENSION_ON_MOVE_SETTING, False):
80+
if not self.is_copy_original_name(path):
81+
_, new_path_extension = os.path.splitext(path)
82+
if new_path_extension == "":
83+
if self.rename_filename is None:
84+
_, extension = os.path.splitext(self.view.file_name())
85+
else:
86+
_, extension = os.path.splitext(self.rename_filename)
87+
path += extension
88+
return path
89+
7290
def _rename_file(self, file_path):
7391
if os.path.isdir(file_path) or re.search(r"(/|\\)$", file_path):
7492
# use original name if a directory path has been passed in.
@@ -113,10 +131,11 @@ def _move_action(self, from_file, to_file):
113131
shutil.move(from_file, to_file)
114132

115133
def update_status_message(self, creation_path):
134+
if self.is_copy_original_name(creation_path):
135+
creation_path = os.path.join(creation_path, self.original_name)
136+
else:
137+
creation_path = self.try_append_extension(creation_path)
116138
if self.view is not None:
117-
if (os.path.isdir(creation_path) or
118-
os.path.basename(creation_path) == ""):
119-
creation_path = os.path.join(creation_path, self.original_name)
120139
self.view.set_status("AdvancedNewFile", "Moving file to %s " %
121140
creation_path)
122141
else:

0 commit comments

Comments
 (0)