kosayoda / nvim-lightbulb

VSCode 💡 for neovim's built-in LSP.
MIT License
801 stars 30 forks source link

FR: Ignore code action #51

Closed chrisgrieser closed 1 week ago

chrisgrieser commented 1 year ago

Would it maybe be possible to ignore certain code actions? Like, some LSP servers have a bunch of useful code actions, but also a bunch of useless code actions, and the lightbulb appears for both.

kosayoda commented 1 year ago

Hello! Does the action_kinds option do what you want? Pasting the relevant docs here:

    -- Code action kinds to observe.
    -- To match all code actions, set to `nil`.
    -- Otherwise, set to a table of kinds.
    -- Example: { "quickfix", "refactor.rewrite" }
    -- See: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeActionKind
    action_kinds = nil,
chrisgrieser commented 1 year ago

Ah yes, that looks indeed looks je it could be it.

Do you know how I can tell which type a particular code action has?

kosayoda commented 1 year ago

Hello again. I just added a NvimLightbulb.debug() function, can you let me know if this helps? Among other debug information it should show code actions at the location along with their type.

Example output:

image
chrisgrieser commented 1 year ago

hi, sorry for not responding. So yeah, it kinda goes in that direction. However, it would need to be quite fine-grained, since for example in one language, I Ignore refactors, in another language, I ignore quickfixes, etc.

Some sort of more general filter might be useful? Like I have this function which filters out code actions from vim.lsp.buf.code_action I never use, and it would be great if the lightbulb could use such a filter function as well?

---@param action object CodeAction Obj https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeAction
---@return boolean
local function codeActionFilter(action)
    local title, kind, ft = action.title, action.kind, vim.bo.ft

    -- in lua, ignore all quickfixes except line disables and all rewrites
    local ignoreInLua = ft == "lua"
        and not (title:find("on this line"))
        and (kind == "quickfix" or kind == "refactor.rewrite")

    -- in python, ignore ruff actions except for line disables
    local ignoreInPython = ft == "python"
        and title:find("^Ruff")
        and not (title:find("Disable for this line$"))
    return not (ignoreInLua or ignoreInPython)
end

vim.keymap.set(
    { "n", "x" },
    "<leader>c",
    function() vim.lsp.buf.code_action { filter = codeActionFilter } end,
    { desc = "ó°’• Code Action" }
)
wookayin commented 10 months ago

As said in https://github.com/kosayoda/nvim-lightbulb/issues/58#issuecomment-1870939846, allowing users to have a custom predicate or filter function fun(lsp.CodeAction|lsp.Command): boolean would be a better and general enough solution.

kosayoda commented 1 week ago

Hello! The filter feature is now implemented in 18c54dd.

Thank you very much for your patience.