Rubyでの開発の為にSolargraphを導入する

はじめに

Rubyでの開発にあたり、gem solargraphVscode拡張機能を使用して導入したのでその振り返りです。

Solargraphとは

https://raw.githubusercontent.com/castwide/vscode-solargraph/master/vscode-solargraph-0.34.1.gif

引用: GitHub - castwide/vscode-solargraph: A Visual Studio Code extension for Solargraph.

上のgifのようにRubyのコード補完、ドキュメントを提供してくれるライブラリです。
また、ファイル保存時にrubocopによるLint errorを検知してくれます。

導入方法

今回はsystemにsolargraphをインストールするやり方で行っていきます。

Image from Gyazo

  • gemをインストールする
gem install solargraph
  • 再読み込みを行う。

以上で完了です。

Railsで使用する場合

Railsで使用する場合は、デフォルトの設定のままだと補完機能が働かないみたいです。
その為、ワークツリーの直下に.solargraph.ymlを作成し、設定を追加する必要があるみたいです。

参考: Rails support · Issue #87 · castwide/solargraph · GitHub

.solargraph.ymlを作成する

以下を実行します

solargraph config .

solargraph.ymlを修正する

インデントに注意してください!!

include:
  - "**/*.rb"
exclude:
  - spec/**/*
  - test/**/*
  - vendor/**/*
  - ".bundle/**/*"
require:
  - actioncable
  - actionmailer
  - actionpack
  - actionview
  - activejob
  - activemodel
  - activerecord
  - activestorage
  - activesupport
domains: []
reporters:
  - rubocop
  - require_not_found
max_files: 5000

include, exclude

デフォルトのままです。

require

先程述べたようにコード補完が働くようにする為、Railsで実装してあるclassをrequireしています。(issueより)

reporters

デフォルトのままです。 構文チェックを行う際に使用するtoolを設定できます。 デフォルトではrubocop

max_file

ワークスペースで対象になるファイルの最大数です。

詳しくは=> https://solargraph.org/guides/configuration