LunarVim / Neovim-from-scratch

📚 A Neovim config designed from scratch to be understandable
https://www.chrisatmachine.com/
GNU General Public License v3.0
5.32k stars 1.17k forks source link

Trying to configure clangd as a language server, running into issues #48

Closed embeddedpenguin closed 2 years ago

embeddedpenguin commented 2 years ago

I'm basically trying to add onto what you've done here in ~/.config/nvim/lua/user/lsp/lsp-installer.lua

local status_ok, lsp_installer = pcall(require, "nvim-lsp-installer")
if not status_ok then
    return
end

-- Register a handler that will be called for all installed servers.
-- Alternatively, you may also register handlers on specific server instances instead (see example below).
lsp_installer.on_server_ready(function(server)
    local opts = {
        on_attach = require("user.lsp.handlers").on_attach,
        capabilities = require("user.lsp.handlers").capabilities,
    }

     if server.name == "jsonls" then
        local jsonls_opts = require("user.lsp.settings.jsonls")
        opts = vim.tbl_deep_extend("force", jsonls_opts, opts)
     end

     if server.name == "sumneko_lua" then
        local sumneko_opts = require("user.lsp.settings.sumneko_lua")
        opts = vim.tbl_deep_extend("force", sumneko_opts, opts)
     end

     if server.name == "pyright" then
        local pyright_opts = require("user.lsp.settings.pyright")
        opts = vim.tbl_deep_extend("force", pyright_opts, opts)
     end

     if server.name == "clangd" then
         local clangd_opts = require("user.lsp.settings.clangd")
         opts = vim.tbl_deep_extend("force", clangd_opts, opts)
     end

    -- This setup() function is exactly the same as lspconfig's setup function.
    -- Refer to https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
    server:setup(opts)
end)

I've added the clangd portion. Then, I've added ~/.config/nvim/lua/user/lsp/settings/clangd.lua :

return {
        clangd = {
            cmd = "clangd --query-driver=/usr/bin/arm-unknown-eabi-gcc"
        }
}

This is probably just my poor understanding of lua, but in my brain this should work. It doesn't though. When opening a C file and using :LspInfo, I can see that the cmd is still just clangd:

 Language client log: /home/penguin/.cache/nvim/lsp.log
 Detected filetype:   c

 1 client(s) attached to this buffer: 

 Client: clangd (id: 1, pid: 26703, bufnr: [1])
    filetypes:       c, cpp, objc, objcpp
    autostart:       true
    root directory:  /tmp/projecttest
    cmd:             clangd

 Configured servers list: clangd

I also can't find any documentation for what settings these "opts" structs actually take for clangd, so I have no idea if it's actually called "cmd" or "command" or what. I'm assuming it's called cmd because LspInfo lists it as cmd.

embeddedpenguin commented 2 years ago

Fixed this by changing ~/.config/nvim/lua/user/lsp/settings/clangd.lua to

return {
    cmd = "clangd --query-driver=/usr/bin/arm-unknown-eabi-gcc"
}

Right now it does feel like a lot of guessing though. That obviously isn't really anyones fault, though. It would be awesome if you could go through and show how to configure these on a per project basis. Like maybe clangd looks in the project root and finds a .clangd file that has some arguments we can pass here so I don't have to change this file every time I need to work with a different toolchain. Thanks a bunch for this series!