我一直在寻找一种类似于CoC的coc-fix-current的解决方案,但在Neovim 0.5中使用原生lsp,但我在documentation中没有找到这样的东西,有没有其他方法可以实现这一点?
发布于 2021-10-27 22:06:01
我有这个问题,通过修改telescope.nvim插件用来列出和运行代码操作的代码,我想出了这个怪物:
local function run_action(action)
if action.edit or type(action.command) == "table" then
if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit)
end
if type(action.command) == "table" then
vim.lsp.buf.execute_command(action.command)
end
else
vim.lsp.buf.execute_command(action)
end
end
local function do_action(action, client)
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(err, real)
if err then
return
end
if real then
run_action(real)
else
run_action(action)
end
end)
else
run_action(action)
end
end
return function()
local params = vim.lsp.util.make_range_params() -- get params for current position
params.context = {
diagnostics = vim.lsp.diagnostic.get_line_diagnostics(),
only = {"quickfix"}
}
local results, err = vim.lsp.buf_request_sync(
0, -- current buffer
"textDocument/codeAction", -- get code actions
params,
900
)
if err then return end
if not results or vim.tbl_isempty(results) then
print "No quickfixes!"
return
end
-- we have an action!
for cid, resp in pairs(results) do
if resp.result then
for _, result in pairs(resp.result) do
-- this is the first action, run it
do_action(result, vim.lsp.get_client_by_id(cid))
return
end
end
end
print "No quickfixes!"
end因为它是lua,所以需要将它放在nvim搜索模块的某个.lua文件中(例如,作为~/.config/nvim/lua/lsp_fixcurrent.lua),然后绑定到:lua require("lsp_fixcurrent")()
发布于 2021-08-06 06:52:18
也许你正在寻找:vim.lsp.buf.code_action()
https://stackoverflow.com/questions/67988374
复制相似问题