From a32473a5d44672b87437819586ff2a84d7a3c39d Mon Sep 17 00:00:00 2001 From: mMontu Date: Sat, 5 Oct 2013 13:19:20 -0300 Subject: [PATCH 1/6] Option for JumpTrigger only during snippet expansion --- doc/UltiSnips.txt | 14 ++++++++++++ plugin/UltiSnips.vim | 41 ++++++++++++++++++++++++++++++++---- plugin/UltiSnips/__init__.py | 2 ++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 6145bd7..c3a6f6b 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -240,6 +240,20 @@ vimrc file. > let g:UltiSnipsJumpForwardTrigger="" let g:UltiSnipsJumpBackwardTrigger="" +The mapping of g:UltiSnipsJumpForwardTrigger and g:UltiSnipsJumpBackwardTrigger +can be controlled with this option: + + *g:UltiSnipsClearJumpTrigger* +g:UltiSnipsClearJumpTrigger By default both triggers are global mappings + that are always present. By setting this + variable to 1 the plugin will define + mappings only during snippet expansions. + +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 + 3.2.1 Using your own trigger functions *UltiSnips-trigger-functions* -------------------------------------- diff --git a/plugin/UltiSnips.vim b/plugin/UltiSnips.vim index 67d0b64..3a65ac8 100644 --- a/plugin/UltiSnips.vim +++ b/plugin/UltiSnips.vim @@ -84,6 +84,12 @@ endif if !exists("g:UltiSnipsSnippetDirectories") let g:UltiSnipsSnippetDirectories = [ "UltiSnips" ] endif + +" Should UltiSnips map JumpForwardTrigger and JumpBackwardTrigger only during +" snippet expansion? +if !exists("g:UltiSnipsClearJumpTrigger") + let g:UltiSnipsClearJumpTrigger = 0 +endif " }}} " Global Commands {{{ @@ -130,12 +136,18 @@ function! CompensateForPUM() endif endfunction function! UltiSnips_ExpandSnippet() + if g:UltiSnipsClearJumpTrigger == 1 + call UltiSnips_MapInnerKeys() + endif exec g:_uspy "UltiSnips_Manager.expand()" return "" endfunction function! UltiSnips_ExpandSnippetOrJump() call CompensateForPUM() + if g:UltiSnipsClearJumpTrigger == 1 + call UltiSnips_MapInnerKeys() + endif exec g:_uspy "UltiSnips_Manager.expand_or_jump()" return "" endfunction @@ -204,12 +216,16 @@ function! UltiSnips_MapKeys() else exec "inoremap " . g:UltiSnipsExpandTrigger . " =UltiSnips_ExpandSnippet()" exec "snoremap " . g:UltiSnipsExpandTrigger . " :call UltiSnips_ExpandSnippet()" - exec "inoremap " . g:UltiSnipsJumpForwardTrigger . " =UltiSnips_JumpForwards()" - exec "snoremap " . g:UltiSnipsJumpForwardTrigger . " :call UltiSnips_JumpForwards()" + if g:UltiSnipsClearJumpTrigger == 0 + exec "inoremap " . g:UltiSnipsJumpForwardTrigger . " =UltiSnips_JumpForwards()" + exec "snoremap " . g:UltiSnipsJumpForwardTrigger . " :call UltiSnips_JumpForwards()" + endif endif exec 'xnoremap ' . g:UltiSnipsExpandTrigger. ' :call UltiSnips_SaveLastVisualSelection()gvs' - exec "inoremap " . g:UltiSnipsJumpBackwardTrigger . " =UltiSnips_JumpBackwards()" - exec "snoremap " . g:UltiSnipsJumpBackwardTrigger . " :call UltiSnips_JumpBackwards()" + if g:UltiSnipsClearJumpTrigger == 0 + exec "inoremap " . g:UltiSnipsJumpBackwardTrigger . " =UltiSnips_JumpBackwards()" + exec "snoremap " . g:UltiSnipsJumpBackwardTrigger . " :call UltiSnips_JumpBackwards()" + endif exec "inoremap " . g:UltiSnipsListSnippets . " =UltiSnips_ListSnippets()" exec "snoremap " . g:UltiSnipsListSnippets . " :call UltiSnips_ListSnippets()" @@ -217,6 +233,23 @@ function! UltiSnips_MapKeys() snoremap c snoremap c endf +function! UltiSnips_MapInnerKeys() + if g:UltiSnipsExpandTrigger != g:UltiSnipsJumpForwardTrigger + exec "inoremap " . g:UltiSnipsJumpForwardTrigger . " =UltiSnips_JumpForwards()" + exec "snoremap " . g:UltiSnipsJumpForwardTrigger . " :call UltiSnips_JumpForwards()" + endif + exec "inoremap " . g:UltiSnipsJumpBackwardTrigger . " =UltiSnips_JumpBackwards()" + exec "snoremap " . g:UltiSnipsJumpBackwardTrigger . " :call UltiSnips_JumpBackwards()" +endf +function! UltiSnips_RestoreInnerKeys() + if g:UltiSnipsExpandTrigger != g:UltiSnipsJumpForwardTrigger + exec "iunmap " . g:UltiSnipsJumpForwardTrigger + exec "sunmap " . g:UltiSnipsJumpForwardTrigger + endif + exec "iunmap " . g:UltiSnipsJumpBackwardTrigger + exec "sunmap " . g:UltiSnipsJumpBackwardTrigger +endf + function! UltiSnips_CursorMoved() exec g:_uspy "UltiSnips_Manager.cursor_moved()" diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index a171bfc..7c8bd0b 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -763,6 +763,8 @@ class SnippetManager(object): def _current_snippet_is_done(self): self._csnippets.pop() + if _vim.eval("g:UltiSnipsClearJumpTrigger") == "1": + _vim.command("call UltiSnips_RestoreInnerKeys()") def _jump(self, backwards = False): jumped = False From 6d7f52bb6e95477a750001d2de1db5629718f06e Mon Sep 17 00:00:00 2001 From: mMontu Date: Sat, 5 Oct 2013 15:59:21 -0300 Subject: [PATCH 2/6] Include handling for expansion from list_snippets --- plugin/UltiSnips.vim | 6 ------ plugin/UltiSnips/__init__.py | 2 ++ 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/plugin/UltiSnips.vim b/plugin/UltiSnips.vim index 3a65ac8..191665e 100644 --- a/plugin/UltiSnips.vim +++ b/plugin/UltiSnips.vim @@ -136,18 +136,12 @@ function! CompensateForPUM() endif endfunction function! UltiSnips_ExpandSnippet() - if g:UltiSnipsClearJumpTrigger == 1 - call UltiSnips_MapInnerKeys() - endif exec g:_uspy "UltiSnips_Manager.expand()" return "" endfunction function! UltiSnips_ExpandSnippetOrJump() call CompensateForPUM() - if g:UltiSnipsClearJumpTrigger == 1 - call UltiSnips_MapInnerKeys() - endif exec g:_uspy "UltiSnips_Manager.expand_or_jump()" return "" endfunction diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 7c8bd0b..e14b06f 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -880,6 +880,8 @@ class SnippetManager(object): """ Expands the given snippet, and handles everything 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 text_before = before if snippet.matched: From ecb7dfdf64d44743fa6279dc83c87384dea6b4c6 Mon Sep 17 00:00:00 2001 From: mMontu Date: Sun, 10 Nov 2013 21:15:24 -0200 Subject: [PATCH 3/6] Change default value/check, fix for nested snippets --- doc/UltiSnips.txt | 9 --------- plugin/UltiSnips.vim | 2 +- plugin/UltiSnips/__init__.py | 7 ++++--- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index c3a6f6b..ac4a7f9 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -240,15 +240,6 @@ vimrc file. > let g:UltiSnipsJumpForwardTrigger="" let g:UltiSnipsJumpBackwardTrigger="" -The mapping of g:UltiSnipsJumpForwardTrigger and g:UltiSnipsJumpBackwardTrigger -can be controlled with this option: - - *g:UltiSnipsClearJumpTrigger* -g:UltiSnipsClearJumpTrigger By default both triggers are global mappings - that are always present. By setting this - variable to 1 the plugin will define - mappings only during snippet expansions. - 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. > diff --git a/plugin/UltiSnips.vim b/plugin/UltiSnips.vim index 191665e..cc4451d 100644 --- a/plugin/UltiSnips.vim +++ b/plugin/UltiSnips.vim @@ -88,7 +88,7 @@ endif " Should UltiSnips map JumpForwardTrigger and JumpBackwardTrigger only during " snippet expansion? if !exists("g:UltiSnipsClearJumpTrigger") - let g:UltiSnipsClearJumpTrigger = 0 + let g:UltiSnipsClearJumpTrigger = 1 endif " }}} diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index e14b06f..88d84a1 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -763,8 +763,9 @@ class SnippetManager(object): def _current_snippet_is_done(self): self._csnippets.pop() - if _vim.eval("g:UltiSnipsClearJumpTrigger") == "1": - _vim.command("call UltiSnips_RestoreInnerKeys()") + if _vim.eval("g:UltiSnipsClearJumpTrigger") != "0": + if len(self._csnippets) == 0: + _vim.command("call UltiSnips_RestoreInnerKeys()") def _jump(self, backwards = False): jumped = False @@ -881,7 +882,7 @@ class SnippetManager(object): that needs to be done with it. """ if _vim.eval("g:UltiSnipsClearJumpTrigger") == "1": - _vim.command("call UltiSnips_MapInnerKeys()") + _vim.command("call UltiSnips_MapInnerKeys()") # Adjust before, maybe the trigger is not the complete word text_before = before if snippet.matched: From fc5d2e61dd3fe2918f19680bcdceec3b3dee506b Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Sun, 5 Jan 2014 08:01:42 +0100 Subject: [PATCH 4/6] Small refactoring. --- plugin/UltiSnips/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 174bc30..110dfde 100755 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -806,9 +806,8 @@ class SnippetManager(object): def _current_snippet_is_done(self): self._csnippets.pop() - if _vim.eval("g:UltiSnipsClearJumpTrigger") != "0": - if len(self._csnippets) == 0: - _vim.command("call UltiSnips_RestoreInnerKeys()") + if not self._csnippets and _vim.eval("g:UltiSnipsClearJumpTrigger") != "0": + _vim.command("call UltiSnips_RestoreInnerKeys()") def _jump(self, backwards = False): jumped = False @@ -1177,4 +1176,3 @@ class SnippetManager(object): UltiSnips_Manager = SnippetManager() - From a7de815f47bdaf9bb84d0d59f3ff72df0fa2d3cf Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Wed, 5 Feb 2014 17:45:45 +0100 Subject: [PATCH 5/6] Started fixing the tests that broke through the remapping change. --- test.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test.py b/test.py index d7207bf..72ff14e 100755 --- a/test.py +++ b/test.py @@ -2724,7 +2724,7 @@ class IMMoving_NoExitingEventAtEnd_ECR(_VimTest): class IMMoving_ExitWhenOutsideRight_ECR(_VimTest): snippets = ("test", r"$1 ${2:blub} ${1:Tab}") 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): snippets = ("test", r"${1:Hi} ${2:blub}") 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}") keys = "hello test this" + ESC + "02f i" + EX + "tab" + 4*ARR_L + \ JF + "hallo" - wanted = "hellohallo tab blub this" + wanted = "hello" + JF + "hallo tab blub this" class IMMoving_ExitWhenOutsideAbove_ECR(_VimTest): snippets = ("test", "${1:Hi}\n${2:blub}") - keys = "hello test this" + ESC + "02f i" + EX + "tab" + 1*ARR_U + JF + \ - "\nhallo" - wanted = "hallo\nhello tab\nblub this" + keys = "hello test this" + ESC + "02f i" + EX + "tab" + 1*ARR_U + "\n" + JF + \ + "hallo" + wanted = JF + "hallo\nhello tab\nblub this" class IMMoving_ExitWhenOutsideBelow_ECR(_VimTest): snippets = ("test", "${1:Hi}\n${2:blub}") keys = "hello test this" + ESC + "02f i" + EX + "tab" + 2*ARR_D + JF + \ "testhallo\n" - wanted = "hello tab\nblub this\ntesthallo" + wanted = "hello tab\nblub this\n" + JF + "testhallo" # End: Insert Mode Moving #}}} # Undo of Snippet insertion {{{# class Undo_RemoveMultilineSnippet(_VimTest): From ed2a0c517fac6417d91ca0be1d6b6ab205a6f0aa Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Wed, 5 Feb 2014 19:13:34 +0100 Subject: [PATCH 6/6] Fixed remaining tests. --- test.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test.py b/test.py index 72ff14e..7cacedf 100755 --- a/test.py +++ b/test.py @@ -701,24 +701,24 @@ class TabStopTestJumpingRLExampleWithZeroTab_ExceptCorrectResult(_VimTest): class TabStopTestJumpingDontJumpToEndIfThereIsTabZero_ExceptCorrectResult(_VimTest): snippets = ("hallo", "hallo $0 $1") keys = "hallo" + EX + "Test" + JF + "Hi" + JF + JF + "du" - wanted = "hallo Hidu Test" + wanted = "hallo Hi" + 2*JF + "du Test" class TabStopTestBackwardJumping_ExceptCorrectResult(_VimTest): snippets = ("hallo", "hallo ${2:End} mitte${1:Beginning}") keys = "hallo" + EX + "Somelengthy Text" + JF + "Hi" + JB + \ "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): snippets = ("hallo", "hallo $2 $1") keys = "hallo" + EX + "Somelengthy Text" + JF + "Hi" + JB + \ "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): snippets = ("hallo", "hallo $0\nnice $1 work\n$3 $2\nSeem to work") keys ="test hallo World" + ESC + "02f i" + EX + "world" + JF + "try" + \ - JF + "test" + JF + "one more" + JF + JF - wanted = "test hallo one more\nnice world work\n" \ + JF + "test" + JF + "one more" + JF + wanted = "test hallo one more" + JF + "\nnice world work\n" \ "test try\nSeem to work World" class TabStop_TSInDefaultTextRLExample_OverwriteNone_ECR(_VimTest): @@ -1185,12 +1185,12 @@ snip.rv = "nothing"` `!p snip.rv = a class PythonCode_LongerTextThanSource_Chars(_VimTest): snippets = ("test", r"""hi`!p snip.rv = "a" * 100`end""") - keys = """test""" + EX + JF + "ups" + keys = """test""" + EX + "ups" wanted = "hi" + 100*"a" + "endups" class PythonCode_LongerTextThanSource_MultiLine(_VimTest): 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" class PythonCode_AccessKilledTabstop_OverwriteSecond(_VimTest): @@ -2762,8 +2762,8 @@ class Undo_RemoveWholeSnippet(_VimTest): wanted = "first line\n\n\nupsy\n\n\nthird line" class JumpForward_DefSnippet(_VimTest): 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" - wanted = "a b c\na\nb\nc\n\nshallnotend" + keys = "test" + EX + "a b c" + JF + "shallnot" + wanted = "a b c\na\nb\nc\n\nshallnot" class DeleteSnippetInsertion0(_VimTest): snippets = ("test", "${1:hello} $1") keys = "test" + EX + ESC + "Vkx" + "i\nworld\n"