From 7c9d0c0c712003f36db6be4ce2e42f501fa17b7c Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Mon, 10 Feb 2014 21:27:51 +0100 Subject: [PATCH] Refactored retaining of unnamed register to be done in VimState. --- plugin/UltiSnips.vim | 2 +- pythonx/UltiSnips/__init__.py | 23 ++++------------------- pythonx/UltiSnips/vim_state.py | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/plugin/UltiSnips.vim b/plugin/UltiSnips.vim index 002e6b5..ad6f747 100644 --- a/plugin/UltiSnips.vim +++ b/plugin/UltiSnips.vim @@ -256,7 +256,7 @@ function! UltiSnips_LeavingBuffer() endf function! UltiSnips_LeavingInsertMode() - exec g:_uspy "UltiSnips_Manager.restore_unnamed_register()" + exec g:_uspy "UltiSnips_Manager.leaving_insert_mode()" endfunction " }}} diff --git a/pythonx/UltiSnips/__init__.py b/pythonx/UltiSnips/__init__.py index 0f4e496..b6b8a4f 100755 --- a/pythonx/UltiSnips/__init__.py +++ b/pythonx/UltiSnips/__init__.py @@ -168,10 +168,6 @@ class SnippetManager(object): while len(self._csnippets): self._current_snippet_is_done() - # needed to retain the unnamed register at all times - self._unnamed_reg_cached = False - self._last_placeholder = None - self._reinit() @err_to_scratch_buffer @@ -439,25 +435,14 @@ class SnippetManager(object): self._current_snippet_is_done() jumped = self._jump(backwards) if jumped: - self._cache_unnamed_register() self._vstate.remember_position() + self._vstate.remember_unnamed_register(self._ctab.current_text) self._ignore_movements = True return jumped - def _cache_unnamed_register(self): - """Save the unnamed register.""" - self._unnamed_reg_cached = True - unnamed_reg = _vim.eval('@"') - if self._last_placeholder != unnamed_reg: - self._unnamed_reg_cache = unnamed_reg - self._last_placeholder = self._ctab.current_text - - def restore_unnamed_register(self): - """Restores the unnamed register from the cache.""" - if self._unnamed_reg_cached: - escaped_cache = self._unnamed_reg_cache.replace("'", "''") - _vim.command("let @\"='%s'" % escaped_cache) - self._unnamed_reg_cached = False + def leaving_insert_mode(self): + """Called whenever we leave the insert mode.""" + self._vstate.restore_unnamed_register() def _handle_failure(self, trigger): """Mainly make sure that we play well with SuperTab.""" diff --git a/pythonx/UltiSnips/vim_state.py b/pythonx/UltiSnips/vim_state.py index 0d305ae..d64254e 100755 --- a/pythonx/UltiSnips/vim_state.py +++ b/pythonx/UltiSnips/vim_state.py @@ -31,6 +31,29 @@ class VimState(object): self._poss = deque(maxlen=5) self._lvb = None + self._text_to_expect = None + self._unnamed_reg_cache = None + self._unnamed_reg_cached = False + + def remember_unnamed_register(self, text_to_expect): + """Save the unnamed register. 'text_to_expect' is text that we expect + to be currently contained in the register - this could be text from the + tabstop that was selected and might have been overwritten. We will not + cash that then.""" + self._unnamed_reg_cached = True + unnamed_reg = _vim.eval('@"') + if unnamed_reg != self._text_to_expect: + self._unnamed_reg_cache = unnamed_reg + self._text_to_expect = text_to_expect + + def restore_unnamed_register(self): + """Restores the unnamed register and forgets what we cached.""" + if not self._unnamed_reg_cached: + return + escaped_cache = self._unnamed_reg_cache.replace("'", "''") + _vim.command("let @\"='%s'" % escaped_cache) + self._unnamed_reg_cached = False + def remember_position(self): """Remember the current position as a previous pose.""" self._poss.append(VimPosition())