fix buffer edits from jump action

This commit is contained in:
Stanislav Seletskiy 2015-06-19 00:04:50 +06:00
parent 5a663116f5
commit 9649f15a9e
4 changed files with 59 additions and 18 deletions

View File

@ -11,12 +11,12 @@ from contextlib import contextmanager
@contextmanager
def use_proxy_buffer(snippets_stack):
def use_proxy_buffer(snippets_stack, vstate):
"""
Forward all changes made in the buffer to the current snippet stack while
function call.
"""
buffer_proxy = VimBufferProxy(snippets_stack)
buffer_proxy = VimBufferProxy(snippets_stack, vstate)
old_buffer = _vim.buf
try:
_vim.buf = buffer_proxy
@ -59,7 +59,7 @@ class VimBufferProxy(_vim.VimBuffer):
actual buffer contents.
"""
def __init__(self, snippets_stack):
def __init__(self, snippets_stack, vstate):
"""
Instantiate new object.
@ -69,6 +69,7 @@ class VimBufferProxy(_vim.VimBuffer):
self._buffer = vim.current.buffer
self._change_tick = int(vim.eval("b:changedtick"))
self._forward_edits = True
self._vstate = vstate
def is_buffer_changed_outside(self):
"""
@ -107,6 +108,8 @@ class VimBufferProxy(_vim.VimBuffer):
if self._forward_edits:
for change in changes:
self._apply_change(change)
if self._snippets_stack:
self._vstate.remember_buffer(self._snippets_stack[0])
def __setslice__(self, i, j, text):
"""
@ -194,6 +197,10 @@ class VimBufferProxy(_vim.VimBuffer):
Position(direction, 0)
)
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)
def _disable_edits(self):

View File

@ -334,26 +334,26 @@ class SnippetDefinition(object):
return False
def do_post_jump(
self, tabstop_number, jump_direction, snippets_stack
self, tabstop_number, jump_direction, snippets_stack, current_snippet
):
if 'post_jump' in self._actions:
start = snippets_stack[-1].start
end = snippets_stack[-1].end
start = current_snippet.start
end = current_snippet.end
locals = {
'tabstop': tabstop_number,
'jump_direction': jump_direction,
'tabstops': snippets_stack[-1].get_tabstops(),
'tabstops': current_snippet.get_tabstops(),
'snippet_start': start,
'snippet_end': end,
'buffer': _vim.buf
}
snip = self._execute_action(
self._actions['post_jump'], snippets_stack[-1].context, locals
self._actions['post_jump'], current_snippet.context, locals
)
snippets_stack[-1].context = snip.context
current_snippet.context = snip.context
return snip.cursor.is_set()
else:

View File

@ -455,6 +455,14 @@ class SnippetManager(object):
# self._ctab is 1 then there is 1 less CursorMove events. We
# cannot ignore next movement in such case.
ntab_short_and_near = False
if self._cs:
snippet_for_action = self._cs
elif stack_for_post_jump:
snippet_for_action = stack_for_post_jump[-1]
else:
snippet_for_action = None
if self._cs:
ntab = self._cs.select_next_tab(backwards)
if ntab:
@ -483,16 +491,12 @@ class SnippetManager(object):
self._ignore_movements = True
if len(stack_for_post_jump) > 0 and ntab is not None:
if self._cs:
snippet_for_action = self._cs
else:
snippet_for_action = stack_for_post_jump[-1]
with use_proxy_buffer(stack_for_post_jump):
with use_proxy_buffer(stack_for_post_jump, self._vstate):
snippet_for_action.snippet.do_post_jump(
ntab.number,
-1 if backwards else 1,
stack_for_post_jump
stack_for_post_jump,
snippet_for_action
)
return jumped
@ -597,7 +601,7 @@ class SnippetManager(object):
if snippet.matched:
text_before = before[:-len(snippet.matched)]
with use_proxy_buffer(self._csnippets):
with use_proxy_buffer(self._csnippets, self._vstate):
with self._action_context():
cursor_set_in_action = snippet.do_pre_expand(
self._visual_content.text,
@ -643,7 +647,7 @@ class SnippetManager(object):
si.update_textobjects()
with use_proxy_buffer(self._csnippets):
with use_proxy_buffer(self._csnippets, self._vstate):
with self._action_context():
snippet.do_post_expand(
si._start, si._end, self._csnippets

View File

@ -316,3 +316,33 @@ class SnippetActions_CanVisuallySelectFirstPlaceholderInAnonSnippetInPre(_VimTes
"""}
keys = "test" + EX + "1" + JF + "2"
wanted = """1, 2"""
class SnippetActions_UseCorrectJumpActions(_VimTest):
files = { 'us/all.snippets': r"""
post_jump "snip.buffer[-2:-2]=['a' + str(snip.tabstop)]"
snippet a "a" wb
$1 {
$2
}
endsnippet
snippet b "b" wb
bbb
endsnippet
post_jump "snip.buffer[-2:-2]=['c' + str(snip.tabstop)]"
snippet c "c" w
$1 : $2 : $3
endsnippet
"""}
keys = "a" + EX + "1" + JF + "b" + EX + " c" + EX + "2" + JF + "3" + JF + "4" + JF + JF
wanted = """1 {
bbb 2 : 3 : 4
}
a1
a2
c1
c2
c3
c0
a0"""