Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize Clang Format using Neovim

I am using none-ls to configure Clang-Format, version 19.1.0, as an LSP in Neovim, version v0.10.2. I want to use a .clangformat file to change the formatting rules, but I cannot figure out how to make Neovim use these new rules.

Right now the .clangformat file is is my user directory, which is a parent directory of my project, but when I run vim.lsp.buf.format, the rules are not applied. When run the executable from the terminal passing in the path to the .clangformat file using :!clang-format -i fileToBeFormatted --style=file: .clangformatPath it correctly applies the rules.

Is this an issue with how none-ls is configured or where the file is located.

My configuration for none-ls using Lazy:

return {
    "nvimtools/none-ls.nvim",
    config = function()
        local null_ls = require("null-ls")
        null_ls.setup({
            debug = true,
            sources = {
                null_ls.builtins.formatting.stylua,
                null_ls.builtins.diagnostics.clang_tidy,
                null_ls.builtins.formatting.clang_format.with({
            extra_args = { "--style=file" },
        }),
            },
        })
        vim.keymap.set("n", "<leader>fo", vim.lsp.buf.format, {})
    end,
}

Just want to add that I would like the keybind <leader>fo to remain language neutral, so I would appreciate if a solution other than hardcoding the .clangformat file location could be found.

like image 744
whatupo9 Avatar asked Oct 24 '25 16:10

whatupo9


1 Answers

Answer provided by @Alan Birtles in the comments is correct. The file name should be .clang-format with a - instead of .clangformat

like image 130
whatupo9 Avatar answered Oct 26 '25 06:10

whatupo9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!