Skip to content

Commit

Permalink
Fixed jumping back bug after killing the current tabstop
Browse files Browse the repository at this point in the history
  • Loading branch information
SirVer committed Feb 12, 2012
1 parent 164bcbc commit 92209f9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
41 changes: 24 additions & 17 deletions plugin/UltiSnips/_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def is_complete_edit(initial_line, a, b, cmds):
if char != '\n':
buf[line] = buf[line][:col] + buf[line][col+len(char):]
else:
if len(buf) > 1:
if line + 1 < len(buf):
buf[line] = buf[line] + buf[line+1]
del buf[line+1]
else:
Expand Down Expand Up @@ -55,25 +55,32 @@ def guess_edit(initial_line, lt, ct, vs):
if sv != pos and sv.line == pos.line:
es.append(("I", sv.line, sv.col, ct[sv.line - initial_line][sv.col:pos.col+1]))
if is_complete_edit(initial_line, lt, ct, es): return True, es
if pos.line == ppos.line and len(lt) == len(ct): # Movement only in one line
llen = len(lt[ppos.line - initial_line])
clen = len(ct[pos.line - initial_line])
if ppos < pos and clen > llen: # Likely that only characters have been added
es = (
("I", ppos.line, ppos.col, ct[ppos.line - initial_line][ppos.col:pos.col]),
)
if is_complete_edit(initial_line, lt, ct, es): return True, es
if clen < llen:
if ppos == pos: # 'x' or DEL or dt or something
es = (
("D", pos.line, pos.col, lt[ppos.line - initial_line][ppos.col:ppos.col + (llen - clen)]),
)
if is_complete_edit(initial_line, lt, ct, es): return True, es
if pos < ppos: # Backspacing or dT dF?
if pos.line == ppos.line:
if len(lt) == len(ct): # Movement only in one line
llen = len(lt[ppos.line - initial_line])
clen = len(ct[pos.line - initial_line])
if ppos < pos and clen > llen: # Likely that only characters have been added
es = (
("D", pos.line, pos.col, lt[pos.line - initial_line][pos.col:pos.col + llen - clen]),
("I", ppos.line, ppos.col, ct[ppos.line - initial_line][ppos.col:pos.col]),
)
if is_complete_edit(initial_line, lt, ct, es): return True, es
if clen < llen:
if ppos == pos: # 'x' or DEL or dt or something
es = (
("D", pos.line, pos.col, lt[ppos.line - initial_line][ppos.col:ppos.col + (llen - clen)]),
)
if is_complete_edit(initial_line, lt, ct, es): return True, es
if pos < ppos: # Backspacing or dT dF?
es = (
("D", pos.line, pos.col, lt[pos.line - initial_line][pos.col:pos.col + llen - clen]),
)
if is_complete_edit(initial_line, lt, ct, es): return True, es
elif len(ct) < len(lt): # Maybe some lines were deleted? (dd or so)
es = []
for i in range(len(lt)-len(ct)):
es.append( ("D", pos.line, 0, lt[pos.line - initial_line + i]))
es.append( ("D", pos.line, 0, '\n'))
if is_complete_edit(initial_line, lt, ct, es): return True, es
else: # Movement in more than one line
if ppos.line + 1 == pos.line and pos.col == 0: # Carriage return?
es = (("I", ppos.line, ppos.col, "\n"),)
Expand Down
2 changes: 1 addition & 1 deletion plugin/UltiSnips/text_objects/_snippet_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def select_next_tab(self, backwards = False):
res = self._get_prev_tab(self._cts)
if res is None:
self._cts = cts_bf
return self._tabstops[self._cts]
return self._tabstops.get(self._cts, None)
self._cts, ts = res
return ts
else:
Expand Down
12 changes: 12 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2800,6 +2800,18 @@ class DeleteLastTwoLinesInSnippet(_VimTest):
snippets = ("test", "$1hello\nnice\nworld")
keys = "test" + EX + ESC + "j2dd"
wanted = "hello"
class DeleteCurrentTabStop1_JumpBack(_VimTest):
snippets = ("test", "${1:hi}\nend")
keys = "test" + EX + ESC + "ddi" + JB
wanted = "end"
class DeleteCurrentTabStop2_JumpBack(_VimTest):
snippets = ("test", "${1:hi}\n${2:world}\nend")
keys = "test" + EX + JF + ESC + "ddi" + JB + "hello"
wanted = "hello\nend"
class DeleteCurrentTabStop3_JumpAround(_VimTest):
snippets = ("test", "${1:hi}\n${2:world}\nend")
keys = "test" + EX + JF + ESC + "ddkji" + JB + "hello" + JF + "world"
wanted = "hello\nendworld"

# End: Normal mode editing #}}}
###########################################################################
Expand Down

0 comments on commit 92209f9

Please sign in to comment.