Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JumpOrExpand support #1542

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions autoload/UltiSnips.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
8 changes: 7 additions & 1 deletion autoload/UltiSnips/map_keys.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 <silent> " . g:UltiSnipsExpandOrJumpTrigger . " <C-R>=UltiSnips#ExpandSnippetOrJump()<cr>"
exec "snoremap <silent> " . g:UltiSnipsExpandOrJumpTrigger . " <Esc>:call UltiSnips#ExpandSnippetOrJump()<cr>"
elseif exists("g:UltiSnipsJumpOrExpandTrigger")
exec "inoremap <silent> " . g:UltiSnipsJumpOrExpandTrigger . " <C-R>=UltiSnips#JumpOrExpandSnippet()<cr>"
exec "snoremap <silent> " . g:UltiSnipsJumpOrExpandTrigger . " <Esc>:call UltiSnips#JumpOrExpandSnippet()<cr>"
elseif g:UltiSnipsExpandTrigger == g:UltiSnipsJumpForwardTrigger
exec "inoremap <silent> " . g:UltiSnipsExpandTrigger . " <C-R>=UltiSnips#ExpandSnippetOrJump()<cr>"
exec "snoremap <silent> " . g:UltiSnipsExpandTrigger . " <Esc>:call UltiSnips#ExpandSnippetOrJump()<cr>"
else
Expand Down
25 changes: 22 additions & 3 deletions doc/UltiSnips.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -267,27 +268,45 @@ following to your vimrc file or switching to a plugin like Supertab or
YouCompleteMe. >
inoremap <c-x><c-k> <c-x><c-k>

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 = "<tab>"

Or else, if you have a preference for jumping forward first, set
g:UltiSnipsJumpOrExpandTrigger instead. >
let g:UltiSnipsJumpOrExpandTrigger = "<tab>"

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
pilgrimlyieu marked this conversation as resolved.
Show resolved Hide resolved
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)

Expand Down
18 changes: 18 additions & 0 deletions pythonx/UltiSnips/snippet_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
pilgrimlyieu marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down