Overwrite all snippets with lower priority on expansion.

Fixes #233.
This commit is contained in:
Holger Rapp 2014-03-23 17:30:44 +01:00
parent 5116001246
commit 4b4ee48858
2 changed files with 26 additions and 8 deletions

View File

@ -440,27 +440,33 @@ class SnippetManager(object):
elif feedkey: elif feedkey:
_vim.command("return %s" % _vim.escape(feedkey)) _vim.command("return %s" % _vim.escape(feedkey))
def _snips(self, before, possible): def _snips(self, before, partial):
""" Returns all the snippets for the given text """Returns all the snippets for the given text before the cursor. If
before the cursor. If possible is True, then get all partial is True, then get also return partial matches. """
possible matches.
"""
filetypes = self._buffer_filetypes[_vim.buf.number][::-1] filetypes = self._buffer_filetypes[_vim.buf.number][::-1]
matching_snippets = defaultdict(list) matching_snippets = defaultdict(list)
for _, source in self._snippet_sources: for _, source in self._snippet_sources:
for snippet in source.get_snippets(filetypes, before, possible): for snippet in source.get_snippets(filetypes, before, partial):
matching_snippets[snippet.trigger].append(snippet) matching_snippets[snippet.trigger].append(snippet)
if not matching_snippets: if not matching_snippets:
return [] return []
# Now filter duplicates and only keep the one with the highest # Now filter duplicates and only keep the one with the highest
# priority. Only keep the snippets with the highest priority. # priority.
snippets = [] snippets = []
for snippets_with_trigger in matching_snippets.values(): for snippets_with_trigger in matching_snippets.values():
highest_priority = max(s.priority for s in snippets_with_trigger) highest_priority = max(s.priority for s in snippets_with_trigger)
snippets.extend(s for s in snippets_with_trigger snippets.extend(s for s in snippets_with_trigger
if s.priority == highest_priority) if s.priority == highest_priority)
return snippets
# For partial matches we are done, but if we want to expand a snippet,
# we have to go over them again and only keep those with the maximum
# priority.
if partial:
return snippets
highest_priority = max(s.priority for s in snippets)
return [s for s in snippets if s.priority == highest_priority]
def _do_snippet(self, snippet, before): def _do_snippet(self, snippet, before):
"""Expands the given snippet, and handles everything """Expands the given snippet, and handles everything

12
test.py
View File

@ -3282,6 +3282,18 @@ snippet test1
} }
keys = "test" + EX keys = "test" + EX
wanted = "blub\n\nblah\n" wanted = "blub\n\nblah\n"
class snipMate_OverwrittenByRegExpTrigger(_VimTest):
files = { "snippets/_.snippets": """
snippet def
\tsnipmate
""",
"us/all.snippets": r"""
snippet "(de)?f" "blub" r
ultisnips
endsnippet
""" }
keys = "def" + EX
wanted = "ultisnips"
# End: snipMate support #}}} # End: snipMate support #}}}
# SnippetsInCurrentScope {{{# # SnippetsInCurrentScope {{{#
class VerifyVimDict1(_VimTest): class VerifyVimDict1(_VimTest):