diff --git a/plugin/UltiSnips.vim b/plugin/UltiSnips.vim index 804e373..a94197e 100644 --- a/plugin/UltiSnips.vim +++ b/plugin/UltiSnips.vim @@ -135,6 +135,32 @@ function! UltiSnips_JumpForwards() return "" 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() " Map the keys correctly if g:UltiSnipsExpandTrigger == g:UltiSnipsJumpForwardTrigger diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 7261c4f..cb7d5c3 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -656,6 +656,16 @@ class SnippetManager(object): 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"): if ft in self._snippets: self._snippets[ft].clear_snippets(triggers) @@ -907,7 +917,10 @@ class SnippetManager(object): """ lineno,col = vim.current.window.cursor # 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 if self._cs: diff --git a/test.py b/test.py index 99173f8..7ba3775 100755 --- a/test.py +++ b/test.py @@ -43,6 +43,7 @@ JF = "?" # Jump forwards JB = "+" # Jump backwards LS = "@" # List snippets EX = "\t" # EXPAND +EA = "#" # Expand anonymous # Some VIM functions COMPL_KW = chr(24)+chr(14) @@ -1718,6 +1719,67 @@ class ListAllAvailable_NonDefined_NoExceptionShouldBeRaised(_ListAllSnippets): keys = "hallo qualle" + LS + "Hi" wanted = "hallo qualleHi" + +####################### +# ANONYMOUS EXPANSION # +####################### + +class _AnonBase(_VimTest): + args = "" + def _options_on(self): + self.send(":inoremap " + EA + ' =UltiSnips_Anon(' + + self.args + ')\n') + def _options_off(self): + self.send(":iunmap " + EA + ' =UltiSnips_Anon(' + + self.args + ')\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_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 # #########################