From 6a69a44154dc45d9771d3b36ea263718cf93f4ec Mon Sep 17 00:00:00 2001 From: chemzqm Date: Mon, 28 Dec 2015 20:01:26 +0800 Subject: [PATCH 1/3] add all param to UltiSnips#SnippetsInCurrentScope --- autoload/UltiSnips.vim | 8 ++++++-- pythonx/UltiSnips/snippet_manager.py | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/autoload/UltiSnips.vim b/autoload/UltiSnips.vim index 8fdf9c1..4d24cf2 100644 --- a/autoload/UltiSnips.vim +++ b/autoload/UltiSnips.vim @@ -89,9 +89,13 @@ function! UltiSnips#ListSnippets() return "" endfunction -function! UltiSnips#SnippetsInCurrentScope() +function! UltiSnips#SnippetsInCurrentScope(...) let g:current_ulti_dict = {} - exec g:_uspy "UltiSnips_Manager.snippets_in_current_scope()" + let all = get(a:, 1, 0) + if all + let g:current_ulti_dict_all = {} + endif + exec g:_uspy "UltiSnips_Manager.snippets_in_current_scope(" . all . ")" return g:current_ulti_dict endfunction diff --git a/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index 4b7c2b0..1522440 100644 --- a/pythonx/UltiSnips/snippet_manager.py +++ b/pythonx/UltiSnips/snippet_manager.py @@ -185,10 +185,10 @@ class SnippetManager(object): self._handle_failure(self.expand_trigger) @err_to_scratch_buffer - def snippets_in_current_scope(self): + def snippets_in_current_scope(self, searchAll): """Returns the snippets that could be expanded to Vim as a global variable.""" - before = _vim.buf.line_till_cursor + before = '' if searchAll else _vim.buf.line_till_cursor snippets = self._snips(before, True) # Sort snippets alphabetically @@ -197,6 +197,8 @@ class SnippetManager(object): description = snip.description[snip.description.find(snip.trigger) + len(snip.trigger) + 2:] + location = snip.location if snip.location else '' + key = as_unicode(snip.trigger) description = as_unicode(description) @@ -211,6 +213,18 @@ class SnippetManager(object): key=key.replace("'", "''"), val=description.replace("'", "''"))) + if searchAll: + _vim.command(as_unicode( + ("let g:current_ulti_dict_all['{key}'] = {{" + "'description': '{description}'," + "'location': '{location}'," + "}}")).format( + key=key.replace("'", "''"), + location=location.replace("'", "''"), + description=description.replace("'", "''"))) + + + @err_to_scratch_buffer def list_snippets(self): """Shows the snippets that could be expanded to the User and let her From 4e2e897de8b34b373fc7c2be2dcde09489ac612f Mon Sep 17 00:00:00 2001 From: chemzqm Date: Mon, 11 Jan 2016 23:58:42 +0800 Subject: [PATCH 2/3] change g:current_ulti_dict_all to g:current_ulti_dict_info --- autoload/UltiSnips.vim | 2 +- pythonx/UltiSnips/snippet_manager.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/UltiSnips.vim b/autoload/UltiSnips.vim index 4d24cf2..6000ff0 100644 --- a/autoload/UltiSnips.vim +++ b/autoload/UltiSnips.vim @@ -93,7 +93,7 @@ function! UltiSnips#SnippetsInCurrentScope(...) let g:current_ulti_dict = {} let all = get(a:, 1, 0) if all - let g:current_ulti_dict_all = {} + let g:current_ulti_dict_info = {} endif exec g:_uspy "UltiSnips_Manager.snippets_in_current_scope(" . all . ")" return g:current_ulti_dict diff --git a/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index 1522440..ffb26fe 100644 --- a/pythonx/UltiSnips/snippet_manager.py +++ b/pythonx/UltiSnips/snippet_manager.py @@ -215,7 +215,7 @@ class SnippetManager(object): if searchAll: _vim.command(as_unicode( - ("let g:current_ulti_dict_all['{key}'] = {{" + ("let g:current_ulti_dict_info['{key}'] = {{" "'description': '{description}'," "'location': '{location}'," "}}")).format( From 52ade8fed6ef01612081bb268d29ad17c4a9eec4 Mon Sep 17 00:00:00 2001 From: chemzqm Date: Tue, 12 Jan 2016 00:37:53 +0800 Subject: [PATCH 3/3] doc for all param of SnippetsInCurrentScope --- doc/UltiSnips.txt | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index c6b3ace..e531c98 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -469,8 +469,13 @@ typed so it can determine if the trigger matches or not. A third function is UltiSnips#SnippetsInCurrentScope which is the equivalent of snipmate GetSnipsInCurrentScope function. + This function simply returns a vim dictionary with the snippets whose trigger -matches the current word. +matches the current word. If you need all snippets information of current +buffer, you can simply pass 1 (which means all) as first argument of this +function, and use a global variable g:current_ulti_dict_info to get the +result (see example below). + This function does not add any new functionality to ultisnips directly but allows to use third party plugins to integrate the current available snippets. @@ -510,6 +515,27 @@ If the trigger for your snippet is lorem, you type lor, and you have no other snippets whose trigger matches lor then hitting will expand to whatever lorem expands to. +A third example on how to use this function to extract all snippets of +current buffer: > + +function! GetAllSnippets() + call UltiSnips#SnippetsInCurrentScope(1) + let list = [] + for [key, info] in items(g:current_ulti_dict_info) + let parts = split(info.location, ':') + call add(list, { + \"key": key, + \"path": parts[0], + \"linenr": parts[1], + \"description": info.description, + \}) + endfor + return list +endfunction + +The new variable g:current_ulti_dict_info is made to avoid confilct with +exists third party plugins. The definition location contains file path and +line number is also included in this variable. 3.6 Warning about missing python support *UltiSnips-python-warning* ----------------------------------------