Skip to content

Commit

Permalink
Add fallback setting if resolving relative path from current file fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
skuroda committed Mar 15, 2014
1 parent 20967f6 commit 7731334
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
8 changes: 7 additions & 1 deletion AdvancedNewFile.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,11 @@
"shell_input": false,

// Setting to control if the extension will be automatically applied to renamed files.
"append_extension_on_move": false
"append_extension_on_move": false,

// 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.
"relative_fallback_index": 0
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ Setting this value to true will allow you to escape characters as you normally w

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

`relative_fallback_index`:

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.

### Project Specific Settings
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.

Expand Down
4 changes: 3 additions & 1 deletion advanced_new_file/anf_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
FILE_TEMPLATES_SETTING = "file_templates"
SHELL_INPUT_SETTING = "shell_input"
APPEND_EXTENSION_ON_MOVE_SETTING = "append_extension_on_move"
RELATIVE_FALLBACK_INDEX_SETTING = "relative_fallback_index"

SETTINGS = [
ALIAS_SETTING,
Expand Down Expand Up @@ -57,7 +58,8 @@
VCS_MANAGEMENT_SETTING,
FILE_TEMPLATES_SETTING,
SHELL_INPUT_SETTING,
APPEND_EXTENSION_ON_MOVE_SETTING
APPEND_EXTENSION_ON_MOVE_SETTING,
RELATIVE_FALLBACK_INDEX_SETTING
]

NIX_ROOT_REGEX = r"^/"
Expand Down
37 changes: 23 additions & 14 deletions advanced_new_file/commands/command_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ def __generate_default_root(self):
if path is None and folder_index is None:
return os.path.expanduser(self.settings.get(DEFAULT_PATH_SETTING))
elif path is None:
if folder_index >= 0:
return self.window.folders()[folder_index]
else:
return os.path.expanduser("~/")
return self.__project_folder_from_index(folder_index)
return path

def __generate_alias_root(self):
Expand Down Expand Up @@ -93,7 +90,6 @@ def __get_aliases(self):
def __parse_path_setting(self, setting, index_setting):
root = None
folder_index = None
num_folders = len(self.window.folders())
if setting == "home":
root = os.path.expanduser("~/")
elif setting == "current":
Expand All @@ -104,22 +100,24 @@ def __parse_path_setting(self, setting, index_setting):
root = os.path.expanduser("~/")
elif setting == "project_folder":
folder_index = self.settings.get(index_setting)
if num_folders == 0:
folder_index = -1
elif num_folders < folder_index:
folder_index = 0
folder_index = self.__validate_folder_index(folder_index)
elif setting == "top_folder":
if num_folders == 0:
folder_index = -1
else:
folder_index = 0
folder_index = self.__validate_folder_index(0)
elif setting == "path":
pass
else:
print("Invalid root specifier")

return (root, folder_index)

def __validate_folder_index(self, folder_index):
num_folders = len(self.window.folders())
if num_folders == 0:
folder_index = -1
elif num_folders < folder_index:
folder_index = 0
return folder_index

def split_path(self, path=""):
HOME_REGEX = r"^~[/\\]"
root = None
Expand Down Expand Up @@ -148,7 +146,12 @@ def split_path(self, path=""):
elif (re.match(r"^\.{1,2}[/\\]", path) and
self.settings.get(RELATIVE_FROM_CURRENT_SETTING, False)):
path_index = 2
root = os.path.dirname(self.view.file_name())
if self.view.file_name() is not None:
root = os.path.dirname(self.view.file_name())
else:
folder_index = self.settings.get(RELATIVE_FALLBACK_INDEX_SETTING, 0)
folder_index = self.__validate_folder_index(folder_index)
root = self.__project_folder_from_index(folder_index)
if re.match(r"^\.{2}[/\\]", path):
root = os.path.dirname(root)
path_index = 3
Expand All @@ -162,6 +165,12 @@ def split_path(self, path=""):

return root, path

def __project_folder_from_index(self, folder_index):
if folder_index >= 0:
return self.window.folders()[folder_index]
else:
return os.path.expanduser("~/")

def bash_expansion(self, path):
if len(path) == 0:
return path
Expand Down

0 comments on commit 7731334

Please sign in to comment.