prototype of autotrigger

This commit is contained in:
Stanislav Seletskiy 2015-06-17 22:56:46 +06:00
parent 7c74aa7686
commit f80414fac5
5 changed files with 37 additions and 8 deletions

View File

@ -144,3 +144,8 @@ 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
function! UltiSnips#TrackChange()
exec g:_uspy "UltiSnips_Manager._track_change()"
endfunction
" }}}

View File

@ -28,7 +28,7 @@ class SnippetSource(object):
deep_extends = self.get_deep_extends(base_filetypes) deep_extends = self.get_deep_extends(base_filetypes)
return [ft for ft in deep_extends if ft in self._snippets] 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 """Returns the snippets for all 'filetypes' (in order) and their
parents matching the text 'before'. If 'possible' is true, a partial parents matching the text 'before'. If 'possible' is true, a partial
match is enough. Base classes can override this method to provide means match is enough. Base classes can override this method to provide means
@ -40,7 +40,7 @@ class SnippetSource(object):
result = [] result = []
for ft in self._get_existing_deep_extends(filetypes): for ft in self._get_existing_deep_extends(filetypes):
snips = self._snippets[ft] snips = self._snippets[ft]
result.extend(snips.get_matching_snippets(before, possible)) result.extend(snips.get_matching_snippets(before, possible, autotrigger_only))
return result return result
def get_clear_priority(self, filetypes): def get_clear_priority(self, filetypes):

View File

@ -16,13 +16,16 @@ class SnippetDictionary(object):
"""Add 'snippet' to this dictionary.""" """Add 'snippet' to this dictionary."""
self._snippets.append(snippet) 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. """Returns all snippets matching the given trigger.
If 'potentially' is true, returns all that could_match(). If 'potentially' is true, returns all that could_match().
""" """
all_snippets = self._snippets all_snippets = self._snippets
if autotrigger_only:
all_snippets = [s for s in all_snippets if s.has_option('A')]
if not potentially: if not potentially:
return [s for s in all_snippets if s.matches(trigger)] return [s for s in all_snippets if s.matches(trigger)]
else: else:

View File

@ -100,6 +100,8 @@ class SnippetManager(object):
self._snip_expanded_in_action = False self._snip_expanded_in_action = False
self._inside_action = False self._inside_action = False
self._last_inserted_char = ''
self._added_snippets_source = AddedSnippetsSource() self._added_snippets_source = AddedSnippetsSource()
self.register_snippet_source('ultisnips_files', UltiSnipsFileSource()) self.register_snippet_source('ultisnips_files', UltiSnipsFileSource())
self.register_snippet_source('added', self._added_snippets_source) self.register_snippet_source('added', self._added_snippets_source)
@ -363,6 +365,8 @@ class SnippetManager(object):
_vim.command('autocmd!') _vim.command('autocmd!')
_vim.command('autocmd CursorMovedI * call UltiSnips#CursorMoved()') _vim.command('autocmd CursorMovedI * call UltiSnips#CursorMoved()')
_vim.command('autocmd CursorMoved * 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( _vim.command(
'autocmd InsertLeave * call UltiSnips#LeavingInsertMode()') 'autocmd InsertLeave * call UltiSnips#LeavingInsertMode()')
@ -541,7 +545,7 @@ class SnippetManager(object):
elif feedkey: elif feedkey:
_vim.command('return %s' % _vim.escape(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. """Returns all the snippets for the given text before the cursor.
If partial is True, then get also return partial matches. If partial is True, then get also return partial matches.
@ -565,7 +569,14 @@ class SnippetManager(object):
cleared[key] = value cleared[key] = value
for _, source in self._snippet_sources: 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) if ((clear_priority is None or snippet.priority > clear_priority)
and (snippet.trigger not in cleared or and (snippet.trigger not in cleared or
snippet.priority > cleared[snippet.trigger])): snippet.priority > cleared[snippet.trigger])):
@ -667,10 +678,10 @@ class SnippetManager(object):
self._snip_expanded_in_action = True 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.""" """Try to expand a snippet in the current place."""
before = _vim.buf.line_till_cursor before = _vim.buf.line_till_cursor
snippets = self._snips(before, False) snippets = self._snips(before, False, autotrigger_only)
if snippets: if snippets:
# prefer snippets with context if any # prefer snippets with context if any
snippets_with_context = [s for s in snippets if s.context] snippets_with_context = [s for s in snippets if s.context]
@ -765,6 +776,16 @@ class SnippetManager(object):
finally: finally:
self._inside_action = old_flag 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 UltiSnips_Manager = SnippetManager( # pylint:disable=invalid-name
vim.eval('g:UltiSnipsExpandTrigger'), vim.eval('g:UltiSnipsExpandTrigger'),
vim.eval('g:UltiSnipsJumpForwardTrigger'), vim.eval('g:UltiSnipsJumpForwardTrigger'),

View File

@ -154,7 +154,7 @@ from UltiSnips.snippet.source import SnippetSource
from UltiSnips.snippet.definition import UltiSnipsSnippetDefinition from UltiSnips.snippet.definition import UltiSnipsSnippetDefinition
class MySnippetSource(SnippetSource): class MySnippetSource(SnippetSource):
def get_snippets(self, filetypes, before, possible): def get_snippets(self, filetypes, before, possible, autotrigger_only):
if before.endswith('blumba'): if before.endswith('blumba'):
return [ return [
UltiSnipsSnippetDefinition( UltiSnipsSnippetDefinition(