LoginSignup
9
4

More than 5 years have passed since last update.

RLS(Rust Language Server)をVimで使う

Posted at

前提

MacOSX or Linux環境でのみ動作確認しました。
RustはNightly(1.17)前提です。

$ rustup default nightly

RLSのビルドとセットアップ

$ cd $HOME
$ git clone https://github.com/rust-lang-nursery/rls.git
$ cd rls
$ cargo build --release

.bash_profile あたりに環境変数を設定しておきます。

export LD_LIBRARY_PATH=$HOME/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib
export RLS_ROOT=/PATH/TO/rls

標準ライブラリのメタデータをダウンロードしておきます。

$ rustup component add rust-analysis

動かしてみます

$ cargo run --release --manifest-path=$HOME/rls/Cargo.toml
    Finished release [optimized] target(s) in 0.1 secs
     Running `codereading/rls/target/release/rls`
Content-Length: 60

{"jsonrpc": "2.0", "id": 1, "method": "exit", "params": {}}
$

動きました。デフォルトだと標準入力からコマンドを受け付けているようです。

Vimの設定

Language Serverとのやりとりには prabirshrestha/vim-lsp を使います。

.vimrc
call plug#begin('~/.vim/plugged')
Plug 'prabirshrestha/async.vim'
Plug 'prabirshrestha/vim-lsp'
call plug#end()

Vimを起動してプラグインをインストールします。

:PlugInstall

RLS用にftpluginを追加します。
https://raw.githubusercontent.com/prabirshrestha/vim-lsp/master/example.vim
これを編集して、 $HOME/.vim/ftplugin/rust/rls.vim あたりに保存します。

以下を追加。パスは適当に修正してください。

.vim/ftplugin/rust/rls.vim
function! s:get_root_uri_for_rust()
    return s:path_to_uri(expand('~/rls'))  " cloneしたrlsのディレクトリ
endfunction

function! s:get_lsp_server_cmd_for_rust()
    return ['rustup', 'run', 'nightly', 'cargo', 'run', '--release', '--manifest-path=/home/vagrant/rls/Cargo.toml']
endfunction

VimでRustのファイルを開いたときにRLSが動いてるか確認

$ ps aux | grep cargo
vagrant  14348  0.0  0.4  28876  8920 ?        Ss   15:11   0:00 rustup run nightly cargo run --release --manifest-path=/home/vagrant/rls/Cargo.toml

OK

使ってみる

適当なワードにカーソルを合わせて以下を入力すると定義位置にジャンプできます。

:GoToDefinition

$HOME/.vim/ftplugin/rust/rls.vim をもう少し改造してさらに使いやすくできるのではないでしょうか。

以上です。

9
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
4