メインコンテンツまでスキップ
Examples Verified (100%)

Ruby LSPの使用

Ruby LSPはRubyコードにオートコンプリート、定義へ移動、インライン診断などのIDE機能を提供します。T-Rubyと組み合わせると、.rbsファイルの型情報に基づいた豊富なIDEサポートを受けられます。

Ruby LSPとは?

Ruby LSPはRuby用のLanguage Server Protocol実装で、以下を提供します:

  • オートコンプリート - インテリジェントなコード補完
  • 定義へ移動 - メソッド/クラス定義へのナビゲーション
  • ホバー情報 - 型シグネチャとドキュメントの表示
  • 診断 - インライン型エラーと警告
  • コードアクション - クイックフィックスとリファクタリング
  • ドキュメントシンボル - ファイル構造のアウトライン表示

インストール

VS Code

公式Ruby LSP拡張機能をインストール:

  1. VS Codeを開く
  2. Extensions(Ctrl+Shift+X)に移動
  3. "Ruby LSP"を検索
  4. Shopifyの拡張機能をインストール

またはコマンドラインで:

code --install-extension Shopify.ruby-lsp

他のエディタ

Ruby LSPはLSPをサポートするすべてのエディタで動作します:

  • Neovimnvim-lspconfigを使用
  • Emacslsp-modeまたはeglotを使用
  • Sublime TextLSPパッケージを使用
  • IntelliJ:組み込みRubyサポート

T-Ruby用の基本セットアップ

ステップ1:Ruby LSP Gemのインストール

gem install ruby-lsp

またはGemfileに追加:

group :development do
gem "ruby-lsp"
gem "t-ruby"
end

ステップ2:T-Rubyコードのコンパイル

型情報用のRBSファイルを生成:

trc compile src/

これで生成されます:

build/          # コンパイル済みRubyファイル
sig/ # RBS型シグネチャ

ステップ3:Ruby LSPの設定

.vscode/settings.jsonを作成:

{
"rubyLsp.enabledFeatures": {
"diagnostics": true,
"formatting": true,
"documentSymbols": true,
"hover": true,
"completion": true,
"codeActions": true
},
"rubyLsp.indexing": {
"includedPatterns": ["**/*.rb", "**/*.trb"],
"excludedPatterns": ["**/test/**", "**/vendor/**"]
}
}

ステップ4:RBSパスの設定

Ruby LSPにRBSシグネチャの場所を伝える:

{
"rubyLsp.rbs": {
"enabled": true,
"path": "sig"
}
}

T-Ruby専用設定

.trbファイルの操作

VS Codeが.trbファイルをRubyとして扱うように設定:

.vscode/settings.json
{
"files.associations": {
"*.trb": "ruby"
},
"rubyLsp.indexing": {
"includedPatterns": ["**/*.rb", "**/*.trb"]
}
}

構文ハイライト

より良いT-Ruby構文ハイライトのために、TextMate文法を作成するかカスタムルールでRubyハイライトを使用:

{
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": "storage.type.ruby",
"settings": {
"foreground": "#569CD6"
}
}
]
}
}

ワークスペース設定

プロジェクト固有の設定:

.vscode/settings.json
{
// Ruby LSP
"rubyLsp.enabled": true,
"rubyLsp.rubyVersionManager": "auto",

// T-Ruby統合
"rubyLsp.rbs": {
"enabled": true,
"path": "sig"
},

// ファイル関連付け
"files.associations": {
"*.trb": "ruby"
},

// フォーマット
"editor.formatOnSave": true,
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp"
},

// 診断
"rubyLsp.enabledFeatures": {
"diagnostics": true,
"hover": true,
"completion": true
}
}

T-RubyでのIDE機能

オートコンプリート

Ruby LSPはRBS型を使用してインテリジェントなオートコンプリートを提供:

user.trb
class User
@name: String
@email: String

def initialize(name: String, email: String): void
@name = name
@email = email
end

def greet: String
"Hello, #{@name}!"
end
end

user = User.new("Alice", "alice@example.com")
user. # <- オートコンプリートが表示:greet, name, email

コンパイル後、Ruby LSPはsig/user.rbsを読み取り、以下を提供:

  • メソッド候補
  • パラメータ型
  • 戻り値型

定義へ移動

使用箇所から定義へナビゲート:

user = User.new("Alice", "alice@example.com")
# ^^^^ Cmd+クリックでUserクラス定義へ移動

result = user.greet
# ^^^^^ Cmd+クリックでgreetメソッドへ移動

ホバー情報

シンボル上にマウスを置くと型情報を表示:

def process_user(user: User): String
# 'user'にホバーすると:User
# メソッドにホバーすると:(User) -> String
user.greet
end

インライン診断

コーディング中に型エラーをインラインで確認:

def greet(name: String): String
name.upcase
end

# インラインでエラー表示:
greet(123) # Expected String, got Integer
^^^

