feat(lsp): options to filter and auto-apply code actions (#18221)

Implement two new options to vim.lsp.buf.code_action():
 - filter (function): predicate taking an Action as input, and returning
   a boolean.
 - apply (boolean): when set to true, and there is just one remaining
   action (after filtering), the action is applied without user query.

These options can, for example, be used to filter out, and automatically
apply, the action indicated by the server to be preferred:

    vim.lsp.buf.code_action({
        filter = function(action)
            return action.isPreferred
        end,
        apply = true,
    })

Fix #17514.
This commit is contained in:
Fredrik Ekre
2022-04-30 10:14:31 +02:00
committed by GitHub
parent de2232878f
commit df09e03cf7
5 changed files with 125 additions and 24 deletions

View File

@@ -969,18 +969,28 @@ add_workspace_folder({workspace_folder})
clear_references() *vim.lsp.buf.clear_references()*
Removes document highlights from current buffer.
code_action({context}) *vim.lsp.buf.code_action()*
code_action({options}) *vim.lsp.buf.code_action()*
Selects a code action available at the current cursor
position.
Parameters: ~
{context} table|nil `CodeActionContext` of the LSP specification:
• diagnostics: (table|nil) LSP`Diagnostic[]` . Inferred from the current position if not
provided.
• only: (string|nil) LSP `CodeActionKind` used
to filter the code actions. Most language
servers support values like `refactor` or
`quickfix`.
{options} table|nil Optional table which holds the
following optional fields:
• context (table|nil): Corresponds to `CodeActionContext` of the LSP specification:
• diagnostics (table|nil): LSP`Diagnostic[]` . Inferred from the current position if not
provided.
• only (string|nil): LSP `CodeActionKind`
used to filter the code actions. Most
language servers support values like
`refactor` or `quickfix`.
• filter (function|nil): Predicate function
taking an `CodeAction` and returning a
boolean.
• apply (boolean|nil): When set to `true`, and
there is just one remaining action (after
filtering), the action is applied without
user query.
See also: ~
https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction