Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Shopify/ruby-lsp で快適な Ruby 生活を始めよう / introduction-shopify-ruby-lsp

igsr5
March 06, 2024
1k

Shopify/ruby-lsp で快適な Ruby 生活を始めよう / introduction-shopify-ruby-lsp

2024/03/06 Gotanda.rb#57 オフライン開催 の資料です。
https://gotanda-rb.connpass.com/event/310542/

igsr5

March 06, 2024
Tweet

Transcript

  1. はじめに Ruby 書いてるとこんなこと思いませんか? 言語機能は RubyMine が一番良いんだけど重たいな、、 IntelliJ IDEA 系の操作がいまいち手に馴染まないな、、 かといって

    VSCode や Vim だと Ruby 書きにくいしな、、 良い LSP がない、、 ( 筆者は YARD 書きたくないので solargraph 使ってない人です) RubyMine 一強な環境へのツラミ 4
  2. 目次 1. LSP (Lauguage Server Protocol) って? 2. Shopify/ruby-lsp のススメ

    3. Shopify/ruby-lsp の辛いところ 4. LSP 以外で Ruby を書くときに工夫していること 7
  3. 前提: LSP (Language Server protocol) Official page for Language Server

    Protocol より Language Server とは特定言語において高度な言語機能を提供するサ ーバーのこと 例えば補完やフォーマッタなどの機能など The Language Server Protocol (LSP) defines the protocol used between an editor or IDE and a language server that provides language features like auto complete, go to definition, find all references etc. “ “ 8
  4. LSP とは Language Server とは特定言語において高度な言語機能を提供する サーバーを指す そして、 LSP (Language Server

    Protocol) とはこの Language Server と開発ツール間の通信プロトコルを指す このプロトコルのおかげで、世のエディタは高度な言語機能を特定 言語に依存しない形で統一的に組み込むことができる VSCode の Ruby 拡張、Vim の Ruby Plugin 、etc みたいに分散しなく て良くて、ユーザーも開発者も嬉しい! 9
  5. 余談) LSP の動作原理 要点のみ抜粋 LSP はサーバークライアント方式 Development Tool ( クライアント)

    は JSON-RPC を用いて Language Server に Notification あるいは Request を送信する そうすると Language Server は Development Tool に Notification あるいは Response を返す LSP 実装が全てのプロトコルに対応しているとは限らない LSP にはある Language Server ・Development Tools が提供可能 な言語機能のセットを表す Capabilities という概念がある 11
  6. 特徴 LSP 実装なのでエディタを問わず利用可能 高パフォーマンス コードの型チェックや型注釈を必要としない (DRY) 以前は Go To Definition

    サポートが薄くて避けられていたが、 v0.13.3 (2024/01/12) のリリースで methods の対応が入って解消 外部 gem のコードも参照可能 Shopify によって活発に開発されている 改善の勢いがすごい リリースノート https://github.com/Shopify/ruby-lsp/releases 13
  7. インストール方法 VSCode https://marketplace.visualstudio.com/items? itemName=Shopify.ruby-lsp から拡張機能をインストール それ以外 gem install ruby-lsp .

    その後 https://github.com/Shopify/ruby- lsp/blob/main/EDITORS.md を参考に LSP Client をセットアップ 14
  8. 例: Go To Definition methods, classes, modules, constants や required

    files に対応 可能性のあるコードを列挙 15
  9. 例: Diagnostic / Code Action rubocop の error や warning

    を表示。autocorrect にも対応。 18
  10. 高速に動作する Code Indexing という仕組みで事前にパースした AST をオンメモリに 保持しているので高速 Code indexing: How

    language servers understand our code を読む とわかりやすい。(RubyKaigi 2023 で Shopify のエンジニアが発表) 19
  11. Shopify/ruby-lsp の辛いところ 今後に期待 methods の Go To Definition は出来ないものもある RubyMine

    の方がまだ上手 プロダクト開発には気にならないレベルだが、よく知らない gem のデバッグを調査をするときなどは辛い addon (ruby-lsp-rspec, ruby-lsp-rails) の開発はまだまだこれから 所々バグっていたりする テストケース ( it block) が認識されないなど、、 20
  12. 他に Ruby 開発で工夫していること RSpec 実行は vscode-run-rspec-file がシンプルで便利 Rails であれば Rails

    DB Schema で schema.rb をもとに補完可能 Ruby はあくまで動的型付け言語という意識をもって、常に動かし ながら実装を進める 数行コード書いたらすぐにテスト実行する癖をつける 本気でバグ調査を行うときは RubyMine を開く 21
  13. 参考資料 (1) LSP 解説 過去の自分のブログ https://igsr5.hatenablog.com/entry/2023/12/18/015901 公式 https://microsoft.github.io/language-server-protocol/ Shopify/ruby-lsp 解説

    リポジトリ https://github.com/Shopify/ruby-lsp VSCode 拡張 https://marketplace.visualstudio.com/items? itemName=Shopify.ruby-lsp ruby-lsp-rspec addon https://github.com/st0012/ruby-lsp-rspec? tab=readme-ov-file ruby-lsp-rails addon https://github.com/Shopify/ruby-lsp-rails 23
  14. 参考資料 (2) Shopify/ruby-lsp 解説 ( 続き) Shopify による紹介ブログ https://shopify.engineering/improving-the- developer-experience-with-ruby-lsp

    Shopify による内部実装解説スライド https://speakerdeck.com/vinistock/code-indexing-how-language-servers- understand-our-code Solargraph との比較を議論している issue https://github.com/Shopify/ruby- lsp/issues/419 24
  15. 参考資料 (3) 便利 VSCode 拡張 vscode-run-rspec-file https://marketplace.visualstudio.com/items? itemName=Thadeu.vscode-run-rspec-file Rails DB

    Schema https://marketplace.visualstudio.com/items? itemName=aki77.rails-db-schema テスト (RSpec) を書くときに意識すべきこと テストコードを負債化させない上手な付き合い方 https://speakerdeck.com/igsr5/test-code-management 信頼できるテストを書く https://speakerdeck.com/igsr5/achieving-100- percent-test 25