Skip to content

[idea] LSP code lens toggle — expose run/debug annotations from rust_analyzer, gopls, clangd #291

Description

@stanfish06

What

Add keymaps to refresh and run LSP code lens for the current buffer. No additional plugins needed — Neovim 0.11+ ships vim.lsp.codelens.* in the stable API.

Where

lua/config/plugin_config.lua — inside the LspAttach callback at line 137, alongside the existing gd and <leader>la keymaps.

Why it matters

Three of the configured LSP servers produce code lens:

Server Code lens output
rust_analyzer "Run ▸", "Debug ▸", inline reference counts
gopls Run/debug test at cursor, reference counts
clangd Reference counts

Without calling vim.lsp.codelens.refresh(), these annotations are never requested and never displayed. With them, "Run ▸" for a #[test] or a Go Test* function becomes a one-key action — a natural complement to the existing build workflow (:Compile, makeprg).

Recommended implementation

Inside the existing LspAttach callback:

vim.api.nvim_create_autocmd("LspAttach", {
    callback = function(ev)
        local client = vim.lsp.get_client_by_id(ev.data.client_id)
        -- ... existing gd / <leader>la keymaps ...

        if client and client:supports_method("textDocument/codeLens") then
            vim.lsp.codelens.refresh()
            vim.api.nvim_create_autocmd({ "BufEnter", "InsertLeave", "BufWritePost" }, {
                buffer = ev.buf,
                callback = vim.lsp.codelens.refresh,
            })
            vim.keymap.set("n", "<leader>ll", vim.lsp.codelens.refresh,
                { buffer = ev.buf, desc = "LSP code lens refresh" })
            vim.keymap.set("n", "<leader>lc", vim.lsp.codelens.run,
                { buffer = ev.buf, desc = "LSP code lens run" })
        end
    end,
})

Note on keymap choice: <leader>lr is already taken by fzf.lsp_references (plugin_config.lua:58). The suggestion above uses <leader>lc for "code lens run" and <leader>ll for "code lens refresh". Adjust to taste — <leader>lL is another option.

Caveats

  • Code lens display uses virtual text by default; tiny-inline-diagnostic.nvim is already running. If the two virt-text layers conflict visually, disable code lens display and use only the run keymap.
  • vim.lsp.codelens.run() opens vim.ui.select to pick a lens if multiple are at the cursor — this goes through fzf.register_ui_select() already in place.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions