fix: code action for rust analyzer (#1311)

This commit is contained in:
Simon Hauser
2021-10-07 13:49:14 +02:00
committed by GitHub
parent d1e9655d90
commit ec6c13fc09

View File

@@ -147,13 +147,18 @@ lsp.document_symbols = function(opts)
end end
lsp.code_actions = function(opts) lsp.code_actions = function(opts)
local params = opts.params or vim.lsp.util.make_range_params() local params = vim.F.if_nil(opts.params, vim.lsp.util.make_range_params())
params.context = { params.context = {
diagnostics = vim.lsp.diagnostic.get_line_diagnostics(), diagnostics = vim.lsp.diagnostic.get_line_diagnostics(),
} }
local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, opts.timeout or 10000) local results_lsp, err = vim.lsp.buf_request_sync(
0,
"textDocument/codeAction",
params,
vim.F.if_nil(opts.timeout, 10000)
)
if err then if err then
print("ERROR: " .. err) print("ERROR: " .. err)
@@ -181,6 +186,7 @@ lsp.code_actions = function(opts)
local entry = { local entry = {
idx = idx, idx = idx,
command_title = result.title:gsub("\r\n", "\\r\\n"):gsub("\n", "\\n"), command_title = result.title:gsub("\r\n", "\\r\\n"):gsub("\n", "\\n"),
client = client,
client_name = client and client.name or "", client_name = client and client.name or "",
command = result, command = result,
} }
@@ -211,9 +217,9 @@ lsp.code_actions = function(opts)
local function make_display(entry) local function make_display(entry)
return displayer { return displayer {
{ entry.idx .. ":", "TelescopePromptPrefix" }, { entry.value.idx .. ":", "TelescopePromptPrefix" },
{ entry.command_title }, { entry.value.command_title },
{ entry.client_name, "TelescopeResultsComment" }, { entry.value.client_name, "TelescopeResultsComment" },
} }
end end
@@ -283,14 +289,10 @@ lsp.code_actions = function(opts)
prompt_title = "LSP Code Actions", prompt_title = "LSP Code Actions",
finder = finders.new_table { finder = finders.new_table {
results = results, results = results,
entry_maker = function(line) entry_maker = function(action)
return { return {
valid = line ~= nil, value = action,
value = line.command, ordinal = action.idx .. action.command_title,
ordinal = line.idx .. line.command_title,
command_title = line.command_title,
idx = line.idx,
client_name = line.client_name,
display = make_display, display = make_display,
} }
end, end,
@@ -299,9 +301,24 @@ lsp.code_actions = function(opts)
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local action = selection.value local action = selection.value.command
local client = selection.value.client
execute_action(transform_action(action)) if
not action.edit
and client
and type(client.resolved_capabilities.code_action) == "table"
and client.resolved_capabilities.code_action.resolveProvider
then
client.request("codeAction/resolve", action, function(resolved_err, resolved_action)
if resolved_err then
vim.notify(resolved_err.code .. ": " .. resolved_err.message, vim.log.levels.ERROR)
return
end
execute_action(transform_action(resolved_action))
end)
else
execute_action(transform_action(action))
end
end) end)
return true return true