コードアクション

クイックフィックスとリファクタリング:

class User
def initialize(name: String): void
@name = name
end
# 💡 コードアクション:「@nameに型アノテーションを追加」
end

ドキュメントシンボル

アウトラインビューに構造を表示:

User (class)
├─ @name: String
├─ @email: String
├─ initialize(name, email)
├─ greet()
└─ update_email(email)

Steepとの統合

強化された型チェックのために、Ruby LSPをSteepと一緒に使用:

Steepのインストール

gem install steep

Steepfileの設定

target :app do
check "build"
signature "sig"
end

Steepを使用するようにRuby LSPを設定

.vscode/settings.json
{
"rubyLsp.typechecker": "steep",
"rubyLsp.rbs": {
"enabled": true,
"path": "sig"
}
}

これでRuby LSPが型チェックにSteepを使用し、以下を提供:

  • リアルタイム型エラー
  • より洗練された型推論
  • より良いオートコンプリート

Ruby LSPワークフロー

開発ワークフロー

  1. VS CodeでT-Rubyファイルを編集
  2. ファイルを保存 - T-Rubyウォッチが自動コンパイル
  3. 更新を確認 - Ruby LSPが診断を更新
  4. オートコンプリートを使用 - 更新されたRBSに基づく

ウォッチモードのセットアップ

保存時に自動コンパイルするためにT-Rubyウォッチを使用:

trc watch src/ --exec "echo 'Compiled'"

VS Codeが起動時にこれを実行するように設定:

.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "T-Ruby Watch",
"type": "shell",
"command": "trc watch src/ --clear",
"isBackground": true,
"problemMatcher": [],
"presentation": {
"reveal": "never",
"panel": "dedicated"
},
"runOptions": {
"runOn": "folderOpen"
}
}
]
}

高度な設定

カスタムRBSパス

複数のRBSディレクトリがある場合:

{
"rubyLsp.rbs": {
"enabled": true,
"paths": [
"sig/generated",
"sig/manual",
"vendor/rbs"
]
}
}

フォーマッタ設定

T-Rubyファイル用のフォーマット設定:

{
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp",
"editor.formatOnSave": true,
"editor.tabSize": 2,
"editor.insertSpaces": true
}
}

診断設定

診断表示をカスタマイズ:

{
"rubyLsp.diagnostics": {
"severity": {
"type_error": "error",
"warning": "warning",
"hint": "information"
}
},
"problems.decorations.enabled": true
}

パフォーマンス設定

大規模プロジェクトでパフォーマンスを最適化:

{
"rubyLsp.indexing": {
"includedPatterns": ["src/**/*.trb", "src/**/*.rb"],
"excludedPatterns": [
"**/test/**",
"**/spec/**",
"**/vendor/**",
"**/node_modules/**",
"**/tmp/**"
]
},
"rubyLsp.experimental": {
"parallelIndexing": true
}
}

他のエディタ設定

Neovim

nvim-lspconfigを使用:

-- init.lua
local lspconfig = require('lspconfig')

lspconfig.ruby_lsp.setup({
cmd = { "bundle", "exec", "ruby-lsp" },
filetypes = { "ruby", "trb" },
init_options = {
enabledFeatures = {
"diagnostics",
"formatting",
"documentSymbols",
"hover",
"completion",
"codeActions"
},
rbs = {
enabled = true,
path = "sig"
}
}
})

-- .trbをRubyとして扱う
vim.filetype.add({
extension = {
trb = "ruby"
}
})

Emacs

lsp-modeを使用:

;; init.el
(use-package lsp-mode
:hook ((ruby-mode . lsp))
:config
(setq lsp-ruby-lsp-rbs-enabled t)
(setq lsp-ruby-lsp-rbs-path "sig")
(add-to-list 'auto-mode-alist '("\\.trb\\'" . ruby-mode)))

Sublime Text

Ruby LSP.sublime-settings
{
"clients": {
"ruby-lsp": {
"enabled": true,
"command": ["bundle", "exec", "ruby-lsp"],
"selector": "source.ruby",
"initializationOptions": {
"enabledFeatures": {
"diagnostics": true,
"hover": true,
"completion": true
},
"rbs": {
"enabled": true,
"path": "sig"
}
}
}
}
}

トラブルシューティング

Ruby LSPが動作しない

問題:オートコンプリートや診断がない。

解決

  1. Ruby LSPが実行中か確認:

    ps aux | grep ruby-lsp
  2. VS CodeでRuby LSPを再起動:

    • Cmd+Shift+P → "Ruby LSP: Restart"
  3. 出力パネルを確認:

    • View → Output → "Ruby LSP"を選択

RBSファイルが見つからない

問題:Ruby LSPが型情報を見つけられない。

