From f511f04ec1436214bb21f072e2e91a731f776af4 Mon Sep 17 00:00:00 2001 From: PilgrimLyieu Date: Mon, 25 Sep 2023 21:15:05 +0800 Subject: [PATCH 1/4] Add JumpOrExpand support --- autoload/UltiSnips.vim | 6 ++++++ autoload/UltiSnips/map_keys.vim | 8 +++++++- pythonx/UltiSnips/snippet_manager.py | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) 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/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index af82b4f0..b4f95583 100644 --- a/pythonx/UltiSnips/snippet_manager.py +++ b/pythonx/UltiSnips/snippet_manager.py @@ -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 wants 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 From e5ec70dd60f7678e3f22bcb1f25cca4ce99ae081 Mon Sep 17 00:00:00 2001 From: PilgrimLyieu Date: Mon, 25 Sep 2023 22:13:45 +0800 Subject: [PATCH 2/4] Update documentation --- doc/UltiSnips.txt | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 5adbcea7..9119223f 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,6 +268,20 @@ 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 @@ -274,20 +289,24 @@ 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) From 076a23d41b4d8763a7f2597ec9e9f2660dabef98 Mon Sep 17 00:00:00 2001 From: PilgrimLyieu Date: Tue, 26 Sep 2023 10:08:14 +0800 Subject: [PATCH 3/4] Update documentation & Fix typo --- doc/UltiSnips.txt | 2 +- pythonx/UltiSnips/snippet_manager.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 9119223f..22f984ca 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -284,7 +284,7 @@ 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 diff --git a/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index b4f95583..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 @@ -199,7 +199,7 @@ def expand_or_jump(self): @err_to_scratch_buffer.wrap def jump_or_expand(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 jump forward, if this fails, it tries to From 094e673d180fc8ba6fb00258e5ac3c275066d13c Mon Sep 17 00:00:00 2001 From: PilgrimLyieu Date: Tue, 26 Sep 2023 11:37:16 +0800 Subject: [PATCH 4/4] Add some tests --- test/test_Expand.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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