Merged anonymous snippets feature from Ryan

This commit is contained in:
Holger Rapp 2010-12-21 13:25:35 +01:00
commit 4930e21663
4 changed files with 137 additions and 1 deletions

View File

@ -11,6 +11,9 @@ UltiSnips *snippet* *snippets* *UltiSnips*
3.1 Commands |UltiSnips-commands| 3.1 Commands |UltiSnips-commands|
3.2 Global Variables |UltiSnips-global-variables| 3.2 Global Variables |UltiSnips-global-variables|
3.3 Warning About Select Mode Mappings |UltiSnips-warning-smappings| 3.3 Warning About Select Mode Mappings |UltiSnips-warning-smappings|
3.4 Functions |UltiSnips-functions|
3.4.1 UltiSnips_AddSnippet |UltiSnips_AddSnippet|
3.4.2 UltiSnips_Anon |UltiSnips_Anon|
4. Syntax |UltiSnips-syntax| 4. Syntax |UltiSnips-syntax|
4.1 Adding Snippets |UltiSnips-adding-snippets| 4.1 Adding Snippets |UltiSnips-adding-snippets|
4.2 Plaintext Snippets |UltiSnips-plaintext-snippets| 4.2 Plaintext Snippets |UltiSnips-plaintext-snippets|
@ -172,6 +175,33 @@ this feature. For example >
will not unmap any mapping that contains the string "somePlugin" or will not unmap any mapping that contains the string "somePlugin" or
"otherPlugin" in its complete definition as listed by *:smap* . "otherPlugin" in its complete definition as listed by *:smap* .
3.4 Functions *UltiSnips-functions*
-------------
Ultisnips provides two functions for extending core functionality.
3.4.1 UltiSnips_AddSnippet *UltiSnips_AddSnippet*
The first function is UltiSnips_AddSnippet(trigger, value, description,
options, ...) which adds a new snippet to the current list of snippets with
the provided trigger, value, description, and options, see |UltiSnips-syntax|
for details on the meaning of those. All of the arguments are strings. You can
also specify the filetype for the new snippet with the final optional
argument.
3.4.2 UltiSnips_Anon *UltiSnips_Anon*
The second function is UltiSnips_Anon(value, ...), which expands an anonymous
Snippet. The snippet that gets expanded doesn't get added to the global list
of snippets, so it can't be expanded a second time unless the function is
called again. The value argument is the same as |UltiSnips_AddSnippet|. There
are also optional arguments, which are 1) the trigger, 2) the description of
the snippet, and 3) the options. All of the optional arguments are the same as
the |UltiSnips_AddSnippet| function. The description is unused right now,
whereas the trigger and options can change the way the snippet expands.
============================================================================= =============================================================================
4. SYNTAX *UltiSnips-syntax* 4. SYNTAX *UltiSnips-syntax*

View File

@ -135,6 +135,32 @@ function! UltiSnips_JumpForwards()
return "" return ""
endfunction endfunction
function! UltiSnips_AddSnippet(trigger, value, descr, options, ...)
" Takes the same arguments as SnippetManager.add_snippet:
" (trigger, value, descr, options, ft = "all", globals = None)
py << EOB
args = vim.eval("a:000")
trigger = vim.eval("a:trigger")
value = vim.eval("a:value")
descr = vim.eval("a:descr")
options = vim.eval("a:options")
UltiSnips_Manager.add_snippet(trigger, value, descr, options, *args)
EOB
return ""
endfunction
function! UltiSnips_Anon(value, ...)
" Takes the same arguments as SnippetManager.expand_anon:
" (value, trigger="", descr="", options="", globals = None)
py << EOB
args = vim.eval("a:000")
value = vim.eval("a:value")
UltiSnips_Manager.expand_anon(value, *args)
EOB
return ""
endfunction
function! UltiSnips_MapKeys() function! UltiSnips_MapKeys()
" Map the keys correctly " Map the keys correctly
if g:UltiSnipsExpandTrigger == g:UltiSnipsJumpForwardTrigger if g:UltiSnipsExpandTrigger == g:UltiSnipsJumpForwardTrigger

View File