解決

  1. RBSファイルが存在するか確認:

    ls sig/
  2. 設定でRBSパスを確認:

    {
    "rubyLsp.rbs": {
    "enabled": true,
    "path": "sig"
    }
    }
  3. VS Codeウィンドウをリロード:

    • Cmd+Shift+P → "Developer: Reload Window"

古い診断

問題:コンパイル後に診断が更新されない。

解決

  1. T-Rubyウォッチが実行中か確認:

    trc watch src/
  2. 手動でRuby LSPを更新:

    • ファイルを保存(Cmd+S)
    • またはRuby LSPを再起動

パフォーマンス問題

問題:Ruby LSPでエディタが遅い。

解決

  1. 不要なディレクトリを除外:

    {
    "rubyLsp.indexing": {
    "excludedPatterns": [
    "**/vendor/**",
    "**/node_modules/**",
    "**/tmp/**"
    ]
    }
    }
  2. 使用しない機能を無効化:

    {
    "rubyLsp.enabledFeatures": {
    "diagnostics": true,
    "hover": true,
    "completion": true,
    "formatting": false,
    "codeActions": false
    }
    }

型情報がない

問題:ホバー時に型情報が表示されない。

解決

  1. RBSが生成されたか確認:

    cat sig/user.rbs
  2. ホバーが有効か確認:

    {
    "rubyLsp.enabledFeatures": {
    "hover": true
    }
    }
  3. RBSが有効か確認:

    rbs validate sig/

ベストプラクティス

1. Ruby LSPを実行し続ける

VS Codeワークスペースと一緒にRuby LSPを自動起動。

2. ウォッチモードを使用

開発中は常にT-Rubyウォッチを実行:

trc watch src/ --clear

3. RBSファイルをコミット

チームメンバーのためにRBSファイルをバージョン管理に保持:

git add sig/
git commit -m "Update RBS signatures"

4. パフォーマンスのための設定

大規模プロジェクトで不要なディレクトリを除外:

{
"rubyLsp.indexing": {
"excludedPatterns": ["**/vendor/**", "**/tmp/**"]
}
}

5. Steep統合を使用

最高の型チェックのためにSteepを有効化:

{
"rubyLsp.typechecker": "steep"
}

完全なVS Codeセットアップ

T-Ruby開発用の完全なVS Code設定:

.vscode/settings.json
{
// Ruby LSP設定
"rubyLsp.enabled": true,
"rubyLsp.rubyVersionManager": "auto",
"rubyLsp.typechecker": "steep",

// RBS統合
"rubyLsp.rbs": {
"enabled": true,
"path": "sig"
},

// 機能
"rubyLsp.enabledFeatures": {
"diagnostics": true,
"formatting": true,
"documentSymbols": true,
"hover": true,
"completion": true,
"codeActions": true,
"inlayHints": true
},

// インデックス
"rubyLsp.indexing": {
"includedPatterns": ["src/**/*.rb", "src/**/*.trb"],
"excludedPatterns": [
"**/test/**",
"**/spec/**",
"**/vendor/**",
"**/tmp/**"
]
},

// ファイル関連付け
"files.associations": {
"*.trb": "ruby"
},

// フォーマット
"editor.formatOnSave": true,
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp",
"editor.tabSize": 2,
"editor.insertSpaces": true
},

// エディタの外観
"editor.inlayHints.enabled": "on",
"problems.decorations.enabled": true,

// ターミナル
"terminal.integrated.env.osx": {
"RUBYOPT": "-W0"
}
}
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "T-Ruby Watch",
"type": "shell",
"command": "trc watch src/ --clear",
"isBackground": true,
"problemMatcher": [],
"presentation": {
"reveal": "never",
"panel": "dedicated"
},
"runOptions": {
"runOn": "folderOpen"
}
},
{
"label": "Steep Watch",
"type": "shell",
"command": "steep watch --code=build --signature=sig",
"isBackground": true,
"problemMatcher": [],
"presentation": {
"reveal": "never",
"panel": "dedicated"
}
}
]
}
.vscode/extensions.json
{
"recommendations": [
"shopify.ruby-lsp",
"rebornix.ruby",
"castwide.solargraph"
]
}

キーボードショートカット

Ruby LSP使用時に便利なVS Codeショートカット:

操作ショートカット(Mac)ショートカット(Windows/Linux)
定義へ移動Cmd+クリックCtrl+クリック
定義へ移動F12F12
定義をプレビューAlt+F12Alt+F12
参照を検索Shift+F12Shift+F12
シンボルの名前変更F2F2
ドキュメントをフォーマットShift+Alt+FShift+Alt+F
ホバーを表示Cmd+K Cmd+ICtrl+K Ctrl+I
候補をトリガーCtrl+SpaceCtrl+Space
クイックフィックスCmd+.Ctrl+.
LSPを再起動Cmd+Shift+P → "Ruby LSP: Restart"Ctrl+Shift+P → "Ruby LSP: Restart"

次のステップ