Made UltiSnipsEdit! more consistent with multiple filetypes.

This commit is contained in:
Holger Rapp 2014-03-15 15:55:03 +01:00
parent 5ac0436694
commit f40c3ac5e0
2 changed files with 25 additions and 20 deletions

View File

@ -34,7 +34,7 @@ function! UltiSnips#Edit(bang, ...)
if a:0 == 1 && a:1 != '' if a:0 == 1 && a:1 != ''
let type = a:1 let type = a:1
else else
exec g:_uspy "vim.command(\"let type = '%s'\" % UltiSnips_Manager._primary_filetype)" let type = ""
endif endif
exec g:_uspy "vim.command(\"let file = '%s'\" % UltiSnips_Manager._file_to_edit(vim.eval(\"type\"), vim.eval('a:bang')))" exec g:_uspy "vim.command(\"let file = '%s'\" % UltiSnips_Manager._file_to_edit(vim.eval(\"type\"), vim.eval('a:bang')))"

View File

@ -527,16 +527,10 @@ class SnippetManager(object):
return None return None
return self._csnippets[-1] return self._csnippets[-1]
@property def _file_to_edit(self, requested_ft, bang): # pylint: disable=no-self-use
def _primary_filetype(self): """Returns a file to be edited for the given requested_ft. If 'bang' is
"""This filetype will be edited when UltiSnipsEdit is called without empty only private files in g:UltiSnipsSnippetsDir are considered,
any arguments.""" otherwise all files are considered and the user gets to choose.
return self._buffer_filetypes[_vim.buf.number][0]
def _file_to_edit(self, ft, bang): # pylint: disable=no-self-use
"""Returns a file to be edited for the given 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 # 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. # therefore in this class because it is the facade to Vim.
@ -551,11 +545,22 @@ class SnippetManager(object):
else: else:
snippet_dir = os.path.join(_vim.eval("$HOME"), snippet_dir = os.path.join(_vim.eval("$HOME"),
".vim", "UltiSnips") ".vim", "UltiSnips")
potentials.update(find_snippet_files(ft, snippet_dir))
potentials.add(os.path.join(snippet_dir, ft + '.snippets'))
if bang: filetypes = []
potentials.update(find_all_snippet_files(ft)) if requested_ft:
filetypes.append(requested_ft)
else:
if bang:
filetypes.extend(self._buffer_filetypes[_vim.buf.number])
else:
filetypes.append(self._buffer_filetypes[_vim.buf.number][0])
for ft in filetypes:
potentials.update(find_snippet_files(ft, snippet_dir))
potentials.add(os.path.join(snippet_dir,
ft + '.snippets'))
if bang:
potentials.update(find_all_snippet_files(ft))
potentials = set(os.path.realpath(os.path.expanduser(p)) potentials = set(os.path.realpath(os.path.expanduser(p))
for p in potentials) for p in potentials)
@ -564,13 +569,13 @@ class SnippetManager(object):
files = sorted(potentials) files = sorted(potentials)
formatted = [as_unicode('%i: %s') % (i, fn) for formatted = [as_unicode('%i: %s') % (i, fn) for
i, fn in enumerate(files, 1)] i, fn in enumerate(files, 1)]
edit = _ask_user(files, formatted) file_to_edit = _ask_user(files, formatted)
if edit is None: if file_to_edit is None:
return "" return ""
else: else:
edit = potentials.pop() file_to_edit = potentials.pop()
dirname = os.path.dirname(edit) dirname = os.path.dirname(file_to_edit)
if not os.path.exists(dirname): if not os.path.exists(dirname):
os.makedirs(dirname) os.makedirs(dirname)
return edit return file_to_edit