8

I am using nvim with lazy and using mason, mason-lspconfig, nvim-lspconfig as follow

return {
        {
                "williamboman/mason.nvim",
                config = function()
                        require("mason").setup()
                end,
        },
        {
                "williamboman/mason-lspconfig.nvim",
                config = function()
                        require("mason-lspconfig").setup({
                                ensure_installed = { "lua_ls" },
                        })
                end,
        },
        {
                "neovim/nvim-lspconfig",
                lazy = false,
                config = function()
                        local lspconfig = require("lspconfig")
                        local capabilities = require("cmp_nvim_lsp").default_capabilities()
                        lspconfig.lua_ls.setup({
                                capabilities = capabilities,
                        })
                        lspconfig.clangd.setup({
                                capabilities = capabilities,
                        })

                        vim.keymap.set({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, {})
                        vim.keymap.set("n", "K", vim.lsp.buf.hover, {})
                        vim.keymap.set("n", "<leader>e", function()
                                vim.diagnostic.open_float(0, { scope = "line" })
                        end, {})
                end,
        },
}

The above setting works fine for c++ except that I cannot format the code using clangd so I followed some guide and set clang-format as the formatter using none-ls as follow:

return {
        "nvimtools/none-ls.nvim",
        config = function()
                local null_ls = require("null-ls")
                null_ls.setup({
                        sources = {
                                null_ls.builtins.formatting.stylua,
                                null_ls.builtins.formatting.black,
                                null_ls.builtins.formatting.isort,
                                null_ls.builtins.formatting.clang_format,
                        },
                })
                vim.keymap.set("n", "<leader>F", vim.lsp.buf.format, {})
        end,
}

However I believed that clang-format is formatting differently than clangd. I feel that using clangd format in vscode keep the code easier to read and most of the legacy project is formatted using that way so when applying clang-format, it creates a bunch of unnecessary change.

I want to set up clangd as formatter similar to vscode setting to avoid unnecessary changes Thanks

1 Answer 1

1

In my Configuration i depend on lsp-zero to easily perform format on save using clangd for C/C++ and rust-analyzer for rust

config = function()
    local lsp_zero = require("lsp-zero")
    lsp_zero.format_on_save({
        format_opts = {
            async = true,
            timeout_ms = 10000,
        },
        servers = {
            ['rust-analyzer'] = { 'rust' }
            ['clangd'] = { 'c', 'cpp' },
        }
    })
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.