diff --git a/autoload/UltiSnips.vim b/autoload/UltiSnips.vim index 75933920..15a1b3e1 100644 --- a/autoload/UltiSnips.vim +++ b/autoload/UltiSnips.vim @@ -87,6 +87,12 @@ function! UltiSnips#ExpandSnippetOrJump() abort return "" endfunction +function! UltiSnips#JumpOrExpandSnippet() abort + call s:compensate_for_pum() + py3 UltiSnips_Manager.jump_or_expand() + return "" +endfunction + function! UltiSnips#ListSnippets() abort py3 UltiSnips_Manager.list_snippets() return "" diff --git a/autoload/UltiSnips/map_keys.vim b/autoload/UltiSnips/map_keys.vim index 821e611e..0f3321ec 100644 --- a/autoload/UltiSnips/map_keys.vim +++ b/autoload/UltiSnips/map_keys.vim @@ -54,7 +54,13 @@ if !exists("g:UltiSnipsEnableSnipMate") endif function! UltiSnips#map_keys#MapKeys() abort - if g:UltiSnipsExpandTrigger == g:UltiSnipsJumpForwardTrigger + if exists("g:UltiSnipsExpandOrJumpTrigger") + exec "inoremap " . g:UltiSnipsExpandOrJumpTrigger . " =UltiSnips#ExpandSnippetOrJump()" + exec "snoremap " . g:UltiSnipsExpandOrJumpTrigger . " :call UltiSnips#ExpandSnippetOrJump()" + elseif exists("g:UltiSnipsJumpOrExpandTrigger") + exec "inoremap " . g:UltiSnipsJumpOrExpandTrigger . " =UltiSnips#JumpOrExpandSnippet()" + exec "snoremap " . g:UltiSnipsJumpOrExpandTrigger . " :call UltiSnips#JumpOrExpandSnippet()" + elseif g:UltiSnipsExpandTrigger == g:UltiSnipsJumpForwardTrigger exec "inoremap " . g:UltiSnipsExpandTrigger . " =UltiSnips#ExpandSnippetOrJump()" exec "snoremap " . g:UltiSnipsExpandTrigger . " :call UltiSnips#ExpandSnippetOrJump()" else diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 5adbcea7..22f984ca 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -246,6 +246,7 @@ The priority will then be rails -> ruby -> programming -> all. *g:UltiSnipsExpandTrigger* *g:UltiSnipsListSnippets* *g:UltiSnipsJumpForwardTrigger* *g:UltiSnipsJumpBackwardTrigger* + *g:UltiSnipsExpandOrJumpTrigger* *g:UltiSnipsJumpOrExpandTrigger* You can define the keys used to trigger UltiSnips actions by setting global variables. Variables define the keys used to expand a snippet, jump forward @@ -267,27 +268,45 @@ following to your vimrc file or switching to a plugin like Supertab or YouCompleteMe. > inoremap +If you prefer to use the same trigger for expanding snippets and jumping +forward, g:UltiSnipsExpandOrJumpTrigger and g:UltiSnipsJumpOrExpandTrigger +variables are what you want. Try to set g:UltiSnipsExpandOrJumpTrigger if +you give priority to expanding snippets. It'll expand snippets first when +you are in a position where you can both expand snippets and jump forward. > + let g:UltiSnipsExpandOrJumpTrigger = "" + +Or else, if you have a preference for jumping forward first, set +g:UltiSnipsJumpOrExpandTrigger instead. > + let g:UltiSnipsJumpOrExpandTrigger = "" + +If both g:UltiSnipsExpandOrJumpTrigger and g:UltiSnipsJumpOrExpandTrigger +is set, g:UltiSnipsExpandOrJumpTrigger variable will take effects. + 3.2.2 Using your own trigger functions *UltiSnips-trigger-functions* -For advanced users there are four functions that you can map directly to a +For advanced users there are functions that you can map directly to a key and that correspond to some of the triggers previously defined: g:UltiSnipsExpandTrigger <--> UltiSnips#ExpandSnippet g:UltiSnipsJumpForwardTrigger <--> UltiSnips#JumpForwards g:UltiSnipsJumpBackwardTrigger <--> UltiSnips#JumpBackwards + g:UltiSnipsExpandOrJumpTrigger <--> UltiSnips#ExpandSnippetOrJump + g:UltiSnipsJumpOrExpandTrigger <--> UltiSnips#JumpOrExpandSnippet If you have g:UltiSnipsExpandTrigger and g:UltiSnipsJumpForwardTrigger set to the same value then the function you are actually going to use is UltiSnips#ExpandSnippetOrJump. Each time any of the functions UltiSnips#ExpandSnippet, -UltiSnips#ExpandSnippetOrJump, UltiSnips#JumpForwards or -UltiSnips#JumpBackwards is called a global variable is set that contains the -return value of the corresponding function. +UltiSnips#ExpandSnippetOrJump, UltiSnips#JumpOrExpandSnippet, +UltiSnips#JumpForwards or UltiSnips#JumpBackwards is called a global variable +is set that contains the return value of the corresponding function. The corresponding variables and functions are: UltiSnips#ExpandSnippet --> g:ulti_expand_res (0: fail, 1: success) UltiSnips#ExpandSnippetOrJump --> g:ulti_expand_or_jump_res (0: fail, 1: expand, 2: jump) +UltiSnips#JumpOrExpandSnippet --> g:ulti_expand_or_jump_res (0: fail, + 1: expand, 2: jump) UltiSnips#JumpForwards --> g:ulti_jump_forwards_res (0: fail, 1: success) UltiSnips#JumpBackwards --> g:ulti_jump_backwards_res (0: fail, 1: success) diff --git a/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index af82b4f0..3fa77501 100644 --- a/pythonx/UltiSnips/snippet_manager.py +++ b/pythonx/UltiSnips/snippet_manager.py @@ -181,7 +181,7 @@ def expand(self): @err_to_scratch_buffer.wrap def expand_or_jump(self): - """This function is used for people who wants to have the same trigger + """This function is used for people who want to have the same trigger for expansion and forward jumping. It first tries to expand a snippet, if this fails, it tries to @@ -197,6 +197,24 @@ def expand_or_jump(self): vim_helper.command("let g:ulti_expand_or_jump_res = 0") self._handle_failure(self.expand_trigger, True) + @err_to_scratch_buffer.wrap + def jump_or_expand(self): + """This function is used for people who want to have the same trigger + for expansion and forward jumping. + + It first tries to jump forward, if this fails, it tries to + expand a snippet. + + """ + vim_helper.command("let g:ulti_expand_or_jump_res = 2") + rv = self._jump(JumpDirection.FORWARD) + if not rv: + vim_helper.command("let g:ulti_expand_or_jump_res = 1") + rv = self._try_expand() + if not rv: + vim_helper.command("let g:ulti_expand_or_jump_res = 0") + self._handle_failure(self.expand_trigger, True) + @err_to_scratch_buffer.wrap def snippets_in_current_scope(self, search_all): """Returns the snippets that could be expanded to Vim as a global diff --git a/test/test_Expand.py b/test/test_Expand.py index 41654760..cf9b70f6 100644 --- a/test/test_Expand.py +++ b/test/test_Expand.py @@ -81,3 +81,35 @@ class SimpleExpand_Issue1343(_VimTest): snippets = ("test", r"${1:\Safe\\}") keys = "test" + EX + JF + "foo" wanted = r"\Safe\foo" + +class SimpleExpandJumpOrExpand_Expand(_VimTest): + snippets = ("hallo", "Hallo Welt!") + keys = "hallo" + EX + wanted = "Hallo Welt!" + + def _extra_vim_config(self, vim_config): + vim_config.append('let g:UltiSnipsJumpOrExpandTrigger=""') + +class SimpleExpandJumpOrExpand_Ambiguity(_VimTest): + snippets = ("test", r"test$1 foo$0") + keys = "test" + EX + EX + "foo" + wanted = "test foofoo" + + def _extra_vim_config(self, vim_config): + vim_config.append('let g:UltiSnipsJumpOrExpandTrigger=""') + +class SimpleExpandExpandOrJump_Expand(_VimTest): + snippets = ("hallo", "Hallo Welt!") + keys = "hallo" + EX + wanted = "Hallo Welt!" + + def _extra_vim_config(self, vim_config): + vim_config.append('let g:UltiSnipsExpandOrJumpTrigger=""') + +class SimpleExpandExpandOrJump_Ambiguity(_VimTest): + snippets = ("test", r"test$1 foo$0") + keys = "test" + EX + EX + "foo" + wanted = "testfoo foo foo" + + def _extra_vim_config(self, vim_config): + vim_config.append('let g:UltiSnipsExpandOrJumpTrigger=""') \ No newline at end of file