Merge pull request #625 from chemzqm/location

add snippets_in_current_buffer function
This commit is contained in:
Stanislav Seletskiy 2016-01-20 22:03:46 +05:00
commit 3f2c591c3b
3 changed files with 49 additions and 5 deletions

View File

@ -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_info = {}
endif
exec g:_uspy "UltiSnips_Manager.snippets_in_current_scope(" . all . ")"
return g:current_ulti_dict
endfunction

View File

@ -470,8 +470,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.
@ -511,6 +516,27 @@ If the trigger for your snippet is lorem, you type lor, and you have no other
snippets whose trigger matches lor then hitting <C-L> 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*
----------------------------------------

View File

@ -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_info['{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