Coc.nvim: Vim-snippets different code snippet will trigger tab exception

Created on 28 Mar 2019  Â·  15Comments  Â·  Source: neoclide/coc.nvim

Result from CocInfo

## versions

vim version: NVIM v0.4.0-460-g2470c8829
node version: v10.15.0
coc.nvim version: 0.0.63
term: xterm-256color
platform: linux

Describe the bug
vim-snippets has two snippets floder.Ultisnippet and snippets provide snippets,
They have different jumps,the fun of snippets provide will trigger tab jump next snippet when i complete not Ctrln in completelist . this code from
https://github.com/honza/vim-snippets/blob/master/snippets/go.snippetst

snippet fun "function"
    func ${1:funcName}(${2}) ${3:error} {
        ${4}
    }
    ${0}   <- -------  (problem is here)

To Reproduce

Note, if you can't provide minimal vimrc that could reproduce the issue,
it's most likely we can't do anything to help.

  • Create a minimal mini.vim with:
call plug#begin('~/.local/share/nvim/plugged')
Plug 'neoclide/coc.nvim', {'do': 'yarn install'}
Plug 'honza/vim-snippets'
call plug#end()
let g:coc_snippet_next = '<TAB>'
let g:coc_snippet_prev = '<S-TAB>'
 let g:coc_global_extensions =['coc-snippets','coc-json']
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
  • Start neovim with command: nvim -u mini.vim

  • Operate vim.

Screenshots

go

Most helpful comment

@chemzqm This is what I finally end up with, can you verify if this is good?

" Press Tab and Shift+Tab and navigate around completion selections
function! s:check_back_space() abort
  let col = col('.') -1
  return !col || getline('.')[col - 1] =~ '\s'
endfunction

inoremap <silent><expr> <Tab>
  \ pumvisible() ? "\<C-n>" :
  \ <SID>check_back_space() ? "\<Tab>" :
  \ coc#refresh()
inoremap <silent><expr> <S-Tab>
  \ pumvisible() ? "\<C-p>" :
  \ <SID>check_back_space() ? "\<S-Tab>" :
  \ coc#refresh()

" Press Enter to select completion items or expand snippets
inoremap <silent><expr> <CR>
  \ pumvisible() ? "\<C-y>" :
  \ "\<C-g>u\<CR>"

let g:coc_snippet_next = '<Tab>'              " Use Tab to jump to next snippet placeholder
let g:coc_snippet_prev = '<S-Tab>'            " Use Shift+Tab to jump to previous snippet placeholder

I press f, it shows me func and function snippets in the menu, then I can press Tab or S-Tab to navigate, then I press Enter to expand the snippet. After the snippet is expanded, I can press Tab and S-Tab to jump back and forth placeholders.

All 15 comments

It's expected since jump placeholder have higher priority, you need to map <tab> like this:

inoremap <silent><expr> <TAB>
      \ pumvisible() ?  "<C-n>" :
      \ coc#expandableOrJumpable() ? coc#rpc#request('doKeymap', ['snippets-expand-jump','']) :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()

Tab does not work as expected, triggers completion
go

I can't understand what's problem here.

I can't understand what's problem here.

hmm Sorry for my explanation, you can see tab triggers completion not jump next .

  • func from Ulitsnippet then input bool and i want to jump next snipepts,but it trigger completion.
  • fun from snippet then input twosum i got same problem,

I have the same problem with with @taigacute.
My expectation is:
Pressing tab when there's only 1 selection should expand or input completion (works as expected), pressing tab when there are 10 selections should go to the next selection until we press enter to select that selection (not working as expected).
Can anyone help with my expectation mapping? Thanks before.

Map tab like this:

inoremap <silent><expr> <TAB>
      \ pumvisible() ? coc#_select_confirm() :
      \ coc#expandableOrJumpable() ? "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()

function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

let g:coc_snippet_next = '<tab>'

Same thing here, if I map like this then it's working, but doesn't expand snippet anymore.

inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ coc#expandableOrJumpable() ? "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()

Maybe there's anyway to make the mapping to only do coc#_select_confirm() only when the list is not jumpable? Sorry if I'm wrong, I'm a newbie.

@budimanjojo You should not map <tab> to jump placeholder and navigate complete item at the same time, since they could be true at the same time, and you will get frustrated.

@chemzqm sorry I don't get understand. So if I shouldn't do like that, can you tell me the keybinding if I want to:
Use tab to navigate completion items, and use enter to select item and jump placeholder?
For example, when I type fu, it shows me function and func snippets, then I press tab to select function snippet and press enter to select that snippet and expand it. Then press enter again to jump placeholder.

use enter to select item and jump placeholder?

What to do when pum is visible and your snippet session is activated?

When pum is visible and I have snippets in the pum, I want to press to move down the pum, and press to select the snippet and expand them. Is that possible?

Sure, write the expr keymap like the above example, functions needed are available.

Can you please show me the entire code to get it working? I'm really stuck here

@chemzqm This is what I finally end up with, can you verify if this is good?

" Press Tab and Shift+Tab and navigate around completion selections
function! s:check_back_space() abort
  let col = col('.') -1
  return !col || getline('.')[col - 1] =~ '\s'
endfunction

inoremap <silent><expr> <Tab>
  \ pumvisible() ? "\<C-n>" :
  \ <SID>check_back_space() ? "\<Tab>" :
  \ coc#refresh()
inoremap <silent><expr> <S-Tab>
  \ pumvisible() ? "\<C-p>" :
  \ <SID>check_back_space() ? "\<S-Tab>" :
  \ coc#refresh()

" Press Enter to select completion items or expand snippets
inoremap <silent><expr> <CR>
  \ pumvisible() ? "\<C-y>" :
  \ "\<C-g>u\<CR>"

let g:coc_snippet_next = '<Tab>'              " Use Tab to jump to next snippet placeholder
let g:coc_snippet_prev = '<S-Tab>'            " Use Shift+Tab to jump to previous snippet placeholder

I press f, it shows me func and function snippets in the menu, then I can press Tab or S-Tab to navigate, then I press Enter to expand the snippet. After the snippet is expanded, I can press Tab and S-Tab to jump back and forth placeholders.

@budimanjojo Thank you for supplying your config. I've been fighting with this for some time and your setup works perfectly. Really wish someone would update the docs with the knowledge you've shared above. Great work!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaskRay picture MaskRay  Â·  3Comments

skylite21 picture skylite21  Â·  3Comments

tom-james-watson picture tom-james-watson  Â·  3Comments

czepluch picture czepluch  Â·  3Comments

cvlmtg picture cvlmtg  Â·  3Comments