stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
3.02k stars 156 forks source link

bug: append_args not working with clang-format and mason #536

Closed Maypher closed 4 days ago

Maypher commented 1 week ago

Neovim version (nvim -v)

NVIM v0.10.1

Operating system/version

Windows 11

Read debugging tips

Add the debug logs

Log file

      2024-09-22 12:23:51[DEBUG] Running formatters on Path/to/main.c: { "clang-format" }
      2024-09-22 12:23:51[INFO] Run clang-format on Path/to/main.c
      2024-09-22 12:23:51[DEBUG] Run command: { "~\\AppData\\Local\\nvim-data\\mason\\bin\\clang-format.CMD", "-assume-filename", "Path/to/main.c" }
      2024-09-22 12:23:51[DEBUG] Run default CWD: Project\directory
      2024-09-22 12:23:51[DEBUG] clang-format exited with code 0

Describe the bug

I'm running clang-format which I installed through mason.nvim with lazy.nvim

I believe the default style it's using is Google but it uses two spaces for indentation and I prefer using 4 so I added an argument to change the indent width

return {
    "stevearc/conform.nvim",
    opts = {
        log_level = vim.log.levels.DEBUG,
        formatters_by_ft = {
            lua = { "stylua" },
            python = { "black" },
            rust = { "rustfmt" },
            javascript = { "prettier" },
            html = { "prettier" },
            css = { "prettier" },
            c = { "clang-format" },
            cpp = { "clang-format" },
            cmake = { "cmake_format" },
        },
        formatters = {
            clang_format = {
                command = "clang-format",
                append_args = { "--style={BasedOnStyle: Google, IndentWidth: 4}" },
            },
        },
        format_on_save = {
            -- These options will be passed to conform.format()
            timeout_ms = 500,
            lsp_format = "fallback",
        },
    },
}

In formatters I edit the clang_format config to append the arguments to it. However, as can be seen in the log file these arguments are nowhere to be found so the file gets formatted with 2 indent spaces. I'm not sure if it's an issue with conform or if it's because the formatter is installed through Mason

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. Install mason.nvim and conform.nvim through lazy.nvim
  2. Install clang-format through mason.
  3. Change the formatter options for clang-format to include the following arguments: --style={BasedOnStyle: Google, IndentWidth: 4}.
  4. Run the formatter.

Expected Behavior

Line indentation should have 4 spaces, instead it has 2.

Minimal example file

int main(int argc, char *argv[]) {
  return 0;
}

Minimal init.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/conform.nvim",
    config = function()
      require("conform").setup({
        log_level = vim.log.levels.DEBUG,
        formatters_by_ft = {
            lua = { "stylua" },
            python = { "black" },
            rust = { "rustfmt" },
            javascript = { "prettier" },
            html = { "prettier" },
            css = { "prettier" },
            c = { "clang-format" },
            cpp = { "clang-format" },
            cmake = { "cmake_format" },
        },
        formatters = {
            clang_format = {
                command = "clang-format",
                append_args = { "--style={BasedOnStyle: Google, IndentWidth: 4}" },
            },
        },
        format_on_save = {
            -- These options will be passed to conform.format()
            timeout_ms = 500,
            lsp_format = "fallback",
        },
      })
    end,
  },
  {
    "williamboman/mason.nvim",
    config = true
  }
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Additional context

No response

domi413 commented 5 days ago

Not the solution you're looking for but you can add a .clang-format to your home dir with the following content

BasedOnStyle: google
TabWidth: 4
IndentWidth: 4
ColumnLimit: 100

# Other languages JavaScript, Proto
Language: Cpp
Maypher commented 4 days ago

No need for that, I figured it out. Turns out that conform looks for an exact match when setting formatters_by_ft and formatters so when setting the file-type to clang-format and the formatter option to clang_format (since I can't use - in dict keys) it took them as two different things.

I solved it by setting the formatter_by_ft for c and cpp to clang_format and the on formatter options I give it a custom command with the correct options.

    opts = {
        log_level = vim.log.levels.DEBUG,
        formatters_by_ft = {
            c = { "clang_format" },
            cpp = { "clang_format" },
        },
        formatters = {
            clang_format = {
                command = "clang-format",
                append_args = function()
                    return { "--style={BasedOnStyle: Google, IndentWidth: 4}" }
                end
            }