編輯器

此檔案包含社群驅動的指南,說明如何在 VS Code 以外的編輯器中設定 Ruby LSP。對於 VS Code,請使用官方的 Ruby LSP 擴充功能

由於語言伺服器協議(Language Server Protocol)的實作不完整,某些 Ruby LSP 功能可能無法使用或受到限制,例如動態功能註冊或 檔案監看

如果您希望啟用或停用功能,或設定語言伺服器的其他方面,請參閱初始化選項

啟動語言伺服器的命令可能取決於您使用的編輯器和版本管理器的組合。為了正常運作,Ruby LSP 必須使用您正在處理的專案所使用的 Ruby 版本啟動,並設定正確的 Bundler 環境。

如果您通常從終端機在已啟用 Ruby 環境的 shell 會話中啟動編輯器,那麼您可能只需使用 ruby-lsp 作為命令。

如果您看到與找不到正確的 gem 或無法找到 ruby-lsp 執行檔相關的問題,則您可能需要確保在嘗試執行 ruby-lsp 執行檔之前,版本管理器已正確設定環境。如何執行此操作將取決於您使用的版本管理器。以下是一些範例:

如果您的版本管理器公開了一個命令,可以在當前 Ruby 的環境中執行一個執行檔,請使用該命令

  • mise x -- ruby-lsp
  • shadowenv exec -- ruby-lsp

如果您的版本管理器建立執行 gem 的 shim,可以執行自動版本切換,請使用這些 shim

  • ~/.rbenv/shims/ruby-lsp
  • ~/.asdf/shims/ruby-lsp

如果您的版本管理器未提供以上任何一種方法,請啟用環境並執行執行檔

  • chruby $(cat .ruby-version) && ruby-lsp

這些策略將確保使用正確的 Ruby 版本、GEM_HOMEGEM_PATH 來調用 ruby-lsp 執行檔,這對於與您的專案正確整合至關重要。

所有初始化選項

每個 LSP 客戶端都可以在啟動時控制 LSP 的各種功能。以下 JSON 字典包含所有可用的初始化選項。通常,編輯器 LSP 客戶端將使用其組態語言(JSON、Lua、ELisp 等)中的字典來設定 LSP 伺服器。

{
  "initializationOptions": {
    "enabledFeatures": {
      "codeActions": true,
      "codeLens": true,
      "completion": true,
      "definition": true,
      "diagnostics": true,
      "documentHighlights": true,
      "documentLink": true,
      "documentSymbols": true,
      "foldingRanges": true,
      "formatting": true,
      "hover": true,
      "inlayHint": true,
      "onTypeFormatting": true,
      "selectionRanges": true,
      "semanticHighlighting": true,
      "signatureHelp": true,
      "typeHierarchy": true,
      "workspaceSymbol": true
    },
    "featuresConfiguration": {
      "inlayHint": {
        "implicitHashValue": true,
        "implicitRescue": true
      }
    },
    "indexing": {
      "excludedPatterns": ["path/to/excluded/file.rb"],
      "includedPatterns": ["path/to/included/file.rb"],
      "excludedGems": ["gem1", "gem2", "etc."],
      "excludedMagicComments": ["compiled:true"]
    },
    "formatter": "auto",
    "linters": [],
    "experimentalFeaturesEnabled": false
  }
}

Emacs Eglot

Eglot 預設執行 solargraph 伺服器。若要將其設定為使用 ruby-lsp,您需要在您的初始化檔案中放入以下內容

(with-eval-after-load 'eglot
 (add-to-list 'eglot-server-programs '((ruby-mode ruby-ts-mode) "ruby-lsp")))

當您執行 eglot 命令時,它會為您執行 ruby-lsp 處理程序。

Neovim

注意:請確保您使用的是 Neovim 0.10 或更新版本。

nvim-lspconfig

nvim-lspconfig 外掛程式支援 Ruby LSP。

在設定 LSP 時,可以使用 init_options 鍵來設定 Ruby LSP。

一個很好的設定範例是啟用 Ruby LSP 的 Standard 附加元件,以啟用格式化和 pull 風格的診斷。以下程式碼片段啟用 standard 用於格式化和 pull 診斷 linting。

local lspconfig = require('lspconfig')
lspconfig.ruby_lsp.setup({
  init_options = {
    formatter = 'standard',
    linters = { 'standard' },
  },
})

Mason

您可以將 mason.nvimmason-lspconfig.nvim 一起使用

local capabilities = vim.lsp.protocol.make_client_capabilities()
local mason_lspconfig = require("mason-lspconfig")
local servers = {
  ruby_lsp = {},
}

