vim配置coc 补全代码

coc项目地址’https://github.com/neoclide/coc.nvim'

Install coc

使用Vim-Plugin安装coc.vim

在vimrc中添加插件

1
2
3
4
5
" Use release branch (recommend) 推荐使用
Plug 'neoclide/coc.nvim', {'branch': 'release'}

" Or build from source code by using yarn: https://yarnpkg.com
Plug 'neoclide/coc.nvim', {'branch': 'master', 'do': 'yarn install --frozen-lockfile'}

安装nodejs

https://nodejs.org/en/

安装coc extention和LSP支持

进入vim 使用coc安装

1
:CocInstall coc-json coc-tsserver

安装语言支持

参考coc 官方拓展:https://github.com/neoclide/coc.nvim/wiki/Using-coc-extensions

安装coc-marketplace

1
:CocInstall coc-marketplace

其他拓展直接使用coc-marketplace在命令行查看

1
:CocList marketplace

配置vimrc文件

按键映射设置回车键触发补全

参考cocwiki https://github.com/neoclide/coc.nvim/wiki/Completion-with-sources#trigger-mode-of-completion

使用Tab键和Shift+Tab选择补全

1
2
3
4
5
6
7
8
9
10
" Use tab for trigger completion with characters ahead and navigate.
" NOTE: There's always complete item selected by default, you may want to enable
" no select by `"suggest.noselect": true` in your configuration file.
" 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() ? coc#pum#next(1) :
\ CheckBackspace() ? "\<Tab>" :
\ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"

使用回车键选择当前候选

1
2
3
4
5
6
7
8
9
" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

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

使用Ctrl+space调出补全窗口

1
2
3
4
5
6
" Use <c-space> to trigger completion.
if has('nvim')
inoremap <silent><expr> <c-space> coc#refresh()
else
inoremap <silent><expr> <c-@> coc#refresh()
endif

跳转到函数定义

1
2
3
4
5
" GoTo code navigation.
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

使用K显示文档窗口

1
2
3
4
5
6
7
8
9
10
" Use K to show documentation in preview window.
nnoremap <silent> K :call ShowDocumentation()<CR>

function! ShowDocumentation()
if CocAction('hasProvider', 'hover')
call CocActionAsync('doHover')
else
call feedkeys('K', 'in')
endif
endfunction

选中当前变量高亮

1
2
" Highlight the symbol and its references when holding the cursor.
autocmd CursorHold * silent call CocActionAsync('highlight')

重命名变量

1
2
" Symbol renaming.
nmap <leader>rn <Plug>(coc-rename)

格式化当前选中代码

1
2
3
4
5
6
7
8
9
10
11
" Formatting selected code.
xmap <leader>f <Plug>(coc-format-selected)
nmap <leader>f <Plug>(coc-format-selected)

augroup mygroup
autocmd!
" Setup formatexpr specified filetype(s).
autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
" Update signature help on jump placeholder.
autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end

选择函数、类 文字对象

1
2
3
4
5
6
7
8
9
10
" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server.
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)