LoginSignup
1
1

More than 3 years have passed since last update.

Vim + rust-analyzerでrustcを読むときのTips

Posted at

はじめに

Vim + rust-analyzerの環境でrustcを読もうとする際のおすすめ設定みたいなものを紹介します。対象をrustcとしていますが、厳密に言えばcargo checkが一般的な動作にならないRustプロジェクトに適用可能なものになります。

設定

rustc-dev-guideにも記載がありますが、rust-analyzerはデフォルトでcargo checkを利用しています。ですがrustcはそれに相当するものとして./x.py checkを使用します。そのため、チェック用のコマンドを上書きしておく必要があります。

ガイドではVSCodeを例としていますが、Vim環境の場合はcoc.nvimに対して設定することになります。ですが、グローバルな設定としてコマンドを上書きしてしまうと、rustc以外の通常のRustプロジェクトを開発する場合に対応できなくなってしまいます。そこで、coc.nvimの設定方法を見てみると、プロジェクト直下に.vimディレクトリを作り、そこにcoc-settings.jsonを配置することで特定のプロジェクトにのみ設定を行うことができそうです。

rust/.vim/coc-settings.json
{
    "rust-analyzer.checkOnSave.overrideCommand": [
        "./x.py",
        "check",
        "--json-output"
    ],
    "rust-analyzer.rustfmt.overrideCommand": [
      "./build/TARGET_TRIPLE/stage0/bin/rustfmt"
    ],
    "editor.formatOnSave": true
}

これでrustcのプロジェクトに対してのみ、チェック用のコマンドを上書きすることができました。

rust-analyzerの設定はできましたが、プロジェクト内に新たなディレクトリを作成したので、このままでは.vimがGitで引っかかってしまい使いづらくなってしまいます。プロジェクトの.gitignoreを見ても、.vimは無視する対象に含まれていないようです。ちなみに.vscodeは含まれています、、.vimも追加するように要望を出していいかもしれませんが、冒頭のコメントには.git/info/excludeを使うように書いてあります。

rust/.gitignore
# This file should only ignore things that are generated during a `x.py` build,
# generated by common IDEs, and optional files controlled by the user that
# affect the build (such as config.toml).
# In particular, things like `mir_dump` should not be listed here; they are only
# created during manual debugging and many people like to clean up instead of
# having git ignore such leftovers. You can use `.git/info/exclude` to
# configure your local ignore list.

なので素直にそうしましょう。.git/info/excludeに記載することで、プロジェクトの.gitignoreに記載することなく、ローカルのファイルを無視することができます。

rust/.git/info/exclude
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.vim/

おまけ

rust-analyzerはrustcみたいな巨大なプロジェクトを読み込むと結構メモリを消費するようなので、Vimを複数立ち上げて使ったりするとそれなりの負担になってしまいます。タブとかを使うことでうまく効率化できるので、そのあたりはマシンスペックと相談しながら使っていくといいのではないでしょうか。

1
1
0

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
1
1