mason_lspconfig.setup {
  ensure_installed = vim.tbl_keys(servers),
}

mason_lspconfig.setup_handlers {
  function(server_name)
    require("lspconfig")[server_name].setup {
      capabilities = capabilities,
      on_attach = on_attach,
      settings = servers[server_name],
      filetypes = (servers[server_name] or {}).filetypes,
    }
  end
}

使用 Mason 管理您的 Ruby LSP 安裝可能會導致錯誤

Mason 將 Ruby LSP 安裝在您的所有 Ruby 版本共用的資料夾中。某些 Ruby LSP 相依性是 C 擴充功能,它們依賴 Ruby ABI 在連結到 Ruby 時以某種方式查找和運作。當使用共用資料夾時,這會導致問題。

請參閱 此問題 以取得更多資訊。

其他設定 (選用)

rubyLsp/workspace/dependencies 是一種自訂方法,目前僅在 VS Code 外掛程式中支援。以下程式碼片段新增 ShowRubyDeps 命令,以在快速修復清單中顯示相依性。

local function add_ruby_deps_command(client, bufnr)
  vim.api.nvim_buf_create_user_command(bufnr, "ShowRubyDeps", function(opts)
    local params = vim.lsp.util.make_text_document_params()
    local showAll = opts.args == "all"

    client.request("rubyLsp/workspace/dependencies", params, function(error, result)
      if error then
        print("Error showing deps: " .. error)
        return
      end

      local qf_list = {}
      for _, item in ipairs(result) do
        if showAll or item.dependency then
          table.insert(qf_list, {
            text = string.format("%s (%s) - %s", item.name, item.version, item.dependency),
            filename = item.path
          })
        end
      end

      vim.fn.setqflist(qf_list)
      vim.cmd('copen')
    end, bufnr)
  end,
  {nargs = "?", complete = function() return {"all"} end})
end

require("lspconfig").ruby_lsp.setup({
  on_attach = function(client, buffer)
    add_ruby_deps_command(client, buffer)
  end,
})

LazyVim LSP

從 v12.33.0 開始,Ruby LSP 是 Ruby 的預設 LSP。

為了確保選取正確的 Ruby 版本,我們建議停用 mason 選項,並將您的 Ruby 版本管理器的適當命令指定為絕對路徑。例如:

return {
  {
    "neovim/nvim-lspconfig",
    opts = {
      servers = {
        ruby_lsp = {
          mason = false,
          cmd = { vim.fn.expand("~/.asdf/shims/ruby-lsp") },
        },
      },
    },
  },
}

Sublime Text LSP

若要使用 Sublime Text 的 LSP 設定 Ruby LSP,請將以下設定新增至您的 LSP 客戶端設定

"clients": {
  "ruby-lsp": {
    "enabled": true,
    "command": [
      "ruby-lsp"
    ],
    "selector": "source.ruby",
    "initializationOptions": {
      "enabledFeatures": {
        "diagnostics": false
      },
      "experimentalFeaturesEnabled": true
    }
  }
}

重新啟動 LSP 或 Sublime Text,當開啟 ruby 檔案時,ruby-lsp 將自動啟用。

Zed

設定 Ruby LSP

Zed 已在 Ruby 擴充功能的 v0.0.2 版本中新增對 Ruby LSP 作為替代語言伺服器的支援

請參閱 https://github.com/zed-industries/zed/issues/4834 以討論限制。

RubyMine

您可以透過以下外掛程式將 Ruby LSP 與 RubyMine (或 IntelliJ IDEA Ultimate) 一起使用。

請注意,由於 IDE 提供了與 Ruby LSP 提供的功能相似的功能,因此將其與 RubyMine 一起使用時,可能會存在功能重疊的情況。

Ruby LSP 外掛程式

Kate

Kate 的 LSP 客戶端外掛程式預設設定為對 Ruby 使用 Solargraph。若要將其與 Ruby LSP 一起使用,您可以在 LSP 客戶端外掛程式的「使用者伺服器設定」中覆寫特定組態項目,如下所示

{
  "servers": {
    "ruby": {
      "command": ["ruby-lsp"],
      "url": "https://github.com/Shopify/ruby-lsp"
    }
  }
}

Kate 將為任何符合 rootIndicationFileNames 的 Ruby 專案在背景中啟動 Ruby LSP 伺服器的實例。如果啟動 Ruby LSP 成功,則會啟用 LSP-Client 選單中的項目。否則,可以在輸出視窗中檢查錯誤輸出。