Be when to map Forward and Backwards Trigger

Only map Forward and Backwards jumping trigger while inside a snippet.
Patch by Marcelo D Montu (mMontu) with fixes by me.
This commit is contained in:
Holger Rapp 2014-02-05 19:19:16 +01:00
commit 16c46616ad
4 changed files with 56 additions and 19 deletions

View File

@ -240,6 +240,11 @@ vimrc file. >
let g:UltiSnipsJumpForwardTrigger="<tab>" let g:UltiSnipsJumpForwardTrigger="<tab>"
let g:UltiSnipsJumpBackwardTrigger="<s-tab>" let g:UltiSnipsJumpBackwardTrigger="<s-tab>"
Note that the default value for g:UltiSnipsJumpBackwardTrigger interferes with
the built-in complete function: |i_CTRL-X_CTRL-K|. A workaround is to add the
following to your vimrc file. >
inoremap <c-x><c-k> <c-x><c-k>
3.2.1 Using your own trigger functions *UltiSnips-trigger-functions* 3.2.1 Using your own trigger functions *UltiSnips-trigger-functions*
-------------------------------------- --------------------------------------
@ -1398,6 +1403,7 @@ Contributors listed in chronological order:
Sergey Alexandrov - taketwo Sergey Alexandrov - taketwo
Brian Mock - saikobee Brian Mock - saikobee
Gernot Höflechner - LFDM Gernot Höflechner - LFDM
Marcelo D Montu - mMontu
8.2 Snippets *UltiSnips-contrisnippets* 8.2 Snippets *UltiSnips-contrisnippets*

View File

@ -84,6 +84,12 @@ endif
if !exists("g:UltiSnipsSnippetDirectories") if !exists("g:UltiSnipsSnippetDirectories")
let g:UltiSnipsSnippetDirectories = [ "UltiSnips" ] let g:UltiSnipsSnippetDirectories = [ "UltiSnips" ]
endif endif
" Should UltiSnips map JumpForwardTrigger and JumpBackwardTrigger only during
" snippet expansion?
if !exists("g:UltiSnipsClearJumpTrigger")
let g:UltiSnipsClearJumpTrigger = 1
endif
" }}} " }}}
" Global Commands {{{ " Global Commands {{{
@ -204,12 +210,16 @@ function! UltiSnips_MapKeys()
else else
exec "inoremap <silent> " . g:UltiSnipsExpandTrigger . " <C-R>=UltiSnips_ExpandSnippet()<cr>" exec "inoremap <silent> " . g:UltiSnipsExpandTrigger . " <C-R>=UltiSnips_ExpandSnippet()<cr>"
exec "snoremap <silent> " . g:UltiSnipsExpandTrigger . " <Esc>:call UltiSnips_ExpandSnippet()<cr>" exec "snoremap <silent> " . g:UltiSnipsExpandTrigger . " <Esc>:call UltiSnips_ExpandSnippet()<cr>"
if g:UltiSnipsClearJumpTrigger == 0
exec "inoremap <silent> " . g:UltiSnipsJumpForwardTrigger . " <C-R>=UltiSnips_JumpForwards()<cr>" exec "inoremap <silent> " . g:UltiSnipsJumpForwardTrigger . " <C-R>=UltiSnips_JumpForwards()<cr>"
exec "snoremap <silent> " . g:UltiSnipsJumpForwardTrigger . " <Esc>:call UltiSnips_JumpForwards()<cr>" exec "snoremap <silent> " . g:UltiSnipsJumpForwardTrigger . " <Esc>:call UltiSnips_JumpForwards()<cr>"
endif endif
endif
exec 'xnoremap ' . g:UltiSnipsExpandTrigger. ' :call UltiSnips_SaveLastVisualSelection()<cr>gvs' exec 'xnoremap ' . g:UltiSnipsExpandTrigger. ' :call UltiSnips_SaveLastVisualSelection()<cr>gvs'
if g:UltiSnipsClearJumpTrigger == 0
exec "inoremap <silent> " . g:UltiSnipsJumpBackwardTrigger . " <C-R>=UltiSnips_JumpBackwards()<cr>" exec "inoremap <silent> " . g:UltiSnipsJumpBackwardTrigger . " <C-R>=UltiSnips_JumpBackwards()<cr>"
exec "snoremap <silent> " . g:UltiSnipsJumpBackwardTrigger . " <Esc>:call UltiSnips_JumpBackwards()<cr>" exec "snoremap <silent> " . g:UltiSnipsJumpBackwardTrigger . " <Esc>:call UltiSnips_JumpBackwards()<cr>"
endif
exec "inoremap <silent> " . g:UltiSnipsListSnippets . " <C-R>=UltiSnips_ListSnippets()<cr>" exec "inoremap <silent> " . g:UltiSnipsListSnippets . " <C-R>=UltiSnips_ListSnippets()<cr>"
exec "snoremap <silent> " . g:UltiSnipsListSnippets . " <Esc>:call UltiSnips_ListSnippets()<cr>" exec "snoremap <silent> " . g:UltiSnipsListSnippets . " <Esc>:call UltiSnips_ListSnippets()<cr>"
@ -217,6 +227,23 @@ function! UltiSnips_MapKeys()
snoremap <silent> <DEL> <c-g>c snoremap <silent> <DEL> <c-g>c
snoremap <silent> <c-h> <c-g>c snoremap <silent> <c-h> <c-g>c
endf endf
function! UltiSnips_MapInnerKeys()
if g:UltiSnipsExpandTrigger != g:UltiSnipsJumpForwardTrigger
exec "inoremap <buffer> <silent> " . g:UltiSnipsJumpForwardTrigger . " <C-R>=UltiSnips_JumpForwards()<cr>"
exec "snoremap <buffer> <silent> " . g:UltiSnipsJumpForwardTrigger . " <Esc>:call UltiSnips_JumpForwards()<cr>"
endif
exec "inoremap <buffer> <silent> " . g:UltiSnipsJumpBackwardTrigger . " <C-R>=UltiSnips_JumpBackwards()<cr>"
exec "snoremap <buffer> <silent> " . g:UltiSnipsJumpBackwardTrigger . " <Esc>:call UltiSnips_JumpBackwards()<cr>"
endf
function! UltiSnips_RestoreInnerKeys()
if g:UltiSnipsExpandTrigger != g:UltiSnipsJumpForwardTrigger
exec "iunmap <buffer> " . g:UltiSnipsJumpForwardTrigger
exec "sunmap <buffer> " . g:UltiSnipsJumpForwardTrigger
endif
exec "iunmap <buffer> " . g:UltiSnipsJumpBackwardTrigger
exec "sunmap <buffer> " . g:UltiSnipsJumpBackwardTrigger
endf
function! UltiSnips_CursorMoved() function! UltiSnips_CursorMoved()
exec g:_uspy "UltiSnips_Manager.cursor_moved()" exec g:_uspy "UltiSnips_Manager.cursor_moved()"

View File

@ -812,6 +812,8 @@ class SnippetManager(object):
def _current_snippet_is_done(self): def _current_snippet_is_done(self):
self._csnippets.pop() self._csnippets.pop()
if not self._csnippets and _vim.eval("g:UltiSnipsClearJumpTrigger") != "0":
_vim.command("call UltiSnips_RestoreInnerKeys()")
def _jump(self, backwards = False): def _jump(self, backwards = False):
jumped = False jumped = False
@ -944,6 +946,8 @@ class SnippetManager(object):
""" Expands the given snippet, and handles everything """ Expands the given snippet, and handles everything
that needs to be done with it. that needs to be done with it.
""" """
if _vim.eval("g:UltiSnipsClearJumpTrigger") == "1":
_vim.command("call UltiSnips_MapInnerKeys()")
# Adjust before, maybe the trigger is not the complete word # Adjust before, maybe the trigger is not the complete word
text_before = before text_before = before
if snippet.matched: if snippet.matched:

30
test.py
View File

@ -701,24 +701,24 @@ class TabStopTestJumpingRLExampleWithZeroTab_ExceptCorrectResult(_VimTest):
class TabStopTestJumpingDontJumpToEndIfThereIsTabZero_ExceptCorrectResult(_VimTest): class TabStopTestJumpingDontJumpToEndIfThereIsTabZero_ExceptCorrectResult(_VimTest):
snippets = ("hallo", "hallo $0 $1") snippets = ("hallo", "hallo $0 $1")
keys = "hallo" + EX + "Test" + JF + "Hi" + JF + JF + "du" keys = "hallo" + EX + "Test" + JF + "Hi" + JF + JF + "du"
wanted = "hallo Hidu Test" wanted = "hallo Hi" + 2*JF + "du Test"
class TabStopTestBackwardJumping_ExceptCorrectResult(_VimTest): class TabStopTestBackwardJumping_ExceptCorrectResult(_VimTest):
snippets = ("hallo", "hallo ${2:End} mitte${1:Beginning}") snippets = ("hallo", "hallo ${2:End} mitte${1:Beginning}")
keys = "hallo" + EX + "Somelengthy Text" + JF + "Hi" + JB + \ keys = "hallo" + EX + "Somelengthy Text" + JF + "Hi" + JB + \
"Lets replace it again" + JF + "Blah" + JF + JB*2 + JF "Lets replace it again" + JF + "Blah" + JF + JB*2 + JF
wanted = "hallo Blah mitteLets replace it again" wanted = "hallo Blah mitteLets replace it again" + JB*2 + JF
class TabStopTestBackwardJumping2_ExceptCorrectResult(_VimTest): class TabStopTestBackwardJumping2_ExceptCorrectResult(_VimTest):
snippets = ("hallo", "hallo $2 $1") snippets = ("hallo", "hallo $2 $1")
keys = "hallo" + EX + "Somelengthy Text" + JF + "Hi" + JB + \ keys = "hallo" + EX + "Somelengthy Text" + JF + "Hi" + JB + \
"Lets replace it again" + JF + "Blah" + JF + JB*2 + JF "Lets replace it again" + JF + "Blah" + JF + JB*2 + JF
wanted = "hallo Blah Lets replace it again" wanted = "hallo Blah Lets replace it again" + JB*2 + JF
class TabStopTestMultilineExpand_ExceptCorrectResult(_VimTest): class TabStopTestMultilineExpand_ExceptCorrectResult(_VimTest):
snippets = ("hallo", "hallo $0\nnice $1 work\n$3 $2\nSeem to work") snippets = ("hallo", "hallo $0\nnice $1 work\n$3 $2\nSeem to work")
keys ="test hallo World" + ESC + "02f i" + EX + "world" + JF + "try" + \ keys ="test hallo World" + ESC + "02f i" + EX + "world" + JF + "try" + \
JF + "test" + JF + "one more" + JF + JF JF + "test" + JF + "one more" + JF
wanted = "test hallo one more\nnice world work\n" \ wanted = "test hallo one more" + JF + "\nnice world work\n" \
"test try\nSeem to work World" "test try\nSeem to work World"
class TabStop_TSInDefaultTextRLExample_OverwriteNone_ECR(_VimTest): class TabStop_TSInDefaultTextRLExample_OverwriteNone_ECR(_VimTest):
@ -1185,12 +1185,12 @@ snip.rv = "nothing"` `!p snip.rv = a
class PythonCode_LongerTextThanSource_Chars(_VimTest): class PythonCode_LongerTextThanSource_Chars(_VimTest):
snippets = ("test", r"""hi`!p snip.rv = "a" * 100`end""") snippets = ("test", r"""hi`!p snip.rv = "a" * 100`end""")
keys = """test""" + EX + JF + "ups" keys = """test""" + EX + "ups"
wanted = "hi" + 100*"a" + "endups" wanted = "hi" + 100*"a" + "endups"
class PythonCode_LongerTextThanSource_MultiLine(_VimTest): class PythonCode_LongerTextThanSource_MultiLine(_VimTest):
snippets = ("test", r"""hi`!p snip.rv = "a" * 100 + '\n'*100 + "a"*100`end""") snippets = ("test", r"""hi`!p snip.rv = "a" * 100 + '\n'*100 + "a"*100`end""")
keys = """test""" + EX + JF + "ups" keys = """test""" + EX + "ups"
wanted = "hi" + 100*"a" + 100*"\n" + 100*"a" + "endups" wanted = "hi" + 100*"a" + 100*"\n" + 100*"a" + "endups"
class PythonCode_AccessKilledTabstop_OverwriteSecond(_VimTest): class PythonCode_AccessKilledTabstop_OverwriteSecond(_VimTest):
@ -2724,7 +2724,7 @@ class IMMoving_NoExitingEventAtEnd_ECR(_VimTest):
class IMMoving_ExitWhenOutsideRight_ECR(_VimTest): class IMMoving_ExitWhenOutsideRight_ECR(_VimTest):
snippets = ("test", r"$1 ${2:blub} ${1:Tab}") snippets = ("test", r"$1 ${2:blub} ${1:Tab}")
keys = "hello test this" + ESC + "02f i" + EX + "tab" + ARR_R + JF + "hallo" keys = "hello test this" + ESC + "02f i" + EX + "tab" + ARR_R + JF + "hallo"
wanted = "hello tab blub tab hallothis" wanted = "hello tab blub tab " + JF + "hallothis"
class IMMoving_NotExitingWhenBarelyOutsideLeft_ECR(_VimTest): class IMMoving_NotExitingWhenBarelyOutsideLeft_ECR(_VimTest):
snippets = ("test", r"${1:Hi} ${2:blub}") snippets = ("test", r"${1:Hi} ${2:blub}")
keys = "hello test this" + ESC + "02f i" + EX + "tab" + 3*ARR_L + \ keys = "hello test this" + ESC + "02f i" + EX + "tab" + 3*ARR_L + \
@ -2734,17 +2734,17 @@ class IMMoving_ExitWhenOutsideLeft_ECR(_VimTest):
snippets = ("test", r"${1:Hi} ${2:blub}") snippets = ("test", r"${1:Hi} ${2:blub}")
keys = "hello test this" + ESC + "02f i" + EX + "tab" + 4*ARR_L + \ keys = "hello test this" + ESC + "02f i" + EX + "tab" + 4*ARR_L + \
JF + "hallo" JF + "hallo"
wanted = "hellohallo tab blub this" wanted = "hello" + JF + "hallo tab blub this"
class IMMoving_ExitWhenOutsideAbove_ECR(_VimTest): class IMMoving_ExitWhenOutsideAbove_ECR(_VimTest):
snippets = ("test", "${1:Hi}\n${2:blub}") snippets = ("test", "${1:Hi}\n${2:blub}")
keys = "hello test this" + ESC + "02f i" + EX + "tab" + 1*ARR_U + JF + \ keys = "hello test this" + ESC + "02f i" + EX + "tab" + 1*ARR_U + "\n" + JF + \
"\nhallo" "hallo"
wanted = "hallo\nhello tab\nblub this" wanted = JF + "hallo\nhello tab\nblub this"
class IMMoving_ExitWhenOutsideBelow_ECR(_VimTest): class IMMoving_ExitWhenOutsideBelow_ECR(_VimTest):
snippets = ("test", "${1:Hi}\n${2:blub}") snippets = ("test", "${1:Hi}\n${2:blub}")
keys = "hello test this" + ESC + "02f i" + EX + "tab" + 2*ARR_D + JF + \ keys = "hello test this" + ESC + "02f i" + EX + "tab" + 2*ARR_D + JF + \
"testhallo\n" "testhallo\n"
wanted = "hello tab\nblub this\ntesthallo" wanted = "hello tab\nblub this\n" + JF + "testhallo"
# End: Insert Mode Moving #}}} # End: Insert Mode Moving #}}}
# Undo of Snippet insertion {{{# # Undo of Snippet insertion {{{#
class Undo_RemoveMultilineSnippet(_VimTest): class Undo_RemoveMultilineSnippet(_VimTest):
@ -2762,8 +2762,8 @@ class Undo_RemoveWholeSnippet(_VimTest):
wanted = "first line\n\n\nupsy\n\n\nthird line" wanted = "first line\n\n\nupsy\n\n\nthird line"
class JumpForward_DefSnippet(_VimTest): class JumpForward_DefSnippet(_VimTest):
snippets = ("test", "${1}\n`!p snip.rv = '\\n'.join(t[1].split())`\n\n${0:pass}") snippets = ("test", "${1}\n`!p snip.rv = '\\n'.join(t[1].split())`\n\n${0:pass}")
keys = "test" + EX + "a b c" + JF + "shallnot" + JF + "end" keys = "test" + EX + "a b c" + JF + "shallnot"
wanted = "a b c\na\nb\nc\n\nshallnotend" wanted = "a b c\na\nb\nc\n\nshallnot"
class DeleteSnippetInsertion0(_VimTest): class DeleteSnippetInsertion0(_VimTest):
snippets = ("test", "${1:hello} $1") snippets = ("test", "${1:hello} $1")
keys = "test" + EX + ESC + "Vkx" + "i\nworld\n" keys = "test" + EX + ESC + "Vkx" + "i\nworld\n"