Switch to .h/cpp via lsp

searching around i saw this

    require'lspconfig'.clangd.setup {
        commands = {
            ClangdSwitchSourceHeader = {
                function()
                    local bufnr = require'lspconfig'.util.validate_bufnr(0)
                    local params = { uri = vim.uri_from_bufnr(bufnr) }
                    vim.lsp.buf_request(bufnr, 'textDocument/switchSourceHeader', params, function(err, _, result)
                        if err then error(tostring(err)) end
                        if not result then print ("Corresponding file can’t be determined") return end
                        vim.api.nvim_command("edit "..vim.uri_to_fname(result))
                        vim.api.nvim_command("bdelete "..tostring(bufnr))
                    end)
                end
            },
        }
    }

only issue is the final bdelete i added to cleanup the old file i swapped from doesn’t always work. When it does work it will complain that it didnt delete anything.


is the inconsistent behavior related to async in some way?
i can repro the inconsistent behavior using the exact original with the extra bdelete command

issue was i had two calls to the lsp setup.