DavidRambo / nvim

Neovim Config
MIT License
12 stars 0 forks source link

Clang-format config #1

Closed szcharlesji closed 3 months ago

szcharlesji commented 3 months ago

Came across your config file to resolve my own clang-format issue and the solution is replacing clang_format with ["clang-format"] in your conform config

DavidRambo commented 3 months ago

Thank you! That's really good to know. I just searched the issues and saw this mentioned as a way to index into the formatters table.

So does clang-format work as you want it to through Conform?

I don't know where I read to use extra_args and "-style=", but I've switched it over to prepend_args and "--style":

    formatters = {
      ["clang-format"] = {
       prepend_args = {
          "--style",
          "{IndentCaseLabels: true, IndentWidth: 2, AllowShortFunctionsOnASingleLine: None}",
        },
      },

It's still not working for me, but using a .clang-format file works well enough.

szcharlesji commented 3 months ago

my setting is here and it works for me:

                ["clang-format"] = {
                    prepend_args = { "--style", "{IndentWidth: 4}" },
                },
szcharlesji commented 3 months ago

Your setup is working for me. It can be other part of your conform setup that is not working for you. Try using :checkhealth to see if there is any issues. Here is the conform setup with Lazy that is working for me:

return {
    "stevearc/conform.nvim",
    event = { "BufReadPre", "BufNewFile" },
    config = function()

        local conform = require("conform")
        conform.setup({
            formatters_by_ft = {
                c = { "clang-format" },
                cpp = { "clang-format" },
                javascript = { "prettier" },
                typescript = { "prettier" },
                javascriptreact = { "prettier" },
                typescriptreact = { "prettier" },
                svelte = { "prettier" },
                css = { "prettier" },
                html = { "prettier" },
                json = { "prettier" },
                yaml = { "prettier" },
                markdown = { "prettier" },
                graphql = { "prettier" },
                liquid = { "prettier" },
                lua = { "stylua" },
                python = { "isort", "black" },
            },

            format_on_save = {
                lsp_fallback = true,
                async = false,
                timeout_ms = 1000,
            },

            -- Configure formatters
            formatters = {
                prettier = {
                    prepend_args = { "--tab-width", "4", "--use-tabs", "false" },
                },
                ["clang-format"] = {
                    prepend_args = {
                        "--style",
                        "{IndentCaseLabels: true, IndentWidth: 4, AllowShortFunctionsOnASingleLine: None}",
                    },
                },
                stylua = {
                    prepend_args = { "--indent-type", "Spaces", "--indent-width", "4" },
                },
                black = {
                    prepend_args = { "--line-length", "88", "--skip-string-normalization" },
                },
                isort = {
                    prepend_args = { "--profile", "black" },
                },
            },
        })

        vim.keymap.set({ "n", "v" }, "<leader>mp", function()
            conform.format({
                lsp_fallback = true,
                async = false,
                timeout_ms = 1000,
            })
        end, { desc = "Format file or range (in visual mode)" })
    end,
}
DavidRambo commented 3 months ago

Oh! I had changed "clang-format" to "clang_format" in the formatters_by_ft.c. All's good now. Thanks for chiming in, I appreciate it. And I hope my config was useful : )