Added support for snippet option "w" for word-boundary triggers.

This commit is contained in:
Michael Henry 2009-08-23 18:44:19 -04:00
parent 4721cb0a9b
commit 66d35419a4
3 changed files with 63 additions and 7 deletions

View File

@ -180,9 +180,15 @@ snippet on. The options currently supported are >
of the cursor). Default is to expand snippets at every position, even of the cursor). Default is to expand snippets at every position, even
mitten in the line. Most of my snippets have this option set, it keeps mitten in the line. Most of my snippets have this option set, it keeps
UltiSnips out of the way. UltiSnips out of the way.
i Inword expansion - normally, triggers need whitespace before them to be i Inword expansion - Normally, triggers need whitespace before them to be
expanded. With this option, triggers are also expanded in the middle of expanded. With this option, triggers are also expanded in the middle of
a word. a word.
w Word boundary - Normally, triggers need whitespace before them to be
expanded. With this option, the trigger will be expanded at a "word"
boundary as well, where word characters are letters, digits, and the
underscore character ('_'). This enables expansion of a trigger that
adjoins punctuation (for example) without expanding suffixes of larger
words.
4.2 Plaintext snippets *UltiSnips-plaintext-snippets* 4.2 Plaintext snippets *UltiSnips-plaintext-snippets*
---------------------- ----------------------

View File

@ -96,14 +96,42 @@ class Snippet(object):
return "Snippet(%s,%s,%s)" % (self._t,self._d,self._opts) return "Snippet(%s,%s,%s)" % (self._t,self._d,self._opts)
def matches(self, trigger): def matches(self, trigger):
if "i" not in self._opts: # If user supplies both "w" and "i", it should perhaps be an
return trigger == self._t # error, but if permitted it seems that "w" should take precedence
return trigger.endswith(self._t) # (since matching at word boundary and within a word == matching at word
# boundary).
if "w" in self._opts:
trigger_len = len(self._t)
trigger_prefix = trigger[:-trigger_len]
trigger_suffix = trigger[-trigger_len:]
match = (trigger_suffix == self._t)
if match and trigger_prefix:
# Require a word boundary between prefix and suffix.
boundaryChars = trigger_prefix[-1:] + trigger_suffix[:1]
match = re.match(r'.\b.', boundaryChars)
elif "i" in self._opts:
match = trigger.endswith(self._t)
else:
match = (trigger == self._t)
return match
def could_match(self, trigger): def could_match(self, trigger):
# it i hard to define when a inword snippet could match if "w" in self._opts:
# therefore we do not specially look for it # Trim non-empty prefix up to word boundary, if present.
return self._t.startswith(trigger) trigger_suffix = re.sub(r'^.+\b(.+)$', r'\1', trigger)
match = self._t.startswith(trigger_suffix)
# TODO: list_snippets() function cannot handle partial-trigger
# matches yet, so for now fail if we trimmed the prefix.
if trigger_suffix != trigger:
match = False
elif "i" in self._opts:
# TODO: It is hard to define when a inword snippet could match,
# therefore we check only for full-word trigger.
match = self._t.startswith(trigger)
else:
match = self._t.startswith(trigger)
return match
def overwrites_previous(self): def overwrites_previous(self):
return "!" in self._opts return "!" in self._opts

22
test.py
View File

@ -1099,6 +1099,28 @@ class SnippetOptions_ExpandInwordSnippetsWithOtherChars_Expand3(_VimTest):
keys = "ätest" + EX keys = "ätest" + EX
wanted = "äExpand me!" wanted = "äExpand me!"
class _SnippetOptions_ExpandWordSnippets(_VimTest):
snippets = (("test", "Expand me!", "", "w"), )
class SnippetOptions_ExpandWordSnippets_NormalExpand(
_SnippetOptions_ExpandWordSnippets):
keys = "test" + EX
wanted = "Expand me!"
class SnippetOptions_ExpandWordSnippets_NoExpand(
_SnippetOptions_ExpandWordSnippets):
keys = "atest" + EX
wanted = "atest"
class SnippetOptions_ExpandWordSnippets_ExpandSuffix(
_SnippetOptions_ExpandWordSnippets):
keys = "a-test" + EX
wanted = "a-Expand me!"
class SnippetOptions_ExpandWordSnippets_ExpandSuffix2(
_SnippetOptions_ExpandWordSnippets):
keys = "a(test" + EX
wanted = "a(Expand me!"
class SnippetOptions_ExpandWordSnippets_ExpandSuffix3(
_SnippetOptions_ExpandWordSnippets):
keys = "[[test" + EX
wanted = "[[Expand me!"
###################### ######################