p00f / clangd_extensions.nvim

Clangd's off-spec features for neovim's LSP client. Use https://sr.ht/~p00f/clangd_extensions.nvim instead
MIT License
485 stars 18 forks source link

on attach doesn't get called #12

Closed max397574 closed 2 years ago

max397574 commented 2 years ago

I have a custom on_attach function I use it like this:

local clangd_defaults = require("lspconfig.server_configurations.clangd")
local clangd_configs = vim.tbl_deep_extend(
    "force",
    clangd_defaults["default_config"],
    {
        on_attach = on_attach,
        cmd = {
            "clangd",
            "-j=4",
            "--background-index",
            "--clang-tidy",
            "--fallback-style=llvm",
            "--all-scopes-completion",
            "--completion-style=detailed",
            "--header-insertion=iwyu",
            "--header-insertion-decorators",
            "--pch-storage=memory",
        },
    }
)
require("clangd_extensions").setup({
    server = {
        clangd_configs,
    },
})

but the on_attach function never gets called

EDIT: after debugging this a little bit I found the problem for me config.options.server is a table inside a table. But it should only be a table I don't know if this is an error in the code or just a problem with my config.

max397574 commented 2 years ago

was a problem with my config

TMTBO commented 2 years ago

I got the same problem, the config.options.server.on_attach never gets called. And thed cmd field load correctly. Any suggestions?

This is my config:

function M.setup(opts)
    opts.cmd = {
        "clangd",
        "--background-index",
        "--clang-tidy",
        "--fallback-style=llvm",
        "--all-scopes-completion",
        "--completion-style=detailed",
        "--header-insertion=iwyu",
        "--header-insertion-decorators",
        "--pch-storage=memory",
        "--inlay-hints",
    }
    require('clangd_extensions').setup({
        server = opts,
    })
end
p00f commented 2 years ago

are you calling M.setup with an actual table

TMTBO commented 2 years ago

are you calling M.setup with an actual table

Yes, I did that. The clangd_extensions.setup function got be called, but the new on_attach function does not be called.

p00f commented 2 years ago

what is inside opts? what happens when you add print(vim.inspect(opts)) just before require('clangd_extensions').setup({...})? Is the output the same as what you expected?

TMTBO commented 2 years ago

what is inside opts? what happens when you add print(vim.inspect(opts)) just before require('clangd_extensions').setup({...})? Is the output the same as what you expected?

The opts does not have any problem. Finlly, I did it with the next config:

-- clangd
--

local cmds = {
    "clangd",
    "--background-index",
    "--clang-tidy",
    "--fallback-style=llvm",
    "--all-scopes-completion",
    "--completion-style=detailed",
    "--header-insertion=iwyu",
    "--header-insertion-decorators",
    "--pch-storage=memory",
    "--inlay-hints",
}

local function on_attach(client, bufnr)
    require('../plugins/lspconfig').on_attach(client, bufnr)
    require('clangd_extensions').setup({
        server = {
            cmd = cmds
        }
    })
end

local config = {
    on_attach = on_attach,
    cmd = cmds,
}

return {
    config = function(_) return config end,
}

Firstly, I called require('lspconfig').clangd.setup(opts). Then, I got on_attach called above. Finlly, I setup clangd_extensions.

That works!!! But I setup clangd_extensions directly still NOT work. It's strange. And I have no idea about that.