Skip to content

Commit

Permalink
Fix SirVer#1339 wrong cursor position in nested snippets (SirVer#1347)
Browse files Browse the repository at this point in the history
This PR fixes the problem mentioned in issue SirVer#1339. _VimCursor dummy NoneditableTextObject is now created as a child of the EditableTextObject actually being edited at that time. Thereby it is ensured that it is unambiguous which mirrors should and should not move the cursor position further. It also resolves general issues when a snippet contains only tabstops and mirrors and no actual text.

Tests have been added to ensure that this remains functional and the test suite completes successfully.

Fixes SirVer#1339.
  • Loading branch information
JeWe37 authored Apr 18, 2021
1 parent b2902a6 commit 204b501
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
5 changes: 3 additions & 2 deletions pythonx/UltiSnips/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
def echo_to_hierarchy(text_object):
"""Outputs the given 'text_object' and its children hierarchically."""
# pylint:disable=protected-access
orig = text_object
parent = text_object
while parent._parent:
parent = parent._parent

def _do_print(text_object, indent=""):
"""prints recursively."""
debug(indent + text_object)
debug(indent + ("MAIN: " if text_object == orig else "") + str(text_object))
try:
for child in text_object._children:
_do_print(child, indent=indent + " ")
Expand All @@ -47,5 +48,5 @@ def print_stack():
"""Dump a stack trace into the debug file."""
import traceback

with open(DUMP_FILENAME, "ab") as dump_file:
with open(DUMP_FILENAME, "a") as dump_file:
traceback.print_stack(file=dump_file)
2 changes: 1 addition & 1 deletion pythonx/UltiSnips/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def guess_edit(initial_line, last_text, current_text, vim_state):
if is_complete_edit(initial_line, last_text, current_text, es):
return True, es
elif len(current_text) < len(last_text):
# where some lines deleted? (dd or so)
# were some lines deleted? (dd or so)
es = []
for i in range(len(last_text) - len(current_text)):
es.append(("D", pos.line, 0, last_text[pos.line - initial_line + i]))
Expand Down
18 changes: 11 additions & 7 deletions pythonx/UltiSnips/text_objects/snippet_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,22 @@ def update_textobjects(self, buf):
This might also move the Cursor
"""
vc = _VimCursor(self)
done = set()
not_done = set()

def _find_recursive(obj):
"""Finds all text objects and puts them into 'not_done'."""
cursorInsideLowest = None
if isinstance(obj, EditableTextObject):
if obj.start <= vim_helper.buf.cursor <= obj.end and not (isinstance(obj, TabStop) and obj.number == 0):
cursorInsideLowest = obj
for child in obj._children:
_find_recursive(child)
cursorInsideLowest = _find_recursive(child) or cursorInsideLowest
not_done.add(obj)

_find_recursive(self)

return cursorInsideLowest
cursorInsideLowest = _find_recursive(self)
if cursorInsideLowest is not None:
vc = _VimCursor(cursorInsideLowest)
counter = 10
while (done != not_done) and counter:
# Order matters for python locals!
Expand All @@ -100,8 +103,9 @@ def _find_recursive(obj):
"'if not snip.c' to make sure to only expand random output "
"once."
)
vc.to_vim()
self._del_child(vc)
if cursorInsideLowest is not None:
vc.to_vim()
cursorInsideLowest._del_child(vc)

def select_next_tab(self, jump_direction: JumpDirection):
"""Selects the next tabstop in the direction of 'jump_direction'."""
Expand Down
25 changes: 25 additions & 0 deletions test/test_Mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ class SimpleMirrorSameLine_ExpectCorrectResult(_VimTest):
keys = "test" + EX + "hallo"
wanted = "hallo hallo"

class SimpleMirrorSameLineNoSpace_ExpectCorrectResult(_VimTest):
snippets = ("test", "$1$1")
keys = "test" + EX + "hallo"
wanted = "hallohallo"

class SimpleMirrorSameLineNoSpaceInsideOther_ExpectCorrectResult(_VimTest):
snippets = (
("test", "$1$1"),
("outer", "$1")
)
keys = "outer" + EX + "test" + EX + "hallo"
wanted = "hallohallo"

class SimpleMirrorSameLineNoSpaceSpaceAfter_ExpectCorrectResult(_VimTest):
snippets = ("test", "$1$1 ")
keys = "test" + EX + "hallo"
wanted = "hallohallo "

class SimpleMirrorSameLineNoSpaceInsideOtherSpaceAfter_ExpectCorrectResult(_VimTest):
snippets = (
("test", "$1$1 "),
("outer", "$1")
)
keys = "outer" + EX + "test" + EX + "hallo"
wanted = "hallohallo "

class SimpleMirrorSameLine_InText_ExpectCorrectResult(_VimTest):
snippets = ("test", "$1 $1")
Expand Down

0 comments on commit 204b501

Please sign in to comment.