Only set autocommands when there are snippets.

This speeds up cursor movement when there is no snippet activated.

Fixes #518.
This commit is contained in:
Holger Rapp 2015-07-11 17:11:04 +02:00
parent 2ed217a059
commit 6b12b11411
4 changed files with 33 additions and 27 deletions

View File

@ -7,7 +7,6 @@ let b:did_autoload_ultisnips = 1
exec g:_uspy "import vim" exec g:_uspy "import vim"
exec g:_uspy "from UltiSnips import UltiSnips_Manager" exec g:_uspy "from UltiSnips import UltiSnips_Manager"
" FUNCTIONS {{{
function! s:compensate_for_pum() function! s:compensate_for_pum()
""" The CursorMovedI event is not triggered while the popup-menu is visible, """ 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 """ and it's by this event that UltiSnips updates its vim-state. The fix is
@ -145,4 +144,3 @@ endf
function! UltiSnips#LeavingInsertMode() function! UltiSnips#LeavingInsertMode()
exec g:_uspy "UltiSnips_Manager._leaving_insert_mode()" exec g:_uspy "UltiSnips_Manager._leaving_insert_mode()"
endfunction endfunction
" }}}

View File

@ -8,7 +8,7 @@ endif
if has("autocmd") if has("autocmd")
augroup UltiSnipsFileType augroup UltiSnipsFileType
au! autocmd!
autocmd FileType * call UltiSnips#FileTypeChanged() autocmd FileType * call UltiSnips#FileTypeChanged()
augroup END augroup END

View File

@ -46,18 +46,6 @@ command! -bang -nargs=? -complete=customlist,UltiSnips#FileTypeComplete UltiSnip
command! -nargs=1 UltiSnipsAddFiletypes :call UltiSnips#AddFiletypes(<q-args>) command! -nargs=1 UltiSnipsAddFiletypes :call UltiSnips#AddFiletypes(<q-args>)
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() call UltiSnips#map_keys#MapKeys()
" vim: ts=8 sts=4 sw=4 " vim: ts=8 sts=4 sw=4

View File

@ -83,7 +83,7 @@ class SnippetManager(object):
self.expand_trigger = expand_trigger self.expand_trigger = expand_trigger
self.forward_trigger = forward_trigger self.forward_trigger = forward_trigger
self.backward_trigger = backward_trigger self.backward_trigger = backward_trigger
self._inner_mappings_in_place = False self._inner_state_up = False
self._supertab_keys = None self._supertab_keys = None
self._csnippets = [] self._csnippets = []
@ -269,8 +269,8 @@ class SnippetManager(object):
@err_to_scratch_buffer @err_to_scratch_buffer
def _cursor_moved(self): def _cursor_moved(self):
"""Called whenever the cursor moved.""" """Called whenever the cursor moved."""
if not self._csnippets and self._inner_mappings_in_place: if not self._csnippets and self._inner_state_up:
self._unmap_inner_keys() self._teardown_inner_state()
self._vstate.remember_position() self._vstate.remember_position()
if _vim.eval('mode()') not in 'in': if _vim.eval('mode()') not in 'in':
return return
@ -333,8 +333,9 @@ class SnippetManager(object):
self._csnippets[0].update_textobjects() self._csnippets[0].update_textobjects()
self._vstate.remember_buffer(self._csnippets[0]) self._vstate.remember_buffer(self._csnippets[0])
def _map_inner_keys(self): def _setup_inner_state(self):
"""Map keys that should only be defined when a snippet is active.""" """Map keys and create autocommands that should only be defined when a
snippet is active."""
if self.expand_trigger != self.forward_trigger: if self.expand_trigger != self.forward_trigger:
_vim.command('inoremap <buffer> <silent> ' + self.forward_trigger + _vim.command('inoremap <buffer> <silent> ' + self.forward_trigger +
' <C-R>=UltiSnips#JumpForwards()<cr>') ' <C-R>=UltiSnips#JumpForwards()<cr>')
@ -344,11 +345,27 @@ class SnippetManager(object):
' <C-R>=UltiSnips#JumpBackwards()<cr>') ' <C-R>=UltiSnips#JumpBackwards()<cr>')
_vim.command('snoremap <buffer> <silent> ' + self.backward_trigger + _vim.command('snoremap <buffer> <silent> ' + self.backward_trigger +
' <Esc>:call UltiSnips#JumpBackwards()<cr>') ' <Esc>:call UltiSnips#JumpBackwards()<cr>')
self._inner_mappings_in_place = True
def _unmap_inner_keys(self): # Setup the autogroups.
"""Unmap keys that should not be active when no snippet is active.""" _vim.command('augroup UltiSnips')
if not self._inner_mappings_in_place: _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 return
try: try:
if self.expand_trigger != self.forward_trigger: if self.expand_trigger != self.forward_trigger:
@ -356,7 +373,10 @@ class SnippetManager(object):
_vim.command('sunmap <buffer> %s' % self.forward_trigger) _vim.command('sunmap <buffer> %s' % self.forward_trigger)
_vim.command('iunmap <buffer> %s' % self.backward_trigger) _vim.command('iunmap <buffer> %s' % self.backward_trigger)
_vim.command('sunmap <buffer> %s' % self.backward_trigger) _vim.command('sunmap <buffer> %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: except _vim.error:
# This happens when a preview window was opened. This issues # This happens when a preview window was opened. This issues
# CursorMoved, but not BufLeave. We have no way to unmap, until we # 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.""" """The current snippet should be terminated."""
self._csnippets.pop() self._csnippets.pop()
if not self._csnippets: if not self._csnippets:
self._unmap_inner_keys() self._teardown_inner_state()
def _jump(self, backwards=False): def _jump(self, backwards=False):
"""Helper method that does the actual jump.""" """Helper method that does the actual jump."""
@ -530,7 +550,7 @@ class SnippetManager(object):
def _do_snippet(self, snippet, before): def _do_snippet(self, snippet, before):
"""Expands the given snippet, and handles everything that needs to be """Expands the given snippet, and handles everything that needs to be
done with it.""" done with it."""
self._map_inner_keys() self._setup_inner_state()
# Adjust before, maybe the trigger is not the complete word # Adjust before, maybe the trigger is not the complete word
text_before = before text_before = before