From f80414fac551fdcb4335afc4d170aeadb873d9d3 Mon Sep 17 00:00:00 2001 From: Stanislav Seletskiy Date: Wed, 17 Jun 2015 22:56:46 +0600 Subject: [PATCH] prototype of autotrigger --- autoload/UltiSnips.vim | 5 ++++ pythonx/UltiSnips/snippet/source/_base.py | 4 +-- .../snippet/source/_snippet_dictionary.py | 5 +++- pythonx/UltiSnips/snippet_manager.py | 29 ++++++++++++++++--- test/test_UltiSnipFunc.py | 2 +- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/autoload/UltiSnips.vim b/autoload/UltiSnips.vim index 8394208..79ff2f4 100644 --- a/autoload/UltiSnips.vim +++ b/autoload/UltiSnips.vim @@ -144,3 +144,8 @@ endf function! UltiSnips#LeavingInsertMode() exec g:_uspy "UltiSnips_Manager._leaving_insert_mode()" endfunction + +function! UltiSnips#TrackChange() + exec g:_uspy "UltiSnips_Manager._track_change()" +endfunction +" }}} diff --git a/pythonx/UltiSnips/snippet/source/_base.py b/pythonx/UltiSnips/snippet/source/_base.py index 71ecf5c..50a68fc 100644 --- a/pythonx/UltiSnips/snippet/source/_base.py +++ b/pythonx/UltiSnips/snippet/source/_base.py @@ -28,7 +28,7 @@ class SnippetSource(object): deep_extends = self.get_deep_extends(base_filetypes) return [ft for ft in deep_extends if ft in self._snippets] - def get_snippets(self, filetypes, before, possible): + def get_snippets(self, filetypes, before, possible, autotrigger_only): """Returns the snippets for all 'filetypes' (in order) and their parents matching the text 'before'. If 'possible' is true, a partial match is enough. Base classes can override this method to provide means @@ -40,7 +40,7 @@ class SnippetSource(object): result = [] for ft in self._get_existing_deep_extends(filetypes): snips = self._snippets[ft] - result.extend(snips.get_matching_snippets(before, possible)) + result.extend(snips.get_matching_snippets(before, possible, autotrigger_only)) return result def get_clear_priority(self, filetypes): diff --git a/pythonx/UltiSnips/snippet/source/_snippet_dictionary.py b/pythonx/UltiSnips/snippet/source/_snippet_dictionary.py index a8b3d65..e334ac6 100644 --- a/pythonx/UltiSnips/snippet/source/_snippet_dictionary.py +++ b/pythonx/UltiSnips/snippet/source/_snippet_dictionary.py @@ -16,13 +16,16 @@ class SnippetDictionary(object): """Add 'snippet' to this dictionary.""" self._snippets.append(snippet) - def get_matching_snippets(self, trigger, potentially): + def get_matching_snippets(self, trigger, potentially, autotrigger_only): """Returns all snippets matching the given trigger. If 'potentially' is true, returns all that could_match(). """ all_snippets = self._snippets + if autotrigger_only: + all_snippets = [s for s in all_snippets if s.has_option('A')] + if not potentially: return [s for s in all_snippets if s.matches(trigger)] else: diff --git a/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index f144fbf..04aba08 100644 --- a/pythonx/UltiSnips/snippet_manager.py +++ b/pythonx/UltiSnips/snippet_manager.py @@ -100,6 +100,8 @@ class SnippetManager(object): self._snip_expanded_in_action = False self._inside_action = False + self._last_inserted_char = '' + self._added_snippets_source = AddedSnippetsSource() self.register_snippet_source('ultisnips_files', UltiSnipsFileSource()) self.register_snippet_source('added', self._added_snippets_source) @@ -363,6 +365,8 @@ class SnippetManager(object): _vim.command('autocmd!') _vim.command('autocmd CursorMovedI * call UltiSnips#CursorMoved()') _vim.command('autocmd CursorMoved * call UltiSnips#CursorMoved()') + _vim.command('autocmd InsertCharPre * call UltiSnips#TrackChange()') + _vim.command('autocmd TextChangedI * call UltiSnips#TrackChange()') _vim.command( 'autocmd InsertLeave * call UltiSnips#LeavingInsertMode()') @@ -541,7 +545,7 @@ class SnippetManager(object): elif feedkey: _vim.command('return %s' % _vim.escape(feedkey)) - def _snips(self, before, partial): + def _snips(self, before, partial, autotrigger_only=False): """Returns all the snippets for the given text before the cursor. If partial is True, then get also return partial matches. @@ -565,7 +569,14 @@ class SnippetManager(object): cleared[key] = value for _, source in self._snippet_sources: - for snippet in source.get_snippets(filetypes, before, partial): + possible_snippets = source.get_snippets( + filetypes, + before, + partial, + autotrigger_only + ) + + for snippet in possible_snippets: if ((clear_priority is None or snippet.priority > clear_priority) and (snippet.trigger not in cleared or snippet.priority > cleared[snippet.trigger])): @@ -667,10 +678,10 @@ class SnippetManager(object): self._snip_expanded_in_action = True - def _try_expand(self): + def _try_expand(self, autotrigger_only=False): """Try to expand a snippet in the current place.""" before = _vim.buf.line_till_cursor - snippets = self._snips(before, False) + snippets = self._snips(before, False, autotrigger_only) if snippets: # prefer snippets with context if any snippets_with_context = [s for s in snippets if s.context] @@ -765,6 +776,16 @@ class SnippetManager(object): finally: self._inside_action = old_flag + def _track_change(self): + inserted_char = _vim.eval('v:char') + if inserted_char == '': + before = _vim.buf.line_till_cursor + if before and before[-1] == self._last_inserted_char: + self._try_expand(autotrigger_only=True) + else: + self._last_inserted_char = inserted_char + + UltiSnips_Manager = SnippetManager( # pylint:disable=invalid-name vim.eval('g:UltiSnipsExpandTrigger'), vim.eval('g:UltiSnipsJumpForwardTrigger'), diff --git a/test/test_UltiSnipFunc.py b/test/test_UltiSnipFunc.py index 6e6a06e..f59c275 100644 --- a/test/test_UltiSnipFunc.py +++ b/test/test_UltiSnipFunc.py @@ -154,7 +154,7 @@ from UltiSnips.snippet.source import SnippetSource from UltiSnips.snippet.definition import UltiSnipsSnippetDefinition class MySnippetSource(SnippetSource): - def get_snippets(self, filetypes, before, possible): + def get_snippets(self, filetypes, before, possible, autotrigger_only): if before.endswith('blumba'): return [ UltiSnipsSnippetDefinition(