Skip to content

Commit

Permalink
Improve/simplify filetype handling for buffers (#745)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
blueyed authored and seletskiy committed Sep 7, 2016
1 parent dfde9b0 commit 8d77e89
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 44 deletions.
16 changes: 1 addition & 15 deletions autoload/UltiSnips.vim
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
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
endif
let b:did_autoload_ultisnips = 1
Expand Down Expand Up @@ -53,7 +46,7 @@ function! UltiSnips#Edit(bang, ...)
endfunction

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 ""
endfunction

Expand Down Expand Up @@ -116,13 +109,6 @@ function! UltiSnips#JumpForwards()
return ""
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)
exec g:_uspy "trigger = vim.eval(\"a:trigger\")"
exec g:_uspy "value = vim.eval(\"a:value\")"
Expand Down
17 changes: 0 additions & 17 deletions ftdetect/UltiSnips.vim

This file was deleted.

4 changes: 4 additions & 0 deletions pythonx/UltiSnips/_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def number(self): # pylint:disable=no-self-use
"""The bufnr() of the current buffer."""
return vim.current.buffer.number

@property
def filetypes(self):
return [ft for ft in vim.eval('&filetype').split('.') if ft]

@property
def cursor(self): # pylint:disable=no-self-use
"""The current windows cursor.
Expand Down
21 changes: 9 additions & 12 deletions pythonx/UltiSnips/snippet_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, expand_trigger, forward_trigger, backward_trigger):
self._supertab_keys = None

self._csnippets = []
self._buffer_filetypes = defaultdict(lambda: ['all'])
self._added_buffer_filetypes = defaultdict(lambda: [])

self._vstate = VimState()
self._visual_content = VisualContentPreserver()
Expand Down Expand Up @@ -255,15 +255,12 @@ def unregister_snippet_source(self, name):
self._snippet_sources[index + 1:]
break

def reset_buffer_filetypes(self):
"""Reset the filetypes for the current buffer."""
if _vim.buf.number in self._buffer_filetypes:
del self._buffer_filetypes[_vim.buf.number]
def get_buffer_filetypes(self):
return (self._added_buffer_filetypes[_vim.buf.number] +
_vim.buf.filetypes + ['all'])

def add_buffer_filetypes(self, ft):
"""Checks for changes in the list of snippet files or the contents of
the snippet files and reloads them if necessary."""
buf_fts = self._buffer_filetypes[_vim.buf.number]
buf_fts = self._added_buffer_filetypes[_vim.buf.number]
idx = -1
for ft in ft.split('.'):
ft = ft.strip()
Expand All @@ -272,7 +269,7 @@ def add_buffer_filetypes(self, ft):
try:
idx = buf_fts.index(ft)
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

@err_to_scratch_buffer.wrap
Expand Down Expand Up @@ -570,7 +567,7 @@ def _snips(self, before, partial, autotrigger_only=False):
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)
clear_priority = None
cleared = {}
Expand Down Expand Up @@ -792,9 +789,9 @@ def _get_file_to_edit(self, snippet_dir, requested_ft, bang,
filetypes.append(requested_ft)
else:
if bang:
filetypes.extend(self._buffer_filetypes[_vim.buf.number])
filetypes.extend(self.get_buffer_filetypes())
else:
filetypes.append(self._buffer_filetypes[_vim.buf.number][0])
filetypes.append(self.get_buffer_filetypes()[0])

for ft in filetypes:
potentials.update(find_snippet_files(ft, snippet_dir))
Expand Down

6 comments on commit 8d77e89

@xusiyuan841028
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blueyed From this commit, the integration with YouCompleteMe is broken, and the complete menu doesn't contains any snips. Could you re-review the code of this PR to resolve the issue of working together with YCM?

commit: dfde9b0
image

commit: 8d77e89
image

@blueyed
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #748.
The PR for YCM is pending.

@noscripter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit cause following errors:

Error detected while processing FileType Auto commands for "*":
E117: Unknown function: UltiSnips#FileTypeChanged

@blueyed
Copy link
Contributor Author

@blueyed blueyed commented on 8d77e89 Sep 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noscripter
The function has been removed, because it is not required anymore.
Are you defining the autocmd yourself, or is it defined from another plugin?

@noscripter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I searched in all my $HOME/.vim/bundle and find nothing called UltiSnips#FileTypeChanged. Everything is fine after I reset from this commit.

@blueyed
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noscripter
What does :verb au FileType * say?

Please sign in to comment.