Added support for overwriting of previous snippet definitions

This commit is contained in:
Holger Rapp 2009-07-17 23:33:48 +02:00
parent e3ae328d8c
commit 93b3cc7126
3 changed files with 73 additions and 13 deletions

View File

@ -141,11 +141,23 @@ specified.
The snippets file format is simple. A line starting with # is a comment, each
snippet starts with a line in the form of: >
snippet tab_trigger "description"
snippet tab_trigger [ "description" [ options ] ]
The following lines are the snippets definition; a line starting with >
endsnippet
marks the end of the snippet. The trailing newline is chopped from the
definition.
definition. In the starting line, the description and options are optional.
The description is only needed when more than one snippet is defined with the
same tab trigger. The user is then given a choice and the description is
displayed for the user as helper.
The options is a word of characters, each turning a specific option for this
snippet on. The options currently supported are >
! Overwrite - This snippet will overwrite all previously defined
snippets with this tab trigger. Default is to let the user choose at
expansion which snippet to expand. This option is useful to overwrite
bundled tab stops with user defined ones.
4.2 Plaintext snippets *UltiSnips-plaintext-snippets*
----------------------

View File

@ -14,10 +14,15 @@ from UltiSnips.Buffer import VimBuffer
class Snippet(object):
_INDENT = re.compile(r"^[ \t]*")
def __init__(self,trigger,value, descr):
def __init__(self, trigger, value, descr, options):
self._t = trigger
self._v = value
self._d = descr
self._overwrites = "!" in options
def overwrites_previous(self):
return self._overwrites
overwrites_previous = property(overwrites_previous)
def value(self):
return self._v
@ -170,11 +175,11 @@ class SnippetManager(object):
self._expect_move_wo_change = False
def add_snippet(self, trigger, value, descr):
def add_snippet(self, trigger, value, descr, options):
if "all" not in self._snippets:
self._snippets["all"] = {}
l = self._snippets["all"].get(trigger,[])
l.append(Snippet(trigger,value, descr))
l.append(Snippet(trigger,value, descr, options))
self._snippets["all"][trigger] = l
def jump(self, backwards = False):
@ -220,6 +225,12 @@ class SnippetManager(object):
for ft in filetypes:
snippets += self._find_snippets(ft, word)
# Search if any of the snippets overwrites the previous
for idx in range(len(snippets)-1,-1,-1):
if snippets[idx].overwrites_previous:
snippets = snippets[idx:]
break
if not len(snippets):
# No snippet found
return False
@ -419,6 +430,7 @@ class SnippetManager(object):
cs = None
cv = ""
cdescr = ""
coptions = ""
for line in open(fn):
if cs is None and line.startswith("#"):
continue
@ -428,15 +440,15 @@ class SnippetManager(object):
if left != -1:
right = line.rfind('"')
cdescr = line[left+1:right]
coptions = line[right:].strip()
continue
if cs != None:
if line.startswith("endsnippet"):
cv = cv[:-1] # Chop the last newline
l = self._snippets[ft].get(cs,[])
l.append(Snippet(cs,cv,cdescr))
l.append(Snippet(cs,cv,cdescr,coptions))
self._snippets[ft][cs] = l
cv = ""
cdescr = ""
cv = cdescr = coptions = ""
cs = None
continue
else:

46
test.py
View File

@ -74,13 +74,19 @@ class _VimTest(unittest.TestCase):
for s in self.snippets:
sv,content = s[:2]
descr = ""
if len(s) == 3:
descr = s[-1]
options = ""
if len(s) > 2:
descr = s[2]
if len(s) > 3:
options = s[3]
self.send(''':py << EOF
UltiSnips_Manager.add_snippet("%s","""%s""", "%s")
UltiSnips_Manager.add_snippet("%s","""%s""", "%s", "%s")
EOF
''' % (sv,content.encode("string-escape"), descr.encode("string-escape"))
)
''' % (sv,content.encode("string-escape"), descr.encode("string-escape"),
options
)
)
# Clear the buffer
self.send("bggVGd")
@ -938,6 +944,36 @@ class Completion_SimpleExample_ECR(_VimTest):
"superkallifragilistik some more"
###################
# SNIPPET OPTIONS #
###################
class SnippetOptions_OverwriteExisting_ECR(_VimTest):
snippets = (
("test", "${1:Hallo}", "Types Hallo"),
("test", "${1:World}", "Types World"),
("test", "We overwrite", "Overwrite the two", "!"),
)
keys = "test" + EX
wanted = "We overwrite"
class SnippetOptions_OverwriteTwice_ECR(_VimTest):
snippets = (
("test", "${1:Hallo}", "Types Hallo"),
("test", "${1:World}", "Types World"),
("test", "We overwrite", "Overwrite the two", "!"),
("test", "again", "Overwrite again", "!"),
)
keys = "test" + EX
wanted = "again"
class SnippetOptions_OverwriteThenChoose_ECR(_VimTest):
snippets = (
("test", "${1:Hallo}", "Types Hallo"),
("test", "${1:World}", "Types World"),
("test", "We overwrite", "Overwrite the two", "!"),
("test", "No overwrite", "Not overwritten", ""),
)
keys = "test" + EX + "1\n\n" + "test" + EX + "2\n"
wanted = "We overwrite\nNo overwrite"
######################
# SELECTING MULTIPLE #