Skip to content

Commit

Permalink
Merge pull request #686 from kovetskiy/ultisnipsedit
Browse files Browse the repository at this point in the history
UltiSnipsEdit should try to find non-empty snippet files
  • Loading branch information
seletskiy committed Apr 18, 2016
2 parents 4c71935 + eb7240f commit 8250d33
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
11 changes: 8 additions & 3 deletions doc/UltiSnips.txt
Expand Up @@ -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
Expand Down
68 changes: 53 additions & 15 deletions pythonx/UltiSnips/snippet_manager.py
Expand Up @@ -771,30 +771,64 @@ def _cs(self):
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
empty only private files in g:UltiSnipsSnippetsDir are considered,
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)
Expand Down Expand Up @@ -824,9 +858,13 @@ def _file_to_edit(self, requested_ft, bang): # pylint: disable=no-self-use
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
Expand Down

0 comments on commit 8250d33

Please sign in to comment.