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

REQ: expand but try to jump forward also #1

Open
butterflyfish opened this issue Jul 10, 2018 · 13 comments
Open

REQ: expand but try to jump forward also #1

butterflyfish opened this issue Jul 10, 2018 · 13 comments

Comments

@butterflyfish
Copy link

currently, ncm2_ultisnips#_do_expand_or only try to expand snippet, but not try to jump forward. is it possible to add ?

let g:ulti_expand_or_jump_res = 0
function! <SID>ExpandOrClosePopup()
    let snippet = UltiSnips#ExpandSnippetOrJump()
    if g:ulti_expand_or_jump_res > 0
        echom snippet
        return snippet
    else
        return "\<c-y>"
    endif
endfunction

inoremap <silent><expr><CR> pumvisible() ? "<C-R>=<SID>ExpandOrClosePopup()<CR>" : "\<Cr>"

taking go/LC as example:
input key word "if" --> trigger expand -> input "tr" -> LC will popup menu and complete to true -> use tab to choose --> press Enter --> cursor will jump to if body.

currently, with ncm2/ultisnips, i have to use C-J to move explicitly.

@roxma
Copy link
Member

roxma commented Jul 10, 2018

I'm not sure what you what.

After the snippet is expanded, ultisnips will automatically jumps to the first snippet tabstop/placeholder.

If you're encountering wired behavior, better to post a gif to describe the issue.

@butterflyfish
Copy link
Author

maybe i hope to jump next placeholder. currently, based on example, press Enter key, it will input new line, but i want to jump to next placeholder(if body). i will try to find a gif tool to record.

thank you!

@roxma
Copy link
Member

roxma commented Jul 10, 2018

If you want a different order of placeholder, you should modify the snippet instead of modify the plugin

@butterflyfish
Copy link
Author

butterflyfish commented Jul 10, 2018

would you like to try ?

key map

let g:ulti_expand_or_jump_res = 0

function! SmartTab()
    if pumvisible() == 1
        return "\<C-n>"
    else
        let snippet = UltiSnips#ExpandSnippetOrJump()
        if g:ulti_expand_or_jump_res > 0
            return snippet
        else
            return "\<Tab>"
        endif
    endif
endfunction
inoremap <silent><expr><CR> pumvisible() ? "<C-R>=<SID>ExpandOrClosePopup()<CR>" : "\<Cr>"
snoremap <silent><Tab>      <Esc>:call UltiSnips#JumpForwards()<CR>
inoremap <silent><S-Tab>    <C-R>=SmartSTab()<CR>

Steps:

  • please use above key map
  • plugin ncm2/go-languageserver
  • input if, then press Tab to trigger snippet, then cursor is at first placeholder
  • input "tr", then ncm2 will popup menu. press Tab/C-n to choose completion true
  • press Enter key, you will find cursor is at if block body(in {})

But above keymap lose function parameter expand. Then change Entry map to your suggestion:

inoremap <silent> <expr> <CR> ncm2_ultisnips#expand_or("\<CR>", 'n')

Re-execute above steps, you will find explicit Enter key is accepted, as result, newline is inserted, { is moved to newline

@butterflyfish butterflyfish changed the title REQ: expand but try to jump also REQ: expand but try to jump forward also Jul 10, 2018
@butterflyfish
Copy link
Author

diff --git a/autoload/ncm2_ultisnips.vim b/autoload/ncm2_ultisnips.vim
index ac80a21..e165896 100644
--- a/autoload/ncm2_ultisnips.vim
+++ b/autoload/ncm2_ultisnips.vim
@@ -33,6 +33,11 @@ func! ncm2_ultisnips#_do_expand_or()
         call feedkeys("\<Plug>(ncm2_ultisnips_expand)", "im")
         return ''
     endif
+    let g:ulti_jump_forwards_res = 0
+    call UltiSnips#JumpForwards()
+    if g:ulti_jump_forwards_res == 1
+        return ''
+    endif
     call call('feedkeys', s:or_key)
     return ''
 endfunc

That's what I want.

@roxma
Copy link
Member

roxma commented Jul 10, 2018

You could wrap your own <Plug>forward_or_cr key so that you could map a key like this:

inoremap <silent> <expr> <CR> ncm2_ultisnips#expand_or("\<Plug>forward_or_cr", 'm')

@roxma
Copy link
Member

roxma commented Jul 10, 2018

I have exported two API, you should be able to program you own key mapping based on

ncm2_ultisnips#completed_is_snippet()

and

ncm2_ultisnips#inject_completed_snippet()

@butterflyfish
Copy link
Author

thank you. Based on requirement, would you like to give an example ? Sorry, I don't know how to do because knowledge of vim script is very limited.

Anyway, thank you!

@roxma
Copy link
Member

roxma commented Jul 11, 2018

ncm2_ultisnips#expand_or is an example for the two APIs

@butterflyfish
Copy link
Author

i understand now, but it will cause vimrc ugly/complex. UltiSnips provide API UltiSnips#ExpandSnippetOrJump(). How about change ncm2_ultisnips#expand_or to ncm2_ultisnips#expand_or_jump_or ? appreciated for this wonderful plugin.

@partounian
Copy link

Sorry if I am overly confused. Using this extension for ncm2 with your recommended configs for both does not allow for auto-completion and rotation through options with and . How should we go about solving this?

@roxma
Copy link
Member

roxma commented Jul 16, 2018

@partounian

If you're having trouble, you need to post your minimal vimrc and elaborate the reproducing steps. Another issue should be open too.

@matt-snider
Copy link

matt-snider commented Apr 5, 2020

This isssue is a bit old, but yes this would be great to have.

Currently I can expand a snippet from ncm2 by pressing enter with the following mapping:

inoremap <silent> <expr> <cr> pumvisible() 
    \ ? ncm2_ultisnips#expand_or("\<c-y>", 'n') 
    \ : "\<cr>"

But I'd like to additionally be able to press enter again to jump to the next placeholder.

This is how <Plug>(neosnippet_expand_or_jump) in neosnippet works.

Edit - it occurred to me that something like this might work but it doesn't:

let g:UltiSnipsJumpForwardTrigger="<c-b>"
inoremap <silent> <expr> <cr> pumvisible()
    \ ? ncm2_ultisnips#expand_or("\<c-y>", 'n')
    \ : ncm2_ultisnips#completed_is_snippet()
    \   ? "\<c-b>"
    \   : "\<cr>"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants