coc.vim 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. " Use tab for trigger completion with characters ahead and navigate.
  2. inoremap <silent><expr> <TAB>
  3. \ pumvisible() ? "\<C-n>" :
  4. \ <SID>check_back_space() ? "\<TAB>" :
  5. \ coc#refresh()
  6. inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
  7. function! s:check_back_space() abort
  8. let col = col('.') - 1
  9. return !col || getline('.')[col - 1] =~# '\s'
  10. endfunction
  11. " Use <c-space> to trigger completion.
  12. inoremap <silent><expr> <c-space> coc#refresh()
  13. " Use <cr> to confirm completion, `<C-g>u` means break undo chain at current
  14. " position. Coc only does snippet and additional edit on confirm.
  15. if exists('*complete_info')
  16. inoremap <expr> <cr> complete_info()["selected"] != "-1" ? "\<C-y>" : "\<C-g>u\<CR>"
  17. else
  18. imap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
  19. endif
  20. " GoTo code navigation.
  21. nmap <silent> gd <Plug>(coc-definition)
  22. nmap <silent> gy <Plug>(coc-type-definition)
  23. nmap <silent> gi <Plug>(coc-implementation)
  24. nmap <silent> gr <Plug>(coc-references)
  25. " Use K to show documentation in preview window.
  26. nnoremap <silent> K :call <SID>show_documentation()<CR>
  27. function! s:show_documentation()
  28. if (index(['vim','help'], &filetype) >= 0)
  29. execute 'h '.expand('<cword>')
  30. elseif (coc#rpc#ready())
  31. call CocActionAsync('doHover')
  32. else
  33. execute '!' . &keywordprg . " " . expand('<cword>')
  34. endif
  35. endfunction
  36. " set keywordprg=:call\ CocActionAsync('doHover')
  37. " augroup VimHelp
  38. " autocmd!
  39. " autocmd Filetype vim,help setlocal keywordprg=:help
  40. " augroup END
  41. " Highlight the symbol and its references when holding the cursor.
  42. autocmd CursorHold * silent call CocActionAsync('highlight')
  43. " Symbol renaming.
  44. " nmap <leader>rn <Plug>(coc-rename)
  45. augroup mygroup
  46. autocmd!
  47. " Setup formatexpr specified filetype(s).
  48. autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
  49. " Update signature help on jump placeholder.
  50. autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
  51. augroup end
  52. " Applying codeAction to the selected region.
  53. " Example: `<leader>aap` for current paragraph
  54. " xmap <leader>a <Plug>(coc-codeaction-selected)
  55. " nmap <leader>a <Plug>(coc-codeaction-selected)
  56. " Remap keys for applying codeAction to the current line.
  57. " nmap <leader>ac <Plug>(coc-codeaction)
  58. " Apply AutoFix to problem on the current line.
  59. " nmap <leader>qf <Plug>(coc-fix-current)
  60. " Introduce function text object
  61. " NOTE: Requires 'textDocument.documentSymbol' support from the language server.
  62. xmap if <Plug>(coc-funcobj-i)
  63. xmap af <Plug>(coc-funcobj-a)
  64. omap if <Plug>(coc-funcobj-i)
  65. omap af <Plug>(coc-funcobj-a)
  66. " Remap <C-f> and <C-b> for scroll float windows/popups.
  67. " Note coc#float#scroll works on neovim >= 0.4.3 or vim >= 8.2.0750
  68. nnoremap <nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
  69. nnoremap <nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
  70. inoremap <nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
  71. inoremap <nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
  72. " NeoVim-only mapping for visual mode scroll
  73. " Useful on signatureHelp after jump placeholder of snippet expansion
  74. if has('nvim')
  75. vnoremap <nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#nvim_scroll(1, 1) : "\<C-f>"
  76. vnoremap <nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#nvim_scroll(0, 1) : "\<C-b>"
  77. endif
  78. " Add `:Format` command to format current buffer.
  79. command! -nargs=0 Format :call CocAction('format')
  80. " Add `:Fold` command to fold current buffer.
  81. command! -nargs=? Fold :call CocAction('fold', <f-args>)
  82. " Add `:OR` command for organize imports of the current buffer.
  83. command! -nargs=0 OR :call CocAction('runCommand', 'editor.action.organizeImport')
  84. " Add (Neo)Vim's native statusline support.
  85. " NOTE: Please see `:h coc-status` for integrations with external plugins that
  86. " provide custom statusline: lightline.vim, vim-airline.
  87. " set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
  88. " Mappings using CoCList:
  89. " Show all diagnostics.
  90. " TODO add these to which key
  91. " nnoremap <silent> <space>a :<C-u>CocList diagnostics<cr>
  92. " " Manage extensions.
  93. " nnoremap <silent> <space>e :<C-u>CocList extensions<cr>
  94. " " Show commands.
  95. " nnoremap <silent> <space>c :<C-u>CocList commands<cr>
  96. " " Find symbol of current document.
  97. " nnoremap <silent> <space>o :<C-u>CocList outline<cr>
  98. " " Search workspace symbols.
  99. " nnoremap <silent> <space>s :<C-u>CocList -I symbols<cr>
  100. " " Do default action for next item.
  101. " nnoremap <silent> <space>j :<C-u>CocNext<CR>
  102. " " Do default action for previous item.
  103. " nnoremap <silent> <space>k :<C-u>CocPrev<CR>
  104. " " Resume latest coc list.
  105. " nnoremap <silent> <space>p :<C-u>CocListResume<CR>
  106. " Explorer
  107. let g:coc_explorer_global_presets = {
  108. \ 'floating': {
  109. \ 'position': 'floating',
  110. \ },
  111. \ 'floatingLeftside': {
  112. \ 'position': 'floating',
  113. \ 'floating-position': 'left-center',
  114. \ 'floating-width': 30,
  115. \ },
  116. \ 'floatingRightside': {
  117. \ 'position': 'floating',
  118. \ 'floating-position': 'right-center',
  119. \ 'floating-width': 30,
  120. \ },
  121. \ 'simplify': {
  122. \ 'file.child.template': '[selection | clip | 1] [indent][icon | 1] [filename omitCenter 1]'
  123. \ }
  124. \ }
  125. "nmap <silent> <space>e :CocCommand explorer<CR>
  126. " nnoremap <silent> <leader>e :CocCommand explorer<CR>
  127. " nmap <space>f :CocCommand explorer --preset floatingRightside<CR>
  128. autocmd BufEnter * if (winnr("$") == 1 && &filetype == 'coc-explorer') | q | endif
  129. " Snippets
  130. " Use <C-l> for trigger snippet expand.
  131. imap <C-l> <Plug>(coc-snippets-expand)
  132. " Use <C-j> for select text for visual placeholder of snippet.
  133. vmap <C-j> <Plug>(coc-snippets-select)
  134. " Use <C-j> for jump to next placeholder, it's default of coc.nvim
  135. let g:coc_snippet_next = '<c-j>'
  136. " Use <C-k> for jump to previous placeholder, it's default of coc.nvim
  137. let g:coc_snippet_prev = '<c-k>'
  138. " Use <C-j> for both expand and jump (make expand higher priority.)
  139. imap <C-j> <Plug>(coc-snippets-expand-jump)