Added support for multiple defined triggers

This commit is contained in:
Holger Rapp 2009-07-04 21:58:13 +02:00
parent 0aa37bf1f7
commit 4eae66c1b8
3 changed files with 109 additions and 20 deletions

View File

@ -29,3 +29,32 @@ except ${2:Exception}, ${3:e}:
${4:raise $3}
endsnippet
snippet try "Try / Except / Else"
try:
${1:pass}
except ${2:Exception}, ${3:e}:
${4:raise $3}
else:
${5:pass}
endsnippet
snippet try "Try / Except / Finally"
try:
${1:pass}
except ${2:Exception}, ${3:e}:
${4:raise $3}
else:
${5:pass}
endsnippet
snippet try "Try / Except / Else / Finally"
try:
${1:pass}
except${2: ${3:Exception}, ${4:e}}:
${5:raise}
else:
${6:pass}
finally:
${7:pass}
endsnippet

View File

@ -451,7 +451,7 @@ class TabStop(ChangeableText):
newcol += col
vim.current.window.cursor = newline + 1, newcol
if len(self.current_text) == 0:
if newcol == 0:
vim.command(r'call feedkeys("\<Esc>i")')
@ -575,9 +575,14 @@ class SnippetInstance(TextObject):
class Snippet(object):
_INDENT = re.compile(r"^[ \t]*")
def __init__(self,trigger,value):
def __init__(self,trigger,value, descr):
self._t = trigger
self._v = value
self._d = descr
def description(self):
return self._d
description = property(description)
def trigger(self):
return self._t
@ -597,10 +602,10 @@ class Snippet(object):
indend = self._INDENT.match(text_before).group(0)
v = self._v
if len(indend):
lines = self._v.splitlines()
lines = self._v.splitlines()
v = lines[0] + os.linesep + \
os.linesep.join([ indend + l for l in lines[1:]])
debug("args: %s" % (repr(v)))
s = SnippetInstance(start, end, v, text_before, text_after)
@ -652,20 +657,27 @@ class SnippetManager(object):
def _load_snippets_from(self, ft, fn):
debug("Loading snippets from: %s" % fn)
cs = None
cv = ""
cdescr = ""
for line in open(fn):
if line.startswith("#"):
continue
if line.startswith("snippet"):
cs = line.split()[1]
left = line.find('"')
if left != -1:
right = line.rfind('"')
cdescr = line[left+1:right]
continue
if cs != None:
if line.startswith("endsnippet"):
cv = cv[:-1] # Chop the last newline
self._snippets[ft][cs] = Snippet(cs,cv)
l = self._snippets[ft].get(cs,[])
l.append(Snippet(cs,cv,cdescr))
self._snippets[ft][cs] = l
cv = ""
cdescr = ""
cs = None
continue
else:
@ -685,10 +697,19 @@ class SnippetManager(object):
self._snippets = {}
self._current_snippets = []
def add_snippet(self,trigger,value):
def add_snippet(self,trigger,value, descr):
if "all" not in self._snippets:
self._snippets["all"] = {}
self._snippets["all"][trigger] = Snippet(trigger,value)
l = self._snippets["all"].get(trigger,[])
l.append(Snippet(trigger,value, descr))
self._snippets["all"][trigger] = l
def _find_snippets(self, ft, trigger):
snips = self._snippets.get(ft,None)
if not snips:
return []
return snips.get(trigger, [])
def try_expand(self, backwards = False):
ft = vim.eval("&filetype")
@ -725,15 +746,29 @@ class SnippetManager(object):
before,after = line[:col], line[col:]
word = before.split()[-1]
snippet = None
snippets = []
if len(ft):
snippet = self._snippets[ft].get(word,None)
if snippet is None:
snippet = self._snippets["all"].get(word,None)
snippets += self._find_snippets(ft, word)
snippets += self._find_snippets("all", word)
if snippet is None:
if not len(snippets):
# No snippet found
return False
elif len(snippets) == 1:
snippet, = snippets
else:
display = repr(
[ "%i: %s" % (i+1,s.description) for i,s in
enumerate(snippets)
]
)
debug("display: %s" % (display))
rv = vim.eval("inputlist(%s)" % display)
if rv is None:
return True
rv = int(rv)
snippet = snippets[rv-1]
s = snippet.launch(before.rstrip()[:-len(word)], after)
@ -744,6 +779,7 @@ class SnippetManager(object):
return True
def cursor_moved(self):
debug("Cursor moved!")
self._cursor.update_position()
if len(self._current_snippets) and (self._cursor.has_moved):
@ -771,13 +807,11 @@ class SnippetManager(object):
self._cursor.pos.col]
cs.chars_entered(chars, self._cursor)
self._cursor.update_position()
self._cursor.update_position()
def entered_insert_mode(self):
debug("Entered insert mode.")
if len(self._current_snippets) and \
not self._current_snippets[-1].tab_selected:
debug("Dropping snippets")
self._current_snippets = []

34
test.py
View File

@ -44,11 +44,16 @@ class _VimTest(unittest.TestCase):
if not isinstance(self.snippets[0],tuple):
self.snippets = ( self.snippets, )
for sv,content in self.snippets:
for s in self.snippets:
sv,content = s[:2]
descr = ""
if len(s) == 3:
descr = s[-1]
self.send(''':py << EOF
PySnipSnippets.add_snippet("%s","""%s""")
PySnipSnippets.add_snippet("%s","""%s""", "%s")
EOF
''' % (sv,content.encode("string-escape")))
''' % (sv,content.encode("string-escape"), descr.encode("string-escape"))
)
# Clear the buffer
self.send("bggVGd")
@ -582,10 +587,31 @@ class CursorMovement_Multiline_ECR(_VimTest):
class ProperIndenting_SimpleCase_ECR(_VimTest):
snippets = ("test", "for\n blah")
wanted = " for\n blahHui"
def cmd(self):
self.type(" test\tHui")
def runTest(self): self.check_output()
######################
# SELECTING MULTIPLE #
######################
class Multiple_SimpleCaseSelectFirst_ECR(_VimTest):
snippets = ( ("test", "Case1", "This is Case 1"),
("test", "Case2", "This is Case 2") )
wanted = "Case1"
def cmd(self):
self.type("test\t1\n")
def runTest(self): self.check_output()
class Multiple_SimpleCaseSelectSecond_ECR(_VimTest):
snippets = ( ("test", "Case1", "This is Case 1"),
("test", "Case2", "This is Case 2") )
wanted = "Case2"
def cmd(self):
self.type("test\t2\n")
def runTest(self): self.check_output()
###########################################################################
# END OF TEST #
###########################################################################
if __name__ == '__main__':
import sys
import optparse