deopleteのソースをJediからLSPに変更する。

ついでにvimtexまわりの変更も行います。

JediからLanguage Serverへ

約2年前にNeovim+vimtex+TeXLive2018でTeX環境を作ってみる。というエントリを書いたのですが、ここへきて Neovim の 0.5.0 がぼちぼち(予定では6月15日)にリリースされるということで、Nightly ビルドを持ってきて使っています。

もともとは Python プログラミングの補完などのためにdeoplete-jediを使っていましたが、jediよりも速いという話もあってLSPを利用した入力サポートへの変更を行いました。

Python用のLanguage Server(LS)はいくつかありますが、VS Codeと併用するならばVS Codeで使用されている Pylance のベースとなっているMicrosoft Python Language Serverが、一番違和感なく使えるのではないかということでこれを候補にしました。
他にもpalantir/python-language-serverhttps://github.com/python-lsp/python-lsp-serverにありますが、こちらはエンジン?にJediを利用するということで、deoplete-jediで使っているのと変わらないためにパスしています。また、NeovimをVS Codeのような感じにするというCoC(Conquer of Completion)というフレームワークがあり、そのプラグインで使うという方法も調べてみましたが、こちらはNode.jsで動くということで、Node.jsを使う予定はないのでこれもパスしました。

結果、構成としては以下のようになります。
  • Neovim + deoplete を利用する
  • deoplete からLSを利用するため、deoplete-lsp プラグインを利用する
  • LSPとしてはMicrosoft Python Language Server(pyls_ms)を使用する
  • pyls_msの設定を簡単に行うため、nvim-lspconfigを使用する

pyls_msをビルドする

Microsoft Python Language ServerはC#で記述されているので、.NETのビルド環境が必要です。うちではVisual Studio 2019がインストールされているので、コマンドプロンプトから以下のコマンドを叩けばビルドできます。
$ git clone https://github.com/Microsoft/python-language-server.git
$ cd python-language-server/src/LanguageServer/Impl
$ dotnet build -c Release
ビルドが正常に終了したらpython-language-server/output/binディレクトリにある全ファイルをc:/Apps/languageServerディレクトリにコピーします。

deoplete-lspを導入する

dein.tomlを編集し、deoplete-jediを削除したあとで以下の記述を追加します。
[[plugins]]
# LSP Completion source for deoplete
repo = 'deoplete-plugins/deoplete-lsp'
hook_add = '''
let g:deoplete#lsp#handler_enabled=1
let g:deoplete#lsp#use_icons_for_candidates=0

'''

nvim-lspconfigを導入する

続けて以下の記述を同じくdein.tomlに追加します。
[[plugins]]
# A collection of common configurations for Neovim's built-in language server client.
repo = 'neovim/nvim-lspconfig'
hook_add = '''
set runtimepath+=~/.cache/dein/repos/github.com/neovim/nvim-lspconfig/lua
lua << EOF
local nvim_lsp = require('lspconfig')

nvim_lsp.pyls_ms.setup{
    cmd = { "dotnet", "exec", "c:/Apps/languageServer/Microsoft.Python.LanguageServer.dll" },
	init_options = {
	  analysisUpdates = true,
	  asyncStartup = true,
	}
}
EOF
'''
一応このとき、dein_lazy.tomlでのdeopleteの設定は以下のようになります。
[[plugins]]
# Dark powered asynchronous completion framework for Neovim / Vim8
# require: python3
repo = 'Shougo/deoplete.nvim'
depends = 'context_filetype.vim'
on_i = 1
hook_post_source = '''
  call deoplete#enable()
  source $XDG_CONFIG_HOME/nvim/rc/deoplete.rc.vim
'''

起動する

設定が終わったらインストールとキャッシュの作り直しをしておきます。
:UpdateRemotePlugins
:call dein#update()
:call dein#recache_runtimepath()
Neovimを再起動してPythonファイルを編集し、補完候補の右側に[LSP]と出てくればちゃんと動いています。

0 件のコメント:

コメントを投稿

AstroNvimでtelescope.nvimがエラーを吐いたとき。

Windowsの環境でAstroNvimをインストールして、Find Fileしたらtelescopeがエラーを吐いてきました。 メッセージは、 Failed to run `config` for telescope.nvim ...a/lazy/telescope.nvim/...