Improve/simplify filetype handling for buffers (#745)

* Improve/simplify filetype handling for buffers

There is no need to have a FileType autocmd anymore, which for some odd
reason even had to be in "ftdetect"?!

Now it will just use the `&filetype` property from buffers, instead of
updating it on FileType events.

This allows for easier lazy-loading of UltiSnips; without this patch it
would not have updated the list of filetypes for the buffer after just
being triggered (e.g. through NeoBundle's lazy-loading).
I am using the following currently:

    NeoBundleLazy 'SirVer/ultisnips', {
      \ 'on_funcs': ['UltiSnips#ExpandSnippetOrJump']}
    inoremap <silent> <c-j> <C-R>=UltiSnips#ExpandSnippetOrJump()<cr>

By manually defining the mapping for the trigger, I can invoke it and
NeoBundle will trigger the loading of the plugin.
This commit is contained in:
Daniel Hahler 2016-09-07 14:51:16 +02:00 committed by Stanislav Seletskiy
parent dfde9b0032
commit 8d77e89d65
4 changed files with 14 additions and 44 deletions

View File

@ -1,11 +1,4 @@
if exists("b:did_autoload_ultisnips") || !exists("g:_uspy") if exists("b:did_autoload_ultisnips") || !exists("g:_uspy")
" Define no-op function, called via ftdetect/UltiSnips.vim.
" TODO(sirver): Add a test for that using a bad g:UltiSnipsPythonVersion
" setting. Without this fix moving the cursor will spam errors, with this
" it should not.
function! UltiSnips#FileTypeChanged(...)
endfunction
finish finish
endif endif
let b:did_autoload_ultisnips = 1 let b:did_autoload_ultisnips = 1
@ -53,7 +46,7 @@ function! UltiSnips#Edit(bang, ...)
endfunction endfunction
function! UltiSnips#AddFiletypes(filetypes) function! UltiSnips#AddFiletypes(filetypes)
exec g:_uspy "UltiSnips_Manager.add_buffer_filetypes('" . a:filetypes . ".all')" exec g:_uspy "UltiSnips_Manager.add_buffer_filetypes('" . a:filetypes . "')"
return "" return ""
endfunction endfunction
@ -116,13 +109,6 @@ function! UltiSnips#JumpForwards()
return "" return ""
endfunction endfunction
function! UltiSnips#FileTypeChanged(...)
exec g:_uspy "UltiSnips_Manager.reset_buffer_filetypes()"
exec g:_uspy "UltiSnips_Manager.add_buffer_filetypes('" . (a:0 ? a:1 : &ft) . "')"
return ""
endfunction
function! UltiSnips#AddSnippetWithPriority(trigger, value, description, options, filetype, priority) function! UltiSnips#AddSnippetWithPriority(trigger, value, description, options, filetype, priority)
exec g:_uspy "trigger = vim.eval(\"a:trigger\")" exec g:_uspy "trigger = vim.eval(\"a:trigger\")"
exec g:_uspy "value = vim.eval(\"a:value\")" exec g:_uspy "value = vim.eval(\"a:value\")"

View File

@ -1,17 +0,0 @@
" This has to be called before ftplugins are loaded. Therefore
" it is here in ftdetect though it maybe shouldn't
" This is necessary to prevent errors when using vim as a pager.
if exists("vimpager")
finish
endif
if has("autocmd") && &loadplugins
augroup UltiSnipsFileType
autocmd!
autocmd FileType * call UltiSnips#FileTypeChanged()
augroup END
" restore 'filetypedetect' group declaration
augroup filetypedetect
endif

View File

@ -51,6 +51,10 @@ class VimBuffer(object):
"""The bufnr() of the current buffer.""" """The bufnr() of the current buffer."""
return vim.current.buffer.number return vim.current.buffer.number
@property
def filetypes(self):
return [ft for ft in vim.eval('&filetype').split('.') if ft]
@property @property
def cursor(self): # pylint:disable=no-self-use def cursor(self): # pylint:disable=no-self-use
"""The current windows cursor. """The current windows cursor.

View File

@ -71,7 +71,7 @@ class SnippetManager(object):
self._supertab_keys = None self._supertab_keys = None
self._csnippets = [] self._csnippets = []
self._buffer_filetypes = defaultdict(lambda: ['all']) self._added_buffer_filetypes = defaultdict(lambda: [])
self._vstate = VimState() self._vstate = VimState()
self._visual_content = VisualContentPreserver() self._visual_content = VisualContentPreserver()
@ -255,15 +255,12 @@ class SnippetManager(object):
self._snippet_sources[index + 1:] self._snippet_sources[index + 1:]
break break
def reset_buffer_filetypes(self): def get_buffer_filetypes(self):
"""Reset the filetypes for the current buffer.""" return (self._added_buffer_filetypes[_vim.buf.number] +
if _vim.buf.number in self._buffer_filetypes: _vim.buf.filetypes + ['all'])
del self._buffer_filetypes[_vim.buf.number]
def add_buffer_filetypes(self, ft): def add_buffer_filetypes(self, ft):
"""Checks for changes in the list of snippet files or the contents of buf_fts = self._added_buffer_filetypes[_vim.buf.number]
the snippet files and reloads them if necessary."""
buf_fts = self._buffer_filetypes[_vim.buf.number]
idx = -1 idx = -1
for ft in ft.split('.'): for ft in ft.split('.'):
ft = ft.strip() ft = ft.strip()
@ -272,7 +269,7 @@ class SnippetManager(object):
try: try:
idx = buf_fts.index(ft) idx = buf_fts.index(ft)
except ValueError: except ValueError:
self._buffer_filetypes[_vim.buf.number].insert(idx + 1, ft) self._added_buffer_filetypes[_vim.buf.number].insert(idx + 1, ft)
idx += 1 idx += 1
@err_to_scratch_buffer.wrap @err_to_scratch_buffer.wrap
@ -570,7 +567,7 @@ class SnippetManager(object):
If partial is True, then get also return partial matches. If partial is True, then get also return partial matches.
""" """
filetypes = self._buffer_filetypes[_vim.buf.number][::-1] filetypes = self.get_buffer_filetypes()[::-1]
matching_snippets = defaultdict(list) matching_snippets = defaultdict(list)
clear_priority = None clear_priority = None
cleared = {} cleared = {}
@ -792,9 +789,9 @@ class SnippetManager(object):
filetypes.append(requested_ft) filetypes.append(requested_ft)
else: else:
if bang: if bang:
filetypes.extend(self._buffer_filetypes[_vim.buf.number]) filetypes.extend(self.get_buffer_filetypes())
else: else:
filetypes.append(self._buffer_filetypes[_vim.buf.number][0]) filetypes.append(self.get_buffer_filetypes()[0])
for ft in filetypes: for ft in filetypes:
potentials.update(find_snippet_files(ft, snippet_dir)) potentials.update(find_snippet_files(ft, snippet_dir))