Skip to content

Commit

Permalink
fix #1165: insert mode lag (#1552)
Browse files Browse the repository at this point in the history
* fix #1165: insert mode lag

Ultisnips was traversing the whole runtimepath every time an expansion
was attempted, something that happens for every key press since there is
autotrigger.

Even without autotrigger, traversal was done at every snippet expansion,
which caused slowdowns in that case.

With this change, traversal happens only the first time, or after
a source must be refreshed (because a snippets file has been updated).
  • Loading branch information
mg979 committed Mar 15, 2024
1 parent 808114c commit 49dc8cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
7 changes: 7 additions & 0 deletions autoload/UltiSnips.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ if exists("b:did_autoload_ultisnips")
endif
let b:did_autoload_ultisnips = 1

" Ensure snippets are loaded for current buffer
au UltiSnips_AutoTrigger FileType,BufEnter * call UltiSnips#CheckFiletype()

" Also import vim as we expect it to be imported in many places.
py3 import vim
py3 from UltiSnips import UltiSnips_Manager
Expand Down Expand Up @@ -188,6 +191,10 @@ function! UltiSnips#TrackChange() abort
py3 UltiSnips_Manager._track_change()
endfunction

function! UltiSnips#CheckFiletype() abort
py3 UltiSnips_Manager._check_filetype(vim.eval('&ft'))
endfunction

function! UltiSnips#RefreshSnippets() abort
py3 UltiSnips_Manager._refresh_snippets()
endfunction
Expand Down
1 change: 1 addition & 0 deletions pythonx/UltiSnips/snippet/source/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SnippetSource:
def __init__(self):
self._snippets = defaultdict(SnippetDictionary)
self._extends = defaultdict(set)
self.must_ensure = True

def ensure(self, filetypes):
"""Ensures that snippets are loaded."""
Expand Down
8 changes: 5 additions & 3 deletions pythonx/UltiSnips/snippet/source/file/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ def __init__(self):
SnippetSource.__init__(self)

def ensure(self, filetypes):
for ft in self.get_deep_extends(filetypes):
if self._needs_update(ft):
self._load_snippets_for(ft)
if self.must_ensure:
for ft in self.get_deep_extends(filetypes):
if self._needs_update(ft):
self._load_snippets_for(ft)
self.must_ensure = False

def refresh(self):
self.__init__()
Expand Down
10 changes: 10 additions & 0 deletions pythonx/UltiSnips/snippet_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def __init__(self, expand_trigger, forward_trigger, backward_trigger):
self._visual_content = VisualContentPreserver()

self._snippet_sources = []
self._filetypes = []

self._snip_expanded_in_action = False
self._inside_action = False
Expand Down Expand Up @@ -984,6 +985,15 @@ def _refresh_snippets(self):
source.refresh()


@err_to_scratch_buffer.wrap
def _check_filetype(self, ft):
"""Ensure snippets are loaded for the current filetype."""
if ft not in self._filetypes:
self._filetypes.append(ft)
for _, source in self._snippet_sources:
source.must_ensure = True


UltiSnips_Manager = SnippetManager( # pylint:disable=invalid-name
vim.eval("g:UltiSnipsExpandTrigger"),
vim.eval("g:UltiSnipsJumpForwardTrigger"),
Expand Down

0 comments on commit 49dc8cb

Please sign in to comment.