Fixes Bug #427676.
- Adds the idea of anonymous snippets with new method on SnippetManager to expand a snippet without adding it. - Adds two new functions for adding new snippets, and expanding anonymous snippets. - Adds tests for the above functionality.
This commit is contained in:
parent
8c71d10ad2
commit
98b396ff24
@ -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
|
||||||
|
@ -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,6 +917,9 @@ 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
|
||||||
|
if snippet.matched:
|
||||||
text_before = before[:-len(snippet.matched)]
|
text_before = before[:-len(snippet.matched)]
|
||||||
|
|
||||||
self._expect_move_wo_change = True
|
self._expect_move_wo_change = True
|
||||||
|
62
test.py
62
test.py
@ -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,67 @@ 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_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 #
|
||||||
#########################
|
#########################
|
||||||
|
Loading…
x
Reference in New Issue
Block a user