diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 1cd5063..5fcc5d3 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -195,9 +195,14 @@ g:UltiSnipsSnippetsDir files are stored in. For example, if the variable is set to "~/.vim/mydir/UltiSnips" and the current 'filetype' is "cpp", then :UltiSnipsEdit will open - "~/.vim/mydir/UltiSnips/cpp.snippets". Note that - directories named "snippets" are reserved for - snipMate snippets and cannot be used. + "~/.vim/mydir/UltiSnips/cpp.snippets" if file is + not empty, if it's empty :UltiSnipsEdit will see + for non-empty files in directories + g:UltiSnipsSnippetDirectories, if nothing found, + :UltiSnipsEdit will open new file in + g:UltiSnipsSnippetsDir. + Note that directories named "snippets" are + reserved for snipMate snippets and cannot be used. *g:UltiSnipsSnippetDirectories* g:UltiSnipsSnippetDirectories diff --git a/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index 9cc82e9..a93b2c7 100644 --- a/pythonx/UltiSnips/snippet_manager.py +++ b/pythonx/UltiSnips/snippet_manager.py @@ -771,7 +771,7 @@ class SnippetManager(object): return None return self._csnippets[-1] - def _file_to_edit(self, requested_ft, bang): # pylint: disable=no-self-use + def _file_to_edit(self, requested_ft, bang): """Returns a file to be edited for the given requested_ft. If 'bang' is @@ -779,22 +779,56 @@ class SnippetManager(object): otherwise all files are considered and the user gets to choose. """ - # This method is not using self, but is called by UltiSnips.vim and is - # therefore in this class because it is the facade to Vim. - potentials = set() + snippet_dir = '' if _vim.eval("exists('g:UltiSnipsSnippetsDir')") == '1': - snippet_dir = _vim.eval('g:UltiSnipsSnippetsDir') - else: - home = _vim.eval('$HOME') - if platform.system() == 'Windows': - snippet_dir = os.path.join(home, 'vimfiles', 'UltiSnips') - elif _vim.eval("has('nvim')") == '1': - xdg_home_config = _vim.eval('$XDG_CONFIG_HOME') or os.path.join(home, ".config") - snippet_dir = os.path.join(xdg_home_config, 'nvim', 'UltiSnips') - else: - snippet_dir = os.path.join(home, '.vim', 'UltiSnips') + dir = _vim.eval('g:UltiSnipsSnippetsDir') + file = self._get_file_to_edit(dir, requested_ft, bang) + if file: + return file + snippet_dir = dir + if _vim.eval("exists('g:UltiSnipsSnippetDirectories')") == '1': + dirs = _vim.eval('g:UltiSnipsSnippetDirectories') + for dir in dirs: + file = self._get_file_to_edit(dir, requested_ft, bang) + if file: + return file + if not snippet_dir: + snippet_dir = dir + + home = _vim.eval('$HOME') + if platform.system() == 'Windows': + dir = os.path.join(home, 'vimfiles', 'UltiSnips') + file = self._get_file_to_edit(dir, requested_ft, bang) + if file: + return file + if not snippet_dir: + snippet_dir = dir + + if _vim.eval("has('nvim')") == '1': + xdg_home_config = _vim.eval('$XDG_CONFIG_HOME') or os.path.join(home, ".config") + dir = os.path.join(xdg_home_config, 'nvim', 'UltiSnips') + file = self._get_file_to_edit(dir, requested_ft, bang) + if file: + return file + if not snippet_dir: + snippet_dir = dir + + dir = os.path.join(home, '.vim', 'UltiSnips') + file = self._get_file_to_edit(dir, requested_ft, bang) + if file: + return file + if not snippet_dir: + snippet_dir = dir + + return self._get_file_to_edit( + snippet_dir, requested_ft, bang, True + ) + + def _get_file_to_edit(self, snippet_dir, requested_ft, bang, + allow_empty=False): # pylint: disable=no-self-use + potentials = set() filetypes = [] if requested_ft: filetypes.append(requested_ft) @@ -824,9 +858,13 @@ class SnippetManager(object): else: file_to_edit = potentials.pop() + if not allow_empty and not os.path.exists(file_to_edit): + return '' + dirname = os.path.dirname(file_to_edit) if not os.path.exists(dirname): os.makedirs(dirname) + return file_to_edit @contextmanager