vim:coc: Customize completion menu mappings
When no item was inserted yet, the first tab should insert the first item or the first shift-tab should select and insert the last item.
This commit is contained in:
@@ -13,20 +13,47 @@ set signcolumn=yes
|
|||||||
inoremap <silent><expr> <C-n> coc#pum#visible() ? coc#pum#next(1) : "\<C-n>"
|
inoremap <silent><expr> <C-n> coc#pum#visible() ? coc#pum#next(1) : "\<C-n>"
|
||||||
inoremap <silent><expr> <C-p> coc#pum#visible() ? coc#pum#prev(1) : "\<C-n>"
|
inoremap <silent><expr> <C-p> coc#pum#visible() ? coc#pum#prev(1) : "\<C-n>"
|
||||||
|
|
||||||
" Use tab for trigger completion with characters ahead and navigate.
|
" When no item was inserted yet, the first tab should insert the first item.
|
||||||
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
|
" `timer_start` is used to delay the execution to insert in the real buffer as
|
||||||
" other plugin before putting this into your config.
|
" far as I understand, otherwise vim will complain with 'E565: Not allowed to
|
||||||
inoremap <silent><expr> <TAB>
|
" change text or change window'. It is the same method used by coc.nvim (e.g. in
|
||||||
\ coc#pum#visible() ? coc#pum#next(1):
|
" coc#pum#next).
|
||||||
\ CheckBackspace() ? "\<Tab>" :
|
function! CocPumNext(n)
|
||||||
\ coc#refresh()
|
if coc#pum#info()['inserted']
|
||||||
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"
|
return coc#pum#next(a:n)
|
||||||
|
else
|
||||||
|
call timer_start(10, { -> coc#pum#select(0,1,0) })
|
||||||
|
return "\<Ignore>"
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
" When no item was inserted yet, the first shift-tab should select and insert
|
||||||
|
" the last item. See note about timer_start above.
|
||||||
|
function! CocPumPrev(n)
|
||||||
|
let l:info = coc#pum#info()
|
||||||
|
if l:info['inserted']
|
||||||
|
return coc#pum#prev(a:n)
|
||||||
|
else
|
||||||
|
call timer_start(10, { -> coc#pum#select(l:info['size'] - 1,1,0) })
|
||||||
|
return "\<Ignore>"
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" Returns true if the cursor is at the beginning of the line or behind
|
||||||
|
" whitespace.
|
||||||
function! CheckBackspace() abort
|
function! CheckBackspace() abort
|
||||||
let col = col('.') - 1
|
let col = col('.') - 1
|
||||||
return !col || getline('.')[col - 1] =~# '\s'
|
return !col || getline('.')[col - 1] =~# '\s'
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
" Use tab for trigger completion with characters ahead and navigate.
|
||||||
|
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
|
||||||
|
" other plugin before putting this into your config.
|
||||||
|
inoremap <silent><expr> <Tab>
|
||||||
|
\ coc#pum#visible() ? CocPumNext(1) :
|
||||||
|
\ CheckBackspace() ? "\<Tab>" :
|
||||||
|
\ coc#refresh()
|
||||||
|
inoremap <expr> <S-Tab> coc#pum#visible() ? CocPumPrev(1) : "\<S-Tab>"
|
||||||
|
|
||||||
" Use <c-space> to trigger completion.
|
" Use <c-space> to trigger completion.
|
||||||
if has('nvim')
|
if has('nvim')
|
||||||
inoremap <silent><expr> <c-space> coc#refresh()
|
inoremap <silent><expr> <c-space> coc#refresh()
|
||||||
|
|||||||
Reference in New Issue
Block a user