VS Code上のRubyのLinter/Formatterを設定してみた
LSP
Language server protocol。補完するための機能たち。
の 2 種類がある模様。
The Ruby LSP features include
- Semantic highlighting
- Symbol search and code outline
- RuboCop errors and warnings (diagnostics)
- Format on save (with RuboCop or Syntax Tree)
- Format on type
- Debugging support
- Running and debugging tests through VS Code's UI
- Go to definition for classes, modules, constants and required files
- Showing documentaton on hover for classes, modules and constants
- Completion for classes, modules, constants and require paths
- Fuzzy search classes, modules and constants anywhere in the project and its dependencies (workspace symbol)
とあるので、Ruby LSP の方でよさそう。
インストール
プラグイン自体のインストール
VS Code で Ruby LSP を探してインストールする。
プラグインの設定
その後、 setting.json
を開いて Ruby 向けの設定を追記する。
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp", // Ruby LSPの機能を使ってコードを整形するぞ、宣言。Ruby LSPは裏でRubocopとかを使うらしいです
"editor.formatOnSave": true, // 保存時にファイルを整形する
"editor.tabSize": 2, // インデントはスペース2つ分
"editor.insertSpaces": true, // インデントはスペースを使う
"editor.semanticHighlighting.enabled": true, // Semantic highlightingってなんだ?
"editor.formatOnType": true, // 入力中のフォーマットを有効にする
},
Rubocop のインストール
各プロジェクトで導入するのがよさそう。
Gemfile
の :development
内に、 rubocop
を追加する。VS Code 側で何かする必要はない。
group :development do
# ...
gem 'rubocop', require: false
end
使い方
自動補完
"Ruby LSP Start" から補完機能をスタートさせる。ちゃんと効いているか、いまいち怪しい…
Linter
書き方が微妙なところを指摘するやつ。
勝手に Rubocop が実行されて、怪しい場所がエディタで見えるようになる。
Formatter
↑ の設定を入れておくと、保存したときに勝手にコードを整形してくれる。