2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

VSCodeのformatterをカスタマイズする

Posted at

Atomも開発終了してしまったことだし、新しく勉強するRustはVSCodeで環境を作ろうと思ったのですが、なんか全然資料が見つからなくて苦労したのでメモしておきます。

例はRustのformatterですが、言語はどれでもいいはず。たぶん恐らく。

手順

formatterの用意

image.png
 とりま拡張機能のrust-analyzer(公式が出してるやつです。Rustとかいうのが使えなくなってこれに代替わりしたらしい)を入れます。これにrustfmtというrust用のformatterも含まれています。
 他の言語の方はそれ用の物をお願いします。VSCodeは詳しくないので。

formatter設定ファイルの登録

左下から適当にSettingsを開きます。
image.png
次におもむろにformatter名、ここではrustfmtで検索します。
その後、image.png
Rust-analyzer › Rustfmt: Extra Args(Rust以外ならArgsってついてるやつ)の中のAdd Itemを押し、--config-pathと入力した後、OKと押します。もうこの時点で全部察したっていう人が大半だと思います。
image.png
次に.rustfmt.tomlの存在するフォルダのPathを入力します。ファイルをのパスを直接でもいいと思います。フルパスです。エスケープはVSCodeがやってくれるのでいりません。
他のformatterの方は専用のコマンドのヘルプを見るか、ぐぐってください。
image.png
上記は一応.rustfmt.tomlの中身です。

ちなみに、setting.jsonを見ると、
image.png
こんな感じに増えてます。
"editor.formatOnSave": trueはsaveした時に勝手にformatterをかけるというVSCodeの機能ですね。
ちなみに、
image.png
のようにも書けますが、まだ安定していない設定(まだnightly Rustとかいうのでしか使えないらしい)を盛り込むとerror?を吐いてformatterは機能しません。設定ファイルになら組み込んでも問題ないのでそうしましょう。いつか使えるようになることを祈っておきます。
あとrustfmtの設定一覧です。なぜかVer1.5で開きますが。

参考

VSCodeRustを始めようと思って調べ始めたサイトたちなので若干関係ない物も入っています。インスピレーションをもらっただけというものや基礎勉強も多々です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?