首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Neovim lsp自动修复/修复当前?

Neovim lsp自动修复/修复当前?
EN

Stack Overflow用户
提问于 2021-06-15 22:37:14
回答 2查看 249关注 0票数 2

我一直在寻找一种类似于CoC的coc-fix-current的解决方案,但在Neovim 0.5中使用原生lsp,但我在documentation中没有找到这样的东西,有没有其他方法可以实现这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-27 22:06:01

我有这个问题,通过修改telescope.nvim插件用来列出和运行代码操作的代码,我想出了这个怪物:

代码语言:javascript
复制
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")()

票数 2
EN

Stack Overflow用户

发布于 2021-08-06 06:52:18

也许你正在寻找:vim.lsp.buf.code_action()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67988374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档