From aaa89bf0d62597f35b9d0de947c78f9539dadcd1 Mon Sep 17 00:00:00 2001 From: Stanislav Seletskiy Date: Wed, 27 Apr 2016 22:29:50 +0600 Subject: [PATCH] actions: do not break cursor on single line change --- pythonx/UltiSnips/buffer_proxy.py | 21 +++++++++++++-------- test/test_SnippetActions.py | 11 +++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/pythonx/UltiSnips/buffer_proxy.py b/pythonx/UltiSnips/buffer_proxy.py index 6c5bb4a..85585f6 100644 --- a/pythonx/UltiSnips/buffer_proxy.py +++ b/pythonx/UltiSnips/buffer_proxy.py @@ -163,12 +163,12 @@ class VimBufferProxy(_vim.VimBuffer): for line_number in range(start, end): if line_number < 0: line_number = len(self._buffer) + line_number - yield ('D', line_number, 0, self._buffer[line_number]) + yield ('D', line_number, 0, self._buffer[line_number], True) if start < 0: start = len(self._buffer) + start for line_number in range(0, len(new_value)): - yield ('I', start+line_number, 0, new_value[line_number]) + yield ('I', start+line_number, 0, new_value[line_number], True) def _get_line_diff(self, line_number, before, after): """ @@ -189,25 +189,30 @@ class VimBufferProxy(_vim.VimBuffer): if not self._snippets_stack: return - line_number = change[1] - column_number = change[2] + change_type, line_number, column_number, change_text = change[0:4] + line_before = line_number <= self._snippets_stack[0]._start.line column_before = column_number <= self._snippets_stack[0]._start.col if line_before and column_before: direction = 1 - if change[0] == 'D': + if change_type == 'D': direction = -1 + diff = Position(direction, 0) + if len(change) != 5: + diff = Position(0, direction * len(change_text)) + print(change, diff) + self._snippets_stack[0]._move( - Position(line_number, 0), - Position(direction, 0) + Position(line_number, column_number), + diff ) else: if line_number > self._snippets_stack[0]._end.line: return if column_number >= self._snippets_stack[0]._end.col: return - self._snippets_stack[0]._do_edit(change) + self._snippets_stack[0]._do_edit(change[0:4]) def _disable_edits(self): """ diff --git a/test/test_SnippetActions.py b/test/test_SnippetActions.py index 403e234..5fc0bdf 100644 --- a/test/test_SnippetActions.py +++ b/test/test_SnippetActions.py @@ -368,3 +368,14 @@ class SnippetActions_PostActionModifiesLineAfterSnippet(_VimTest): """} keys = '\n3' + ARR_U + 'a' + EX + '1' + JF + '2' wanted = '1: 1\n2' + + +class SnippetActions_DoNotBreakCursorOnSingleLikeChange(_VimTest): + files = { 'us/all.snippets': r""" + post_expand "snip.buffer[snip.snippet_end[0]] = 'def'; snip.cursor.preserve()" + snippet a "desc" + asd + endsnippet + """} + keys = 'a' + EX + '123' + wanted = 'def123'