Skip to content

Commit 7731334

Browse files
committed
Add fallback setting if resolving relative path from current file fails.
1 parent 20967f6 commit 7731334

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

AdvancedNewFile.sublime-settings

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,11 @@
132132
"shell_input": false,
133133

134134
// Setting to control if the extension will be automatically applied to renamed files.
135-
"append_extension_on_move": false
135+
"append_extension_on_move": false,
136+
137+
// An integer value representing a folder index to be used when a relative path
138+
// cannot be resolved from the current active view. If an index outside of the range
139+
// of existing folders is used, it will default to 0 (the top level folder). If no
140+
// folders exist as part of the project the home directory will be used.
141+
"relative_fallback_index": 0
136142
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ Setting this value to true will allow you to escape characters as you normally w
168168

169169
Setting to control if the extension will be automatically applied to renamed files.
170170

171+
`relative_fallback_index`:
172+
173+
An integer value representing a folder index to be used when a relative path cannot be resolved from the current active view. If an index outside of the range of existing folders is used, it will default to 0 (the top level folder). If no folders exist as part of the project the home directory will be used.
174+
171175
### Project Specific Settings
172176
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.
173177

advanced_new_file/anf_util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
FILE_TEMPLATES_SETTING = "file_templates"
3030
SHELL_INPUT_SETTING = "shell_input"
3131
APPEND_EXTENSION_ON_MOVE_SETTING = "append_extension_on_move"
32+
RELATIVE_FALLBACK_INDEX_SETTING = "relative_fallback_index"
3233

3334
SETTINGS = [
3435
ALIAS_SETTING,
@@ -57,7 +58,8 @@
5758
VCS_MANAGEMENT_SETTING,
5859
FILE_TEMPLATES_SETTING,
5960
SHELL_INPUT_SETTING,
60-
APPEND_EXTENSION_ON_MOVE_SETTING
61+
APPEND_EXTENSION_ON_MOVE_SETTING,
62+
RELATIVE_FALLBACK_INDEX_SETTING
6163
]
6264

6365
NIX_ROOT_REGEX = r"^/"

advanced_new_file/commands/command_base.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ def __generate_default_root(self):
3030
if path is None and folder_index is None:
3131
return os.path.expanduser(self.settings.get(DEFAULT_PATH_SETTING))
3232
elif path is None:
33-
if folder_index >= 0:
34-
return self.window.folders()[folder_index]
35-
else:
36-
return os.path.expanduser("~/")
33+
return self.__project_folder_from_index(folder_index)
3734
return path
3835

3936
def __generate_alias_root(self):
@@ -93,7 +90,6 @@ def __get_aliases(self):
9390
def __parse_path_setting(self, setting, index_setting):
9491
root = None
9592
folder_index = None
96-
num_folders = len(self.window.folders())
9793
if setting == "home":
9894
root = os.path.expanduser("~/")
9995
elif setting == "current":
@@ -104,22 +100,24 @@ def __parse_path_setting(self, setting, index_setting):
104100
root = os.path.expanduser("~/")
105101
elif setting == "project_folder":
106102
folder_index = self.settings.get(index_setting)
107-
if num_folders == 0:
108-
folder_index = -1
109-
elif num_folders < folder_index:
110-
folder_index = 0
103+
folder_index = self.__validate_folder_index(folder_index)
111104
elif setting == "top_folder":
112-
if num_folders == 0:
113-
folder_index = -1
114-
else:
115-
folder_index = 0
105+
folder_index = self.__validate_folder_index(0)
116106
elif setting == "path":
117107
pass
118108
else:
119109
print("Invalid root specifier")
120110

121111
return (root, folder_index)
122112

113+
def __validate_folder_index(self, folder_index):
114+
num_folders = len(self.window.folders())
115+
if num_folders == 0:
116+
folder_index = -1
117+
elif num_folders < folder_index:
118+
folder_index = 0
119+
return folder_index
120+
123121
def split_path(self, path=""):
124122
HOME_REGEX = r"^~[/\\]"
125123
root = None
@@ -148,7 +146,12 @@ def split_path(self, path=""):
148146
elif (re.match(r"^\.{1,2}[/\\]", path) and
149147
self.settings.get(RELATIVE_FROM_CURRENT_SETTING, False)):
150148
path_index = 2
151-
root = os.path.dirname(self.view.file_name())
149+
if self.view.file_name() is not None:
150+
root = os.path.dirname(self.view.file_name())
151+
else:
152+
folder_index = self.settings.get(RELATIVE_FALLBACK_INDEX_SETTING, 0)
153+
folder_index = self.__validate_folder_index(folder_index)
154+
root = self.__project_folder_from_index(folder_index)
152155
if re.match(r"^\.{2}[/\\]", path):
153156
root = os.path.dirname(root)
154157
path_index = 3
@@ -162,6 +165,12 @@ def split_path(self, path=""):
162165

163166
return root, path
164167

168+
def __project_folder_from_index(self, folder_index):
169+
if folder_index >= 0:
170+
return self.window.folders()[folder_index]
171+
else:
172+
return os.path.expanduser("~/")
173+
165174
def bash_expansion(self, path):
166175
if len(path) == 0:
167176
return path

0 commit comments

Comments
 (0)