Update neovim config
[khome.git] / home / .config / nvim / setup-coc.vim
CommitLineData
e66a0080
SK
1" May need for Vim (not Neovim) since coc.nvim calculates byte offset by count
2" utf-8 byte sequence
3" set encoding=utf-8
4" Some servers have issues with backup files, see #649
5set nobackup
6set nowritebackup
7
8" Having longer updatetime (default is 4000 ms = 4s) leads to noticeable
9" delays and poor user experience
10set updatetime=300
11
12" Always show the signcolumn, otherwise it would shift the text each time
13" diagnostics appear/become resolved
14set signcolumn=yes
15
16" Use tab for trigger completion with characters ahead and navigate
17" NOTE: There's always complete item selected by default, you may want to enable
18" no select by `"suggest.noselect": true` in your configuration file
19" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
20" other plugin before putting this into your config
21inoremap <silent><expr> <TAB>
22 \ coc#pum#visible() ? coc#pum#next(1) :
23 \ CheckBackspace() ? "\<Tab>" :
24 \ coc#refresh()
25inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"
26
27" Make <CR> to accept selected completion item or notify coc.nvim to format
28" <C-g>u breaks current undo, please make your own choice
29inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
30 \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
31
32function! CheckBackspace() abort
33 let col = col('.') - 1
34 return !col || getline('.')[col - 1] =~# '\s'
35endfunction
36
37" Use <c-space> to trigger completion
38if has('nvim')
39 inoremap <silent><expr> <c-space> coc#refresh()
40else
41 inoremap <silent><expr> <c-@> coc#refresh()
42endif
43
44" Use `[g` and `]g` to navigate diagnostics
45" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list
46nmap <silent> [g <Plug>(coc-diagnostic-prev)
47nmap <silent> ]g <Plug>(coc-diagnostic-next)
48
49" GoTo code navigation
50nmap <silent> gd <Plug>(coc-definition)
51nmap <silent> gy <Plug>(coc-type-definition)
52nmap <silent> gi <Plug>(coc-implementation)
53nmap <silent> gr <Plug>(coc-references)
54
55" Use K to show documentation in preview window
56nnoremap <silent> K :call ShowDocumentation()<CR>
57
58function! ShowDocumentation()
59 if CocAction('hasProvider', 'hover')
60 call CocActionAsync('doHover')
61 else
62 call feedkeys('K', 'in')
63 endif
64endfunction
65
66" Highlight the symbol and its references when holding the cursor
67autocmd CursorHold * silent call CocActionAsync('highlight')
68
69" Symbol renaming
70nmap <leader>rn <Plug>(coc-rename)
71
72" Formatting selected code
73xmap <leader>f <Plug>(coc-format-selected)
74nmap <leader>f <Plug>(coc-format-selected)
75
76augroup mygroup
77 autocmd!
78 " Setup formatexpr specified filetype(s)
79 autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
80 " Update signature help on jump placeholder
81 autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
82augroup end
83
84" Applying code actions to the selected code block
85" Example: `<leader>aap` for current paragraph
86xmap <leader>a <Plug>(coc-codeaction-selected)
87nmap <leader>a <Plug>(coc-codeaction-selected)
88
89" Remap keys for applying code actions at the cursor position
90nmap <leader>ac <Plug>(coc-codeaction-cursor)
91" Remap keys for apply code actions affect whole buffer
92nmap <leader>as <Plug>(coc-codeaction-source)
93" Apply the most preferred quickfix action to fix diagnostic on the current line
94nmap <leader>qf <Plug>(coc-fix-current)
95
96" Remap keys for applying refactor code actions
97nmap <silent> <leader>re <Plug>(coc-codeaction-refactor)
98xmap <silent> <leader>r <Plug>(coc-codeaction-refactor-selected)
99nmap <silent> <leader>r <Plug>(coc-codeaction-refactor-selected)
100
101" Run the Code Lens action on the current line
102nmap <leader>cl <Plug>(coc-codelens-action)
103
104" Map function and class text objects
105" NOTE: Requires 'textDocument.documentSymbol' support from the language server
106xmap if <Plug>(coc-funcobj-i)
107omap if <Plug>(coc-funcobj-i)
108xmap af <Plug>(coc-funcobj-a)
109omap af <Plug>(coc-funcobj-a)
110xmap ic <Plug>(coc-classobj-i)
111omap ic <Plug>(coc-classobj-i)
112xmap ac <Plug>(coc-classobj-a)
113omap ac <Plug>(coc-classobj-a)
114
115" Remap <C-f> and <C-b> to scroll float windows/popups
116if has('nvim-0.4.0') || has('patch-8.2.0750')
117 nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
118 nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
119 inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
120 inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
121 vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
122 vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
123endif
124
125" Use CTRL-S for selections ranges
126" Requires 'textDocument/selectionRange' support of language server
127nmap <silent> <C-s> <Plug>(coc-range-select)
128xmap <silent> <C-s> <Plug>(coc-range-select)
129
130" Add `:Format` command to format current buffer
131command! -nargs=0 Format :call CocActionAsync('format')
132
133" Add `:Fold` command to fold current buffer
134command! -nargs=? Fold :call CocAction('fold', <f-args>)
135
136" Add `:OR` command for organize imports of the current buffer
137command! -nargs=0 OR :call CocActionAsync('runCommand', 'editor.action.organizeImport')
138
139" Add (Neo)Vim's native statusline support
140" NOTE: Please see `:h coc-status` for integrations with external plugins that
141" provide custom statusline: lightline.vim, vim-airline
142set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
143
144" Mappings for CoCList
145" Show all diagnostics
146nnoremap <silent><nowait> <space>a :<C-u>CocList diagnostics<cr>
147" Manage extensions
148nnoremap <silent><nowait> <space>e :<C-u>CocList extensions<cr>
149" Show commands
150nnoremap <silent><nowait> <space>c :<C-u>CocList commands<cr>
151" Find symbol of current document
152nnoremap <silent><nowait> <space>o :<C-u>CocList outline<cr>
153" Search workspace symbols
154nnoremap <silent><nowait> <space>s :<C-u>CocList -I symbols<cr>
155" Do default action for next item
156nnoremap <silent><nowait> <space>j :<C-u>CocNext<CR>
157" Do default action for previous item
158nnoremap <silent><nowait> <space>k :<C-u>CocPrev<CR>
159" Resume latest coc list
160nnoremap <silent><nowait> <space>p :<C-u>CocListResume<CR>
This page took 0.035934 seconds and 4 git commands to generate.