@ -656,6 +656,16 @@ class SnippetManager(object):
Snippet(trigger, value, descr, options, globals or {}) Snippet(trigger, value, descr, options, globals or {})
) )
def expand_anon(self, value, trigger="", descr="", options="", globals=None):
if globals is None:
globals = {}
before, after = self._get_before_after()
snip = Snippet(trigger, value, descr, options, globals)
if snip.matches(before):
self._do_snippet(snip, before, after)
def clear_snippets(self, triggers = [], ft = "all"): def clear_snippets(self, triggers = [], ft = "all"):
if ft in self._snippets: if ft in self._snippets:
self._snippets[ft].clear_snippets(triggers) self._snippets[ft].clear_snippets(triggers)
@ -907,7 +917,10 @@ class SnippetManager(object):
""" """
lineno,col = vim.current.window.cursor lineno,col = vim.current.window.cursor
# Adjust before, maybe the trigger is not the complete word # Adjust before, maybe the trigger is not the complete word
text_before = before[:-len(snippet.matched)]
text_before = before
if snippet.matched:
text_before = before[:-len(snippet.matched)]
self._expect_move_wo_change = True self._expect_move_wo_change = True
if self._cs: if self._cs:

67
test.py
View File

@ -43,6 +43,7 @@ JF = "?" # Jump forwards
JB = "+" # Jump backwards JB = "+" # Jump backwards
LS = "@" # List snippets LS = "@" # List snippets
EX = "\t" # EXPAND EX = "\t" # EXPAND
EA = "#" # Expand anonymous
# Some VIM functions # Some VIM functions
COMPL_KW = chr(24)+chr(14) COMPL_KW = chr(24)+chr(14)
@ -1718,6 +1719,72 @@ class ListAllAvailable_NonDefined_NoExceptionShouldBeRaised(_ListAllSnippets):
keys = "hallo qualle" + LS + "Hi" keys = "hallo qualle" + LS + "Hi"
wanted = "hallo qualleHi" wanted = "hallo qualleHi"
#######################
# ANONYMOUS EXPANSION #
#######################
class _AnonBase(_VimTest):
args = ""
def _options_on(self):
self.send(":inoremap <silent> " + EA + ' <C-R>=UltiSnips_Anon('
+ self.args + ')<cr>\n')
def _options_off(self):
self.send(":iunmap <silent> " + EA + ' <C-R>=UltiSnips_Anon('
+ self.args + ')<cr>\n')
class Anon_NoTrigger_Simple(_AnonBase):
args = '"simple expand"'
keys = "abc" + EA
wanted = "abcsimple expand"
class Anon_NoTrigger_Multi(_AnonBase):
args = '"simple $1 expand $1 $0"'
keys = "abc" + EA + "123" + JF + "456"
wanted = "abcsimple 123 expand 123 456"
class Anon_Trigger_Multi(_AnonBase):
args = '"simple $1 expand $1 $0", "abc"'
keys = "123 abc" + EA + "123" + JF + "456"
wanted = "123 simple 123 expand 123 456"
class Anon_Trigger_Simple(_AnonBase):
args = '"simple expand", "abc"'
keys = "abc" + EA
wanted = "simple expand"
class Anon_Trigger_Twice(_AnonBase):
args = '"simple expand", "abc"'
keys = "abc" + EA + "\nabc" + EX
wanted = "simple expand\nabc" + EX
class Anon_Trigger_Opts(_AnonBase):
args = '"simple expand", ".*abc", "desc", "r"'
keys = "blah blah abc" + EA
wanted = "simple expand"
########################
# ADD SNIPPET FUNCTION #
########################
class _AddFuncBase(_VimTest):
args = ""
def _options_on(self):
self.send(":call UltiSnips_AddSnippet("
+ self.args + ')\n')
class AddFunc_Simple(_AddFuncBase):
args = '"test", "simple expand", "desc", ""'
keys = "abc test" + EX
wanted = "abc simple expand"
class AddFunc_Opt(_AddFuncBase):
args = '".*test", "simple expand", "desc", "r"'
keys = "abc test" + EX
wanted = "simple expand"
######################### #########################
# SNIPPETS FILE PARSING # # SNIPPETS FILE PARSING #
######################### #########################