diff options
author | Fredrik Ekre <ekrefredrik@gmail.com> | 2022-05-12 18:48:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-12 18:48:02 +0200 |
commit | a9d25e94725d5cfc41c2fabff22b2284e109fa0c (patch) | |
tree | 1f7382012d1ecded52011e600c651c48e406bd77 /runtime/lua/vim/lsp/buf.lua | |
parent | de5ccf2348a1fabf3867da9256e4740a7dfcf004 (diff) | |
download | rneovim-a9d25e94725d5cfc41c2fabff22b2284e109fa0c.tar.gz rneovim-a9d25e94725d5cfc41c2fabff22b2284e109fa0c.tar.bz2 rneovim-a9d25e94725d5cfc41c2fabff22b2284e109fa0c.zip |
fix(lsp): perform client side filtering of code actions (#18392)
Implement filtering of actions based on the kind when passing the 'only'
parameter to code_action(). Action kinds are hierachical with a '.' as
the separator, and the filter thus allows, for example, both 'quickfix'
and 'quickfix.foo' when requestiong only 'quickfix'.
Fix https://github.com/neovim/neovim/pull/18221#issuecomment-1110179121
Diffstat (limited to 'runtime/lua/vim/lsp/buf.lua')
-rw-r--r-- | runtime/lua/vim/lsp/buf.lua | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 1cfbcd1259..b0bf2c6e5b 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -691,10 +691,38 @@ end --- `codeAction/resolve` local function on_code_action_results(results, ctx, options) local action_tuples = {} - local filter = options and options.filter + + ---@private + local function action_filter(a) + -- filter by specified action kind + if options and options.context and options.context.only then + if not a.kind then + return false + end + local found = false + for _, o in ipairs(options.context.only) do + -- action kinds are hierachical with . as a separator: when requesting only + -- 'quickfix' this filter allows both 'quickfix' and 'quickfix.foo', for example + if a.kind:find('^' .. o .. '$') or a.kind:find('^' .. o .. '%.') then + found = true + break + end + end + if not found then + return false + end + end + -- filter by user function + if options and options.filter and not options.filter(a) then + return false + end + -- no filter removed this action + return true + end + for client_id, result in pairs(results) do for _, action in pairs(result.result or {}) do - if not filter or filter(action) then + if action_filter(action) then table.insert(action_tuples, { client_id, action }) end end |