fix(lsp): allow Lua pattern chars in code action filter (#24041)

Previously, filtering code actions with the "only" option failed
if the code action kind contained special Lua pattern chars such as "-"
(e.g. the ocaml language server supports a "type-annotate" code action).

Solution: use string comparison instead of string.find
This commit is contained in:
Jonas Strittmatter 2023-06-17 08:01:31 +02:00 committed by GitHub
parent 4e63104c47
commit c07dceba33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 20 deletions

View File

@ -608,9 +608,9 @@ local function on_code_action_results(results, ctx, options)
end
local found = false
for _, o in ipairs(options.context.only) do
-- action kinds are hierarchical 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
-- action kinds are hierarchical with . as a separator: when requesting only 'type-annotate'
-- this filter allows both 'type-annotate' and 'type-annotate.foo', for example
if a.kind == o or vim.startswith(a.kind, o .. '.') then
found = true
break
end

View File

@ -831,21 +831,21 @@ function tests.code_action_filter()
isPreferred = true,
command = 'preferred_command',
}
local quickfix_action = {
local type_annotate_action = {
title = 'Action 3',
kind = 'quickfix',
command = 'quickfix_command',
kind = 'type-annotate',
command = 'type_annotate_command',
}
local quickfix_foo_action = {
local type_annotate_foo_action = {
title = 'Action 4',
kind = 'quickfix.foo',
command = 'quickfix_foo_command',
kind = 'type-annotate.foo',
command = 'type_annotate_foo_command',
}
expect_request('textDocument/codeAction', function()
return nil, { action, preferred_action, quickfix_action, quickfix_foo_action, }
return nil, { action, preferred_action, type_annotate_action, type_annotate_foo_action, }
end)
expect_request('textDocument/codeAction', function()
return nil, { action, preferred_action, quickfix_action, quickfix_foo_action, }
return nil, { action, preferred_action, type_annotate_action, type_annotate_foo_action, }
end)
notify('shutdown')
end;

View File

@ -3357,22 +3357,22 @@ describe('LSP', function()
vim.lsp.commands['executed_preferred'] = function()
end
end
vim.lsp.commands['quickfix_command'] = function(cmd)
vim.lsp.commands['executed_quickfix'] = function()
vim.lsp.commands['type_annotate_command'] = function(cmd)
vim.lsp.commands['executed_type_annotate'] = function()
end
end
local bufnr = vim.api.nvim_get_current_buf()
vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID)
vim.lsp.buf.code_action({ filter = function(a) return a.isPreferred end, apply = true, })
vim.lsp.buf.code_action({
-- expect to be returned actions 'quickfix' and 'quickfix.foo'
context = { only = {'quickfix'}, },
-- expect to be returned actions 'type-annotate' and 'type-annotate.foo'
context = { only = { 'type-annotate' }, },
apply = true,
filter = function(a)
if a.kind == 'quickfix.foo' then
vim.lsp.commands['filtered_quickfix_foo'] = function() end
if a.kind == 'type-annotate.foo' then
vim.lsp.commands['filtered_type_annotate_foo'] = function() end
return false
elseif a.kind == 'quickfix' then
elseif a.kind == 'type-annotate' then
return true
else
assert(nil, 'unreachable')
@ -3382,8 +3382,8 @@ describe('LSP', function()
]])
elseif ctx.method == 'shutdown' then
eq('function', exec_lua[[return type(vim.lsp.commands['executed_preferred'])]])
eq('function', exec_lua[[return type(vim.lsp.commands['filtered_quickfix_foo'])]])
eq('function', exec_lua[[return type(vim.lsp.commands['executed_quickfix'])]])
eq('function', exec_lua[[return type(vim.lsp.commands['filtered_type_annotate_foo'])]])
eq('function', exec_lua[[return type(vim.lsp.commands['executed_type_annotate'])]])
client.stop()
end
end