diff --git a/Makefile b/Makefile index e325ac2..1ef6a82 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,9 @@ panvimdoc: lint: stylua --check lsp/ lua/ tests/ +# the C locale keeps panvimdoc from mangling emoji when it uppercases headings docs: panvimdoc - $(PANVIMDOC_DIR)/panvimdoc.sh \ + LC_ALL=C $(PANVIMDOC_DIR)/panvimdoc.sh \ --project-name ruby-lsp.nvim \ --input-file README.md \ --vim-version "NVIM v0.12.0" \ diff --git a/README.md b/README.md index 148942a..3621eb5 100644 --- a/README.md +++ b/README.md @@ -59,25 +59,59 @@ Don't lazy-load the plugin. Its `lsp/ruby_lsp.lua` must be on the runtimepath be If you're upgrading and already start Ruby LSP yourself, remove your `vim.lsp.enable("ruby_lsp")` call and your `fullTestDiscovery` setting. Or set `lsp.auto_start = false` and keep your `vim.lsp.enable("ruby_lsp")` call, as long as the plugin still isn't lazy-loaded. +## 🔍 Code Lenses + +The plugin turns on code lenses in buffers Ruby LSP is attached to, so **Run**, **Run In Terminal**, and **Debug** show above your tests. Run the lens on the cursor line with `grx`, Neovim's default mapping for `vim.lsp.codelens.run()`. To use a different key, map it yourself: + +```lua +vim.keymap.set("n", "cl", vim.lsp.codelens.run, { desc = "run code lens" }) +``` + +Set `codelens = false` to leave code lenses to your own config. With it on, the plugin enables code lenses for every server attached to a Ruby buffer, which overrides a global `vim.lsp.codelens.enable(false)` there. + ## 🖥️ Ruby LSP Server The plugin configures and starts Ruby LSP for you, in any project with a `Gemfile` or `.git` directory. You don't need nvim-lspconfig or a `ruby_lsp` config of your own. If nvim-lspconfig is installed, its config merges with the plugin's without conflict. Before starting, the plugin checks that `ruby-lsp --version` runs from the project root. If you set `cmd` to a list such as `{ "bundle", "exec", "ruby-lsp" }`, it only checks that the first entry is executable. When the check fails, the server stays off and nothing is shown. Run `:checkhealth ruby-lsp` to see why. -Set `lsp.auto_start` to choose where the server starts. `true` starts it in every Ruby project, and `false` leaves it to you to call `vim.lsp.enable("ruby_lsp")`. A function decides per project. This one skips projects that use solargraph: +### Starting the Ruby LSP server yourself + +By default the plugin starts the Ruby LSP server in every Ruby project. To start the server yourself instead, set `lsp.auto_start = false` and call `vim.lsp.enable("ruby_lsp")` when you want it: + +```lua +require("ruby-lsp").setup({ + lsp = { + auto_start = false, + }, +}) + +vim.lsp.enable("ruby_lsp") +``` + +### Choosing which projects start the Ruby LSP server (optional) + +`lsp.auto_start` also takes a function. It receives the project root and returns whether to start the Ruby LSP server there. For example, if some of your projects bundle solargraph and you want solargraph to handle those, skip Ruby LSP when `Gemfile.lock` lists it: ```lua require("ruby-lsp").setup({ lsp = { auto_start = function(root_dir) - return vim.fn.filereadable(root_dir .. "/.solargraph.yml") == 0 + local lockfile = root_dir .. "/Gemfile.lock" + if vim.fn.filereadable(lockfile) == 0 then + return true + end + return not vim.iter(vim.fn.readfile(lockfile)):any(function(line) + return line:match("^%s+solargraph%f[^%w_-]") ~= nil + end) end, }, }) ``` -To customize the server, use `after/lsp/ruby_lsp.lua` or `vim.lsp.config("ruby_lsp", { ... })`. Your settings merge on top of the plugin's: +### Customizing the Ruby LSP server + +Use `after/lsp/ruby_lsp.lua` or `vim.lsp.config("ruby_lsp", { ... })`. Your settings merge on top of the plugin's: ```lua vim.lsp.config("ruby_lsp", { @@ -165,6 +199,9 @@ require("ruby-lsp").setup({ auto_start = true, }, + -- Show code lenses in buffers Ruby LSP is attached to + codelens = true, + -- Add :RubyLsp* user commands to buffers Ruby LSP is attached to user_commands = false, }) diff --git a/doc/ruby-lsp.nvim.txt b/doc/ruby-lsp.nvim.txt index f2fd62a..51e4e3d 100644 --- a/doc/ruby-lsp.nvim.txt +++ b/doc/ruby-lsp.nvim.txt @@ -1,5 +1,5 @@ *ruby-lsp.nvim.txt* - For NVIM v0.12.0 Last change: 2026 October 31 + For NVIM v0.12.0 Last change: 2026 November 01 ============================================================================== Table of Contents *ruby-lsp.nvim-table-of-contents* @@ -8,7 +8,8 @@ Table of Contents *ruby-lsp.nvim-table-of-contents* - 💡 Why |ruby-lsp.nvim-ruby-lsp.nvim-💡-why| - 📝 Commands |ruby-lsp.nvim-ruby-lsp.nvim-📝-commands| - 📋 Requirements |ruby-lsp.nvim-ruby-lsp.nvim-📋-requirements| - - 🛠️ Installation |ruby-lsp.nvim-ruby-lsp.nvim-���-️-installation| + - 🛠️ Installation |ruby-lsp.nvim-ruby-lsp.nvim-🛠️-installation| + - 🔍 Code Lenses |ruby-lsp.nvim-ruby-lsp.nvim-🔍-code-lenses| - 🖥️ Ruby LSP Server|ruby-lsp.nvim-ruby-lsp.nvim-🖥️-ruby-lsp-server| - 🧪 Neotest |ruby-lsp.nvim-ruby-lsp.nvim-🧪-neotest| - 🐞 Debugging |ruby-lsp.nvim-ruby-lsp.nvim-🐞-debugging| @@ -28,7 +29,7 @@ this plugin they actually do something: run through neotest with nvim-dap . -П�� WHY *ruby-lsp.nvim-ruby-lsp.nvim-💡-why* +💡 WHY *ruby-lsp.nvim-ruby-lsp.nvim-💡-why* Ruby LSP’s code lenses call client-side commands like `rubyLsp.runTest` and `rubyLsp.debugTest`. VS Code’s Ruby LSP extension implements them, but Neovim @@ -40,7 +41,7 @@ neotest adapter and an rdbg debug adapter that use Ruby LSP to find and run your tests. -П�� COMMANDS *ruby-lsp.nvim-ruby-lsp.nvim-📝-commands* +📝 COMMANDS *ruby-lsp.nvim-ruby-lsp.nvim-📝-commands* ----------------------------------------------------------------------- Command What it does @@ -81,7 +82,7 @@ attached to: the buffer in the quickfix list ----------------------------------------------------------------------- -П�� REQUIREMENTS *ruby-lsp.nvim-ruby-lsp.nvim-📋-requirements* +📋 REQUIREMENTS *ruby-lsp.nvim-ruby-lsp.nvim-📋-requirements* - Neovim >= 0.12 - Ruby LSP >= 0.23.0, installed with `gem install ruby-lsp`. With a Ruby version manager, install it once for each Ruby version you use. @@ -95,7 +96,7 @@ OPTIONAL ~ - ruby-lsp-rspec in your Gemfile for RSpec projects. Minitest works out of the box. -П��ϸ� INSTALLATION *ruby-lsp.nvim-ruby-lsp.nvim-���-️-installation* +🛠️ INSTALLATION *ruby-lsp.nvim-ruby-lsp.nvim-🛠️-installation* With lazy.nvim : @@ -118,7 +119,23 @@ If you’re upgrading and already start Ruby LSP yourself, remove your long as the plugin still isn’t lazy-loaded. -П��ϸ� RUBY LSP SERVER *ruby-lsp.nvim-ruby-lsp.nvim-🖥️-ruby-lsp-server* +🔍 CODE LENSES *ruby-lsp.nvim-ruby-lsp.nvim-🔍-code-lenses* + +The plugin turns on code lenses in buffers Ruby LSP is attached to, so **Run**, +**Run In Terminal**, and **Debug** show above your tests. Run the lens on the +cursor line with `grx`, Neovim’s default mapping for +`vim.lsp.codelens.run()`. To use a different key, map it yourself: + +>lua + vim.keymap.set("n", "cl", vim.lsp.codelens.run, { desc = "run code lens" }) +< + +Set `codelens = false` to leave code lenses to your own config. With it on, the +plugin enables code lenses for every server attached to a Ruby buffer, which +overrides a global `vim.lsp.codelens.enable(false)` there. + + +🖥️ RUBY LSP SERVER *ruby-lsp.nvim-ruby-lsp.nvim-🖥️-ruby-lsp-server* The plugin configures and starts Ruby LSP for you, in any project with a `Gemfile` or `.git` directory. You don’t need nvim-lspconfig or a `ruby_lsp` @@ -131,24 +148,52 @@ project root. If you set `cmd` to a list such as `{ "bundle", "exec", check fails, the server stays off and nothing is shown. Run `:checkhealth ruby-lsp` to see why. -Set `lsp.auto_start` to choose where the server starts. `true` starts it in -every Ruby project, and `false` leaves it to you to call -`vim.lsp.enable("ruby_lsp")`. A function decides per project. This one skips -projects that use solargraph: + +STARTING THE RUBY LSP SERVER YOURSELF ~ + +By default the plugin starts the Ruby LSP server in every Ruby project. To +start the server yourself instead, set `lsp.auto_start = false` and call +`vim.lsp.enable("ruby_lsp")` when you want it: + +>lua + require("ruby-lsp").setup({ + lsp = { + auto_start = false, + }, + }) + + vim.lsp.enable("ruby_lsp") +< + + +CHOOSING WHICH PROJECTS START THE RUBY LSP SERVER (OPTIONAL) ~ + +`lsp.auto_start` also takes a function. It receives the project root and +returns whether to start the Ruby LSP server there. For example, if some of +your projects bundle solargraph and you want solargraph to handle those, skip +Ruby LSP when `Gemfile.lock` lists it: >lua require("ruby-lsp").setup({ lsp = { auto_start = function(root_dir) - return vim.fn.filereadable(root_dir .. "/.solargraph.yml") == 0 + local lockfile = root_dir .. "/Gemfile.lock" + if vim.fn.filereadable(lockfile) == 0 then + return true + end + return not vim.iter(vim.fn.readfile(lockfile)):any(function(line) + return line:match("^%s+solargraph%f[^%w_-]") ~= nil + end) end, }, }) < -To customize the server, use `after/lsp/ruby_lsp.lua` or -`vim.lsp.config("ruby_lsp", { ... })`. Your settings merge on top of the -plugin’s: + +CUSTOMIZING THE RUBY LSP SERVER ~ + +Use `after/lsp/ruby_lsp.lua` or `vim.lsp.config("ruby_lsp", { ... })`. Your +settings merge on top of the plugin’s: >lua vim.lsp.config("ruby_lsp", { @@ -162,7 +207,7 @@ If you override `root_dir`, your function replaces the plugin’s start checks, including `lsp.auto_start` and the check that `ruby-lsp` runs. -П�� NEOTEST *ruby-lsp.nvim-ruby-lsp.nvim-🧪-neotest* +🧪 NEOTEST *ruby-lsp.nvim-ruby-lsp.nvim-🧪-neotest* The neotest adapter finds tests with Ruby LSP and runs the commands Ruby LSP resolves for them, so it works the same for Minitest and RSpec, in Rails apps @@ -181,7 +226,7 @@ results show up in neotest’s signs, summary, and output. **Run In Terminal** always uses a terminal. -П�� DEBUGGING *ruby-lsp.nvim-ruby-lsp.nvim-🐞-debugging* +🐞 DEBUGGING *ruby-lsp.nvim-ruby-lsp.nvim-🐞-debugging* When nvim-dap is installed, the plugin registers a `ruby` debug adapter that starts your tests under rdbg. The **Debug** code lens uses it, and the plugin @@ -205,7 +250,7 @@ adapter is already registered under the `dap.adapter` name, since they depend on its own adapter. -š�ϸ� OPTIONS *ruby-lsp.nvim-ruby-lsp.nvim-⚙️-options* +⚙️ OPTIONS *ruby-lsp.nvim-ruby-lsp.nvim-⚙️-options* These are the defaults. You only need to pass the ones you want to change. @@ -253,13 +298,16 @@ These are the defaults. You only need to pass the ones you want to change. auto_start = true, }, + -- Show code lenses in buffers Ruby LSP is attached to + codelens = true, + -- Add :RubyLsp* user commands to buffers Ruby LSP is attached to user_commands = false, }) < -П�� HEALTH CHECK *ruby-lsp.nvim-ruby-lsp.nvim-🩺-health-check* +🩺 HEALTH CHECK *ruby-lsp.nvim-ruby-lsp.nvim-🩺-health-check* > :checkhealth ruby-lsp @@ -270,7 +318,7 @@ It reports whether `ruby-lsp` runs from the current project, the the plugin can use. -П�� DEVELOPMENT *ruby-lsp.nvim-ruby-lsp.nvim-🔧-development* +🔧 DEVELOPMENT *ruby-lsp.nvim-ruby-lsp.nvim-🔧-development* One-time setup to enable the local git hooks: @@ -289,7 +337,7 @@ requires pandoc . < -П�� ACKNOWLEDGEMENTS *ruby-lsp.nvim-ruby-lsp.nvim-🙏-acknowledgements* +🙏 ACKNOWLEDGEMENTS *ruby-lsp.nvim-ruby-lsp.nvim-🙏-acknowledgements* Thank you to the Ruby LSP team for such a great tool. This isn’t an official Ruby LSP project, just my work to make diff --git a/lua/ruby-lsp/config.lua b/lua/ruby-lsp/config.lua index 6f5c846..ad30d69 100644 --- a/lua/ruby-lsp/config.lua +++ b/lua/ruby-lsp/config.lua @@ -31,6 +31,8 @@ local defaults = { auto_start = true, }, + codelens = true, + user_commands = false, } diff --git a/lua/ruby-lsp/health.lua b/lua/ruby-lsp/health.lua index 23a758a..b384872 100644 --- a/lua/ruby-lsp/health.lua +++ b/lua/ruby-lsp/health.lua @@ -72,6 +72,7 @@ local function check_install(root) else vim.health.info('auto_start: ' .. (auto_start and 'on' or 'off')) end + vim.health.info('codelens: ' .. (config.get().codelens and 'on' or 'off')) if bundles_ruby_lsp(root) then vim.health.warn('ruby-lsp is in ' .. root .. '/Gemfile.lock', { diff --git a/lua/ruby-lsp/init.lua b/lua/ruby-lsp/init.lua index a773102..e985fd1 100644 --- a/lua/ruby-lsp/init.lua +++ b/lua/ruby-lsp/init.lua @@ -40,7 +40,13 @@ function M.setup(opts) return end -- per buffer, so it runs before the per-client guard below - if config.get().user_commands then + local cfg = config.get() + -- scoped to the buffer only: with client_id, Neovim records the flag on the + -- client, so is_enabled({ bufnr }) is false and the lenses outlive the client + if cfg.codelens then + vim.lsp.codelens.enable(true, { bufnr = ev.buf }) + end + if cfg.user_commands then require('ruby-lsp.user_commands').attach(client, ev.buf) end if client._ruby_lsp_nvim_attached then diff --git a/tests/config_spec.lua b/tests/config_spec.lua index 50f7926..4b8bf93 100644 --- a/tests/config_spec.lua +++ b/tests/config_spec.lua @@ -53,6 +53,10 @@ describe('config', function() it('leaves user commands off by default', function() assert.is_false(config.get().user_commands) end) + + it('enables code lenses by default', function() + assert.is_true(config.get().codelens) + end) end) describe('setup', function() diff --git a/tests/health_spec.lua b/tests/health_spec.lua index 4e701d7..26d80d8 100644 --- a/tests/health_spec.lua +++ b/tests/health_spec.lua @@ -162,6 +162,16 @@ describe('health', function() assert.truthy(find('info', 'auto_start: function')) end) + it('reports the codelens setting', function() + health.check() + assert.truthy(find('info', 'codelens: on')) + + reports = {} + config.setup({ codelens = false }) + health.check() + assert.truthy(find('info', 'codelens: off')) + end) + it('places the checks after the Neovim version check', function() health.check() diff --git a/tests/init_spec.lua b/tests/init_spec.lua index ba46f88..82ddf13 100644 --- a/tests/init_spec.lua +++ b/tests/init_spec.lua @@ -69,63 +69,84 @@ describe('setup', function() assert.is_false(c.reuse_client(client, other)) end) - it('adds user commands on attach only when enabled', function() - local buf = vim.api.nvim_create_buf(true, false) - local original_get = vim.lsp.get_client_by_id - local client = { id = 7, name = 'ruby_lsp', handlers = {}, request = function() end } - local other = { id = 8, name = 'lua_ls', handlers = {}, request = function() end } - vim.lsp.get_client_by_id = function(id) - return id == 7 and client or other - end + describe('on attach', function() + local buf, clients, codelens_calls, originals local function attach(id) - vim.api.nvim_exec_autocmds('LspAttach', { - group = 'RubyLsp', - buffer = buf, - data = { client_id = id }, - }) - end - - ruby_lsp.setup({ dap = { auto_configure = false } }) - attach(7) - assert.is_nil(vim.api.nvim_buf_get_commands(buf, {}).RubyLspShowDependencies) - - ruby_lsp.setup({ dap = { auto_configure = false }, user_commands = true }) - attach(8) - assert.is_nil(vim.api.nvim_buf_get_commands(buf, {}).RubyLspShowDependencies) - attach(7) - assert.is_not_nil(vim.api.nvim_buf_get_commands(buf, {}).RubyLspShowDependencies) - - vim.lsp.get_client_by_id = original_get - vim.api.nvim_buf_delete(buf, { force = true }) - end) - - it('drops null values from progress notifications on attach', function() - local buf = vim.api.nvim_create_buf(true, false) - local original_get = vim.lsp.get_client_by_id - local original_progress = vim.lsp.handlers['$/progress'] - local client = { id = 9, name = 'ruby_lsp', handlers = {}, request = function() end } - vim.lsp.get_client_by_id = function() - return client - end - local received - vim.lsp.handlers['$/progress'] = function(_, result) - received = result + vim.api.nvim_exec_autocmds('LspAttach', { group = 'RubyLsp', buffer = buf, data = { client_id = id } }) end - ruby_lsp.setup({ dap = { auto_configure = false } }) - vim.api.nvim_exec_autocmds('LspAttach', { group = 'RubyLsp', buffer = buf, data = { client_id = 9 } }) - client.handlers['$/progress']( - nil, - { token = 't', value = { kind = 'report', message = vim.NIL, percentage = vim.NIL } }, - {}, - {} - ) - - assert.same({ token = 't', value = { kind = 'report' } }, received) - - vim.lsp.handlers['$/progress'] = original_progress - vim.lsp.get_client_by_id = original_get - vim.api.nvim_buf_delete(buf, { force = true }) + before_each(function() + buf = vim.api.nvim_create_buf(true, false) + clients = { + [1] = { id = 1, name = 'ruby_lsp', handlers = {}, request = function() end }, + [2] = { id = 2, name = 'lua_ls', handlers = {}, request = function() end }, + } + codelens_calls = {} + originals = { + get_client_by_id = vim.lsp.get_client_by_id, + codelens_enable = vim.lsp.codelens.enable, + progress = vim.lsp.handlers['$/progress'], + } + vim.lsp.get_client_by_id = function(id) + return clients[id] + end + vim.lsp.codelens.enable = function(enable, filter) + table.insert(codelens_calls, { enable, filter }) + end + end) + + after_each(function() + vim.lsp.get_client_by_id = originals.get_client_by_id + vim.lsp.codelens.enable = originals.codelens_enable + vim.lsp.handlers['$/progress'] = originals.progress + vim.api.nvim_buf_delete(buf, { force = true }) + end) + + it('adds user commands only when enabled', function() + ruby_lsp.setup({ dap = { auto_configure = false } }) + attach(1) + assert.is_nil(vim.api.nvim_buf_get_commands(buf, {}).RubyLspShowDependencies) + + ruby_lsp.setup({ dap = { auto_configure = false }, user_commands = true }) + attach(2) + assert.is_nil(vim.api.nvim_buf_get_commands(buf, {}).RubyLspShowDependencies) + attach(1) + assert.is_not_nil(vim.api.nvim_buf_get_commands(buf, {}).RubyLspShowDependencies) + end) + + it('drops null values from progress notifications', function() + local received + vim.lsp.handlers['$/progress'] = function(_, result) + received = result + end + + ruby_lsp.setup({ dap = { auto_configure = false } }) + attach(1) + clients[1].handlers['$/progress']( + nil, + { token = 't', value = { kind = 'report', message = vim.NIL, percentage = vim.NIL } }, + {}, + {} + ) + + assert.same({ token = 't', value = { kind = 'report' } }, received) + end) + + it('enables code lenses in buffers the ruby_lsp client attaches to', function() + -- the real API, so the test checks what a user can observe + vim.lsp.codelens.enable = originals.codelens_enable + ruby_lsp.setup({ dap = { auto_configure = false } }) + attach(2) + assert.is_false(vim.lsp.codelens.is_enabled({ bufnr = buf })) + attach(1) + assert.is_true(vim.lsp.codelens.is_enabled({ bufnr = buf })) + end) + + it('leaves code lenses alone when turned off', function() + ruby_lsp.setup({ dap = { auto_configure = false }, codelens = false }) + attach(1) + assert.same({}, codelens_calls) + end) end) end)