プログラミングとかLinuxとかの備忘録

プログラミング、Linuxでハマった箇所や環境構築のメモ

NeoVimでvim-lspの環境を構築する

スポンサーリンク

ArchLinux (5.8.9-arch2-1) + Neovim v0.4.4の環境でvim-lspを使える様にする。

~/.config/nvim/init.vimの編集

asyncomplete.vim+vim-lspの環境に必要なプラグイン4つと

  • prabirshrestha/asyncomplete.vim
  • prabirshrestha/vim-lsp
  • prabirshrestha/async.vim
  • prabirshrestha/asyncomplete-lsp.vim

vim-lspの設定を勝手にやってくれる

  • mattn/vim-lsp-settings

をインストールするぐらい。

プラグインマネージャにvim-plugを使っている場合は、

" Install vim-plug
if has('vim_starting')
  let s:plug_dir = expand('~/.local/share/nvim/site/autoload/plug.vim')
  if !filereadable(s:plug_dir)
    execute '!curl -fLo ' . s:plug_dir . ' --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
  endif
endif
" Init vim-plug
let s:plugged_dir = expand('~/.local/share/nvim/plugged')
if !isdirectory(s:plugged_dir)
  call mkdir(s:plugged_dir)
endif

call plug#begin(s:plugged_dir)
Plug 'prabirshrestha/asyncomplete.vim'      " Auto complete
Plug 'prabirshrestha/vim-lsp'               " LSP
Plug 'prabirshrestha/async.vim'             " Execute in asynchronous
Plug 'prabirshrestha/asyncomplete-lsp.vim'  " Add LSP support
Plug 'mattn/vim-lsp-settings'               " Auto configure vim-lsp
call plug#end()

let g:lsp_diagnostics_enabled = 0           " Disable Linter comments (warning, error) from vim-lsp

lsp_diagnostics_enabledはインラインでメッセージが出てきて見にくいので、無効にする。

LanguageServerのインストール

基本的にはmattn/vim-lsp-settings:LspInstallServerを使って問題ないが、システムにインストールする必要があるものや仮想環境との関係で自分でインストールしたほうが良さそう?なものは別にインストールする。

C++

$ sudo pacman -S clangd

CMakeを使っている場合は

$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

して生成されたcompile_commands.jsonCMakeLists.txtと同じディレクトリに置く。具体的には

$ mkdir build
$ cd build
$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
$ mv compile_commands.json ../
$ cd ..

する。

Python

仮想環境内にLanguageServerを入れたくないので別で入れる。

$ pip3 install python-language-server
$ type pyls
pyls is /home/vild/.local/bin/pyls

Ruby

:LspInstallServerでインストールしても補完が動かなかったので、手動でインストールする。

$ gem install solargraph

Ruby2.7系の場合は~/.local/gem/のため~/.bashrc等で

$ export PATH=${PATH}:${HOME}/.local/gem/ruby

しておくと

$ type solargraph
solargraph is /home/vild/.local/ruby/2.7.0/gems/bin/solargraph

となり使用できるようになる。