diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 26af376..a15789b 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -411,6 +411,51 @@ the '$$' trigger and passes '$$' to the function as the trigger argument. This is required in order for UltiSnips to have access to the characters typed so it can determine if the trigger matches or not. + 3.5.3 UltiSnips_SnippetDict *UltiSnips_SnippetDict* + +A third function is UltiSnips_SnippetDict which is the equivalent of +snipmate GetSnipsInCurrentScope function. +This function simply returns a vim dictionary with the snippets whose trigger +matches the current word. +This function does not add any new functionality to ultisnips directly but +allows to use third party plugins to integrate the current available snippets. + +An example of such third party plugin is SnippetCompleteSnipMate which uses +the function GetSnipsInCurrentScope to integrate the current available +snippets with user defined abbreviations and provides these and a completion +menu. +This script is located in +http://www.vim.org/scripts/script.php?script_id=4276. +Note: If you check the above website it lists two dependencies: the +SnippetComplete plugin and snipmate. +You do need the SnippetComplete plugin but you obviously don't need snipmate, +you just have to define the function GetSnipsInCurrentScope. Put the following +in your vimrc: + +function! GetSnipsInCurrentScope() + return UltiSnips_SnippetDict() +endfunction + + +As a second example on how to use this function consider the following +function and mapping definition: + +function! ExpandPossibleShorterSnippet() + if len(UltiSnips_SnippetDict()) == 1 "only one candidate... + let curr_key = keys(UltiSnips_SnippetDict())[0] + normal diw + exe "normal a" . curr_key + exe "normal a " + return 1 + endif + return 0 +endfunction +inoremap =(ExpandPossibleShorterSnippet() == 0? '': UltiSnips_ExpandSnippet()) + +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. + 3.6 Warning about missing python support *UltiSnips-python-warning* ---------------------------------------- diff --git a/plugin/UltiSnips.vim b/plugin/UltiSnips.vim index f5acf4c..cde04ef 100644 --- a/plugin/UltiSnips.vim +++ b/plugin/UltiSnips.vim @@ -145,6 +145,12 @@ function! UltiSnips_ListSnippets() return "" endfunction +function! UltiSnips_SnippetDict() + let g:current_ulti_dict = {} + exec g:_uspy "UltiSnips_Manager.list_snippets_dict()" + return g:current_ulti_dict +endfunction + function! UltiSnips_SaveLastVisualSelection() exec g:_uspy "UltiSnips_Manager.save_last_visual_selection()" return "" diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index d7c5ba0..1e35d51 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -546,6 +546,29 @@ class SnippetManager(object): self._handle_failure(self.expand_trigger) @err_to_scratch_buffer + def list_snippets_dict(self): + before, after = _vim.buf.current_line_splitted + snippets = self._snips(before, True) + + # Sort snippets alphabetically + snippets.sort(key=lambda x: x.trigger) + for snip in snippets: + description = snip.description[snip.description.find(snip.trigger) + + len(snip.trigger) + 2:] + + key = as_unicode(snip.trigger) + description = as_unicode(description) + + #remove surrounding "" or '' in snippet description if it exists + if len(description) > 2: + if description[0] == description[-1] and description[0] in ['"', "'"]: + pass + description = description[1:-1] + + _vim.command(as_unicode("let g:current_ulti_dict['{key}'] = '{val}'").format( + key=key.replace("'", "''"), val=description)) + + @err_to_scratch_buffer def list_snippets(self): before, after = _vim.buf.current_line_splitted snippets = self._snips(before, True) diff --git a/test.py b/test.py index 01f7f4b..b610149 100755 --- a/test.py +++ b/test.py @@ -2988,6 +2988,42 @@ class DeleteCurrentTabStop3_JumpAround(_VimTest): wanted = "hello\nendworld" # End: Normal mode editing #}}} + +class VerifyVimDict1(_VimTest): + """check: + correct type (4 means vim dictionary) + correct length of dictionary (in this case we have on element if the use same prefix, dictionary should have 1 element) + correct description + if the prefix is mismatched no resulting dict should have 0 elements + """ + + snippets = ('testâ', 'abc123ά', '123êabc') + keys = ('test=(type(UltiSnips_SnippetDict()) . len(UltiSnips_SnippetDict()) . ' + + 'UltiSnips_SnippetDict()["testâ"]' + ')\n' + + '=len(UltiSnips_SnippetDict())\n') + + wanted = 'test41123êabc0' + +class VerifyVimDict2(_VimTest): + """check: + can use " in trigger + """ + + snippets = ('te"stâ', 'abc123ά', '123êabc') + akey = "'te{}stâ'".format('"') + keys = ('te"=(UltiSnips_SnippetDict()[{}]'.format(akey) + ')\n') + wanted = 'te"123êabc' + +class VerifyVimDict3(_VimTest): + """check: + can use ' in trigger + """ + + snippets = ("te'stâ", 'abc123ά', '123êabc') + akey = '"te{}stâ"'.format("'") + keys = ("te'=(UltiSnips_SnippetDict()[{}]".format(akey) + ')\n') + wanted = "te'123êabc" + ########################################################################### # END OF TEST # ###########################################################################