0%

使用 LSP 搭建代码补全环境

Language Server Protocol (LSP) 用于架接编辑器与具体语言,实现各种功能。

LSP

Vim 8 中 C/C++ 符号索引:LSP 篇

LSP 是一套通信协议,遵从 LSP 规范的客户端(各种编辑器/IDE)可以通过众多 LSP 服务端按协议标准进行通信,由客户端完成用户界面相关的事情,由服务端提编程语言相关的:补全,定义引用查找,诊断,帮助文档,重构等服务。

安装

coc.nvim
Vim的新一代补全插件:coc.nvim

使用 vim-plug 安装

" Use release branch (Recommend)
Plug 'neoclide/coc.nvim', {'branch': 'release'}

配置

执行 :CocConfig 生成配置文件 ~/.vim/coc-settings.json

例如

"diagnostic.enable":
        Set to false to disable diagnostic display,  default: true

coc-settings.json 中添加

"diagnostic.enable": false,

安装内置插件

内置插件支持列表
coc-marketplace
coc.nvim on npm

例如:

  • coc-json for json.
  • coc-tsserver for javascript and typescript.
  • coc-html for html, handlebars and razor.
  • coc-css for css, scss and less.
  • coc-ember for ember projects.
  • coc-vetur for vue, use vetur.
  • coc-phpls for php, use intelephense-docs.
  • coc-java for java, use eclipse.jdt.ls.

使用 :CocInstall coc-xxx 安装,位于 /.config/coc/extensions/node_modules,配置文件 coc-settings.json

例如 coc-python 需要忽略特定错误,查看 配置 添加如下内容

"python.linting.flake8Args": ["--ignore=E501"]

安装 LSP

Language servers

支持多种 LSP,例如:

  • Python
  • C/C++
  • Lua

选择 ccls 作为 C LSP

编译安装如下

$ git clone --depth=1 --recursive https://github.com/MaskRay/ccls
$ wget -c http://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz
$ tar xf clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz
$ cd ccls
$ cmake -H. -BRelease -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=$PWD/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04
$ cmake --build Release

coc-settings.json 中添加配置

"ccls": {                                                               
      "command": "~/.vim/ccls/ccls/Release/ccls",              
      "filetypes": ["c", "cpp", "objc", "objcpp"],                        
      "rootPatterns": [".ccls", "compile_commands.json", ".vim/", ".git/", ".hg/"],
      "initializationOptions": {                                          
          "cache": {                                                      
              "directory": "/tmp/ccls"                                    
          }                                                               
      }                                                                   
  }

ccls 运行依赖于 compile_commands.json,使用 Bear 生成此文件

$ Bear make

.vimrc 配置

Example vim configuration
ccls coc.nvim配置

nmap <silent> <M-j> <Plug>(coc-definition)
nmap <silent> <C-,> <Plug>(coc-references)
nn <silent> K :call CocActionAsync('doHover')<cr>

" bases
nn <silent> xb :call CocLocations('ccls','$ccls/inheritance')<cr>
" bases of up to 3 levels
nn <silent> xB :call CocLocations('ccls','$ccls/inheritance',{'levels':3})<cr>
" derived
nn <silent> xd :call CocLocations('ccls','$ccls/inheritance',{'derived':v:true})<cr>
" derived of up to 3 levels
nn <silent> xD :call CocLocations('ccls','$ccls/inheritance',{'derived':v:true,'levels':3})<cr>

" caller
nn <silent> xc :call CocLocations('ccls','$ccls/call')<cr>
" callee
nn <silent> xC :call CocLocations('ccls','$ccls/call',{'callee':v:true})<cr>

" $ccls/member
" member variables / variables in a namespace
nn <silent> xm :call CocLocations('ccls','$ccls/member')<cr>
" member functions / functions in a namespace
nn <silent> xf :call CocLocations('ccls','$ccls/member',{'kind':3})<cr>
" nested classes / types in a namespace
nn <silent> xs :call CocLocations('ccls','$ccls/member',{'kind':2})<cr>

nmap <silent> xt <Plug>(coc-type-definition)<cr>
nn <silent> xv :call CocLocations('ccls','$ccls/vars')<cr>
nn <silent> xV :call CocLocations('ccls','$ccls/vars',{'kind':1})<cr>