coc.vim 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. " Use `[g` and `]g` to navigate diagnostics
  21. nmap <silent> [g <Plug>(coc-diagnostic-prev)
  22. nmap <silent> ]g <Plug>(coc-diagnostic-next)
  23. " GoTo code navigation.
  24. nmap <silent> gd <Plug>(coc-definition)
  25. nmap <silent> gy <Plug>(coc-type-definition)
  26. nmap <silent> gi <Plug>(coc-implementation)
  27. nmap <silent> gr <Plug>(coc-references)
  28. " Use K to show documentation in preview window.
  29. nnoremap <silent> K :call <SID>show_documentation()<CR>
  30. function! s:show_documentation()
  31. if (index(['vim','help'], &filetype) >= 0)
  32. execute 'h '.expand('<cword>')
  33. else
  34. call CocAction('doHover')
  35. endif
  36. endfunction
  37. " Highlight the symbol and its references when holding the cursor.
  38. autocmd CursorHold * silent call CocActionAsync('highlight')
  39. " Symbol renaming.
  40. nmap <leader>rn <Plug>(coc-rename)
  41. " Formatting selected code.
  42. xmap <leader>f <Plug>(coc-format-selected)
  43. nmap <leader>f <Plug>(coc-format-selected)
  44. augroup mygroup
  45. autocmd!
  46. " Setup formatexpr specified filetype(s).
  47. autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
  48. " Update signature help on jump placeholder.
  49. autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
  50. augroup end
  51. " Applying codeAction to the selected region.
  52. " Example: `<leader>aap` for current paragraph
  53. xmap <leader>a <Plug>(coc-codeaction-selected)
  54. nmap <leader>a <Plug>(coc-codeaction-selected)
  55. " Remap keys for applying codeAction to the current line.
  56. nmap <leader>ac <Plug>(coc-codeaction)
  57. " Apply AutoFix to problem on the current line.
  58. nmap <leader>qf <Plug>(coc-fix-current)
  59. " Introduce function text object
  60. " NOTE: Requires 'textDocument.documentSymbol' support from the language server.
  61. xmap if <Plug>(coc-funcobj-i)
  62. xmap af <Plug>(coc-funcobj-a)
  63. omap if <Plug>(coc-funcobj-i)
  64. omap af <Plug>(coc-funcobj-a)
  65. " Use <TAB> for selections ranges.
  66. " NOTE: Requires 'textDocument/selectionRange' support from the language server.
  67. " coc-tsserver, coc-python are the examples of servers that support it.
  68. " nmap <silent> <TAB> <Plug>(coc-range-select)
  69. " xmap <silent> <TAB> <Plug>(coc-range-select)
  70. " Add `:Format` command to format current buffer.
  71. command! -nargs=0 Format :call CocAction('format')
  72. " Add `:Fold` command to fold current buffer.
  73. command! -nargs=? Fold :call CocAction('fold', <f-args>)
  74. " Add `:OR` command for organize imports of the current buffer.
  75. command! -nargs=0 OR :call CocAction('runCommand', 'editor.action.organizeImport')
  76. " Add (Neo)Vim's native statusline support.
  77. " NOTE: Please see `:h coc-status` for integrations with external plugins that
  78. " provide custom statusline: lightline.vim, vim-airline.
  79. set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
  80. " Mappings using CoCList:
  81. " Show all diagnostics.
  82. nnoremap <silent> <space>a :<C-u>CocList diagnostics<cr>
  83. " Manage extensions.
  84. nnoremap <silent> <space>e :<C-u>CocList extensions<cr>
  85. " Show commands.
  86. nnoremap <silent> <space>c :<C-u>CocList commands<cr>
  87. " Find symbol of current document.
  88. nnoremap <silent> <space>o :<C-u>CocList outline<cr>
  89. " Search workspace symbols.
  90. nnoremap <silent> <space>s :<C-u>CocList -I symbols<cr>
  91. " Do default action for next item.
  92. nnoremap <silent> <space>j :<C-u>CocNext<CR>
  93. " Do default action for previous item.
  94. nnoremap <silent> <space>k :<C-u>CocPrev<CR>
  95. " Resume latest coc list.
  96. nnoremap <silent> <space>p :<C-u>CocListResume<CR>
  97. " Explorer
  98. let g:coc_explorer_global_presets = {
  99. \ 'floating': {
  100. \ 'position': 'floating',
  101. \ },
  102. \ 'floatingLeftside': {
  103. \ 'position': 'floating',
  104. \ 'floating-position': 'left-center',
  105. \ 'floating-width': 30,
  106. \ },
  107. \ 'floatingRightside': {
  108. \ 'position': 'floating',
  109. \ 'floating-position': 'right-center',
  110. \ 'floating-width': 30,
  111. \ },
  112. \ 'simplify': {
  113. \ 'file.child.template': '[selection | clip | 1] [indent][icon | 1] [filename omitCenter 1]'
  114. \ }
  115. \ }
  116. nmap <silent> <space>e :CocCommand explorer<CR>
  117. nmap <space>f :CocCommand explorer --preset floatingRightside<CR>
  118. autocmd BufEnter * if (winnr("$") == 1 && &filetype == 'coc-explorer') | q | endif