there is now only one snippet active at all times, we no longer need an array for the current snippets

This commit is contained in:
Holger Rapp 2009-07-09 07:55:25 +02:00
parent e0f689df41
commit 448a0a30b9

View File

@ -208,7 +208,7 @@ class SnippetManager(object):
def reset(self): def reset(self):
self._snippets = {} self._snippets = {}
self._current_snippets = [] self._csnippet = None
def add_snippet(self, trigger, value, descr): def add_snippet(self, trigger, value, descr):
if "all" not in self._snippets: if "all" not in self._snippets:
@ -234,10 +234,9 @@ class SnippetManager(object):
self._accept_input = False self._accept_input = False
self._expect_move_wo_change = False self._expect_move_wo_change = False
if len(self._current_snippets): if self._csnippet:
cs = self._current_snippets[-1]
self._expect_move_wo_change = True self._expect_move_wo_change = True
ts = cs.select_next_tab(backwards) ts = self._csnippet.select_next_tab(backwards)
if ts is None: if ts is None:
# HACK: only jump to end if there is no zero defined. This # HACK: only jump to end if there is no zero defined. This
# TODO: this jump should be inside select_next_tab or even # TODO: this jump should be inside select_next_tab or even
@ -245,11 +244,11 @@ class SnippetManager(object):
# defined, a $0 should be appended to the end of it and this # defined, a $0 should be appended to the end of it and this
# extra code should be ignored Jump to the end of the snippet # extra code should be ignored Jump to the end of the snippet
# and enter insert mode # and enter insert mode
cs = self._current_snippets[-1] if 0 not in self._csnippet._tabstops:
if 0 not in cs._tabstops: vim.current.window.cursor = self._csnippet.end.line +1, \
vim.current.window.cursor = cs.end.line +1, cs.end.col self._csnippet.end.col
vim.command(r'call feedkeys("\<Esc>a")') vim.command(r'call feedkeys("\<Esc>a")')
self._current_snippets.pop() self._csnippet = None
return True return True
else: else:
@ -305,7 +304,7 @@ class SnippetManager(object):
self._vstate.update() self._vstate.update()
if s is not None: if s is not None:
self._current_snippets.append(s) self._csnippet = s
self._accept_input = True self._accept_input = True
@ -327,25 +326,20 @@ class SnippetManager(object):
debug("Checking if we left the snippet") debug("Checking if we left the snippet")
debug("self._vstate.pos: %s" % (self._vstate.pos)) debug("self._vstate.pos: %s" % (self._vstate.pos))
if len(self._current_snippets): if self._csnippet:
cs = self._current_snippets[-1] is_inside = self._vstate.pos in self._csnippet.span
debug("cs.span: %s" % (cs.span))
is_inside = self._vstate.pos in cs.span
debug("is_inside: %s" % (is_inside)) debug("is_inside: %s" % (is_inside))
if not is_inside: if not is_inside:
self._current_snippets.pop() self._csnippet = None
debug("self._accept_input): %s" % (self._accept_input)) debug("self._accept_input): %s" % (self._accept_input))
if not self._accept_input: if not self._accept_input:
return return
if self._vstate.buf_changed and len(self._current_snippets): if self._vstate.buf_changed and self._csnippet:
if 0 <= self._vstate.moved.line <= 1: if 0 <= self._vstate.moved.line <= 1:
cs = self._current_snippets[-1]
# Detect a carriage return # Detect a carriage return
if self._vstate.moved.col < 0 and self._vstate.moved.line == 1: if self._vstate.moved.col < 0 and self._vstate.moved.line == 1:
# Hack, remove a line in vim, because we are going to # Hack, remove a line in vim, because we are going to
@ -355,16 +349,17 @@ class SnippetManager(object):
# user # user
cache_pos = vim.current.window.cursor cache_pos = vim.current.window.cursor
del vim.current.buffer[self._vstate.pos.line-1] del vim.current.buffer[self._vstate.pos.line-1]
cs.chars_entered('\n', self._vstate) self._csnippet.chars_entered('\n', self._vstate)
vim.current.window.cursor = cache_pos vim.current.window.cursor = cache_pos
elif self._vstate.moved.col < 0: # Some deleting was going on elif self._vstate.moved.col < 0: # Some deleting was going on
cs.backspace(-self._vstate.moved.col, self._vstate) self._csnippet.backspace(-self._vstate.moved.col,
self._vstate)
else: else:
line = vim.current.line line = vim.current.line
chars = line[self._vstate.pos.col - self._vstate.moved.col: chars = line[self._vstate.pos.col - self._vstate.moved.col:
self._vstate.pos.col] self._vstate.pos.col]
cs.chars_entered(chars, self._vstate) self._csnippet.chars_entered(chars, self._vstate)
self._vstate.update() self._vstate.update()
@ -375,21 +370,16 @@ class SnippetManager(object):
self._vstate.update() self._vstate.update()
debug("self._vstate.has_moved: %s" % (self._vstate.has_moved)) debug("self._vstate.has_moved: %s" % (self._vstate.has_moved))
if len(self._current_snippets) and \ if self._csnippet and self._vstate.has_moved:
self._vstate.has_moved: self._csnippet = None
# not self._current_snippets[-1].tab_selected and \
self._current_snippets = []
def backspace(self): def backspace(self):
# BS was called in select mode # BS was called in select mode
if len(self._current_snippets) and \ if self._csnippet and self._csnippet.tab_selected:
self._current_snippets[-1].tab_selected:
# This only happens when a default value is delted using backspace # This only happens when a default value is delted using backspace
vim.command(r'call feedkeys("i")') vim.command(r'call feedkeys("i")')
cs = self._current_snippets[-1] self._csnippet.chars_entered('', self._vstate)
cs.chars_entered('', self._vstate)
self._vstate.update() self._vstate.update()
else: else:
vim.command(r'call feedkeys("\<BS>")') vim.command(r'call feedkeys("\<BS>")')