From 6b12b114117c2ec2eb1f825980f8e02bb5f9ad6a Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Sat, 11 Jul 2015 17:11:04 +0200 Subject: [PATCH] Only set autocommands when there are snippets. This speeds up cursor movement when there is no snippet activated. Fixes #518. --- autoload/UltiSnips.vim | 2 -- ftdetect/UltiSnips.vim | 2 +- plugin/UltiSnips.vim | 12 -------- pythonx/UltiSnips/snippet_manager.py | 44 ++++++++++++++++++++-------- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/autoload/UltiSnips.vim b/autoload/UltiSnips.vim index 2c004d4..8394208 100644 --- a/autoload/UltiSnips.vim +++ b/autoload/UltiSnips.vim @@ -7,7 +7,6 @@ let b:did_autoload_ultisnips = 1 exec g:_uspy "import vim" exec g:_uspy "from UltiSnips import UltiSnips_Manager" -" FUNCTIONS {{{ function! s:compensate_for_pum() """ The CursorMovedI event is not triggered while the popup-menu is visible, """ and it's by this event that UltiSnips updates its vim-state. The fix is @@ -145,4 +144,3 @@ endf function! UltiSnips#LeavingInsertMode() exec g:_uspy "UltiSnips_Manager._leaving_insert_mode()" endfunction -" }}} diff --git a/ftdetect/UltiSnips.vim b/ftdetect/UltiSnips.vim index 446cf66..7c410c4 100644 --- a/ftdetect/UltiSnips.vim +++ b/ftdetect/UltiSnips.vim @@ -8,7 +8,7 @@ endif if has("autocmd") augroup UltiSnipsFileType - au! + autocmd! autocmd FileType * call UltiSnips#FileTypeChanged() augroup END diff --git a/plugin/UltiSnips.vim b/plugin/UltiSnips.vim index 7e9de94..a32b018 100644 --- a/plugin/UltiSnips.vim +++ b/plugin/UltiSnips.vim @@ -46,18 +46,6 @@ command! -bang -nargs=? -complete=customlist,UltiSnips#FileTypeComplete UltiSnip command! -nargs=1 UltiSnipsAddFiletypes :call UltiSnips#AddFiletypes() -augroup UltiSnips - au! - au CursorMovedI * call UltiSnips#CursorMoved() - au CursorMoved * call UltiSnips#CursorMoved() - - au InsertLeave * call UltiSnips#LeavingInsertMode() - - au BufLeave * call UltiSnips#LeavingBuffer() - au CmdwinEnter * call UltiSnips#LeavingBuffer() - au CmdwinLeave * call UltiSnips#LeavingBuffer() -augroup END - call UltiSnips#map_keys#MapKeys() " vim: ts=8 sts=4 sw=4 diff --git a/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index d9f95a1..4271495 100644 --- a/pythonx/UltiSnips/snippet_manager.py +++ b/pythonx/UltiSnips/snippet_manager.py @@ -83,7 +83,7 @@ class SnippetManager(object): self.expand_trigger = expand_trigger self.forward_trigger = forward_trigger self.backward_trigger = backward_trigger - self._inner_mappings_in_place = False + self._inner_state_up = False self._supertab_keys = None self._csnippets = [] @@ -269,8 +269,8 @@ class SnippetManager(object): @err_to_scratch_buffer def _cursor_moved(self): """Called whenever the cursor moved.""" - if not self._csnippets and self._inner_mappings_in_place: - self._unmap_inner_keys() + if not self._csnippets and self._inner_state_up: + self._teardown_inner_state() self._vstate.remember_position() if _vim.eval('mode()') not in 'in': return @@ -333,8 +333,9 @@ class SnippetManager(object): self._csnippets[0].update_textobjects() self._vstate.remember_buffer(self._csnippets[0]) - def _map_inner_keys(self): - """Map keys that should only be defined when a snippet is active.""" + def _setup_inner_state(self): + """Map keys and create autocommands that should only be defined when a + snippet is active.""" if self.expand_trigger != self.forward_trigger: _vim.command('inoremap ' + self.forward_trigger + ' =UltiSnips#JumpForwards()') @@ -344,11 +345,27 @@ class SnippetManager(object): ' =UltiSnips#JumpBackwards()') _vim.command('snoremap ' + self.backward_trigger + ' :call UltiSnips#JumpBackwards()') - self._inner_mappings_in_place = True - def _unmap_inner_keys(self): - """Unmap keys that should not be active when no snippet is active.""" - if not self._inner_mappings_in_place: + # Setup the autogroups. + _vim.command('augroup UltiSnips') + _vim.command('autocmd!') + _vim.command('autocmd CursorMovedI * call UltiSnips#CursorMoved()') + _vim.command('autocmd CursorMoved * call UltiSnips#CursorMoved()') + + _vim.command( + 'autocmd InsertLeave * call UltiSnips#LeavingInsertMode()') + + _vim.command('autocmd BufLeave * call UltiSnips#LeavingBuffer()') + _vim.command( + 'autocmd CmdwinEnter * call UltiSnips#LeavingBuffer()') + _vim.command( + 'autocmd CmdwinLeave * call UltiSnips#LeavingBuffer()') + _vim.command('augroup END') + self._inner_state_up = True + + def _teardown_inner_state(self): + """Reverse _setup_inner_state.""" + if not self._inner_state_up: return try: if self.expand_trigger != self.forward_trigger: @@ -356,7 +373,10 @@ class SnippetManager(object): _vim.command('sunmap %s' % self.forward_trigger) _vim.command('iunmap %s' % self.backward_trigger) _vim.command('sunmap %s' % self.backward_trigger) - self._inner_mappings_in_place = False + _vim.command('augroup UltiSnips') + _vim.command('autocmd!') + _vim.command('augroup END') + self._inner_state_up = False except _vim.error: # This happens when a preview window was opened. This issues # CursorMoved, but not BufLeave. We have no way to unmap, until we @@ -402,7 +422,7 @@ class SnippetManager(object): """The current snippet should be terminated.""" self._csnippets.pop() if not self._csnippets: - self._unmap_inner_keys() + self._teardown_inner_state() def _jump(self, backwards=False): """Helper method that does the actual jump.""" @@ -530,7 +550,7 @@ class SnippetManager(object): def _do_snippet(self, snippet, before): """Expands the given snippet, and handles everything that needs to be done with it.""" - self._map_inner_keys() + self._setup_inner_state() # Adjust before, maybe the trigger is not the complete word text_before = before