stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.72k stars 142 forks source link

bug: formaters for C not enabled in header files #382

Closed chalbin73 closed 3 months ago

chalbin73 commented 3 months ago

Neovim version (nvim -v)

NVIM v0.9.5

Operating system/version

Linux 6.8.4-zen

Add the debug logs

Log file

23:02:20[DEBUG] Running formatters on [...]/file.h: {}
23:02:20[DEBUG] No formatters found for [...]/file.h

Describe the bug

The 'uncrustify' formatter requested for C, is not run on the header files (.h), even though it works perfectly on the .c files.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. Setup conform with :

    
    conform.setup({
    formatters_by_ft = {
        c = { "uncrustify" },
        h = { "uncrustify" },
    
        lua = { "stylua" },
        tex = { "latexindent" },
    },
    })
2. Edit a C file with nvim (.c), do ":ConfomInfo" : uncrutify ready listed in "formatters for this buffer"
3. Edit a C header file with nvim (.h), do ":ConfomInfo" : uncrutify ready **not** listed in "formatters for this buffer"

### Expected Behavior

Uncrustify, being a C formatter should be enabled in both h files and c files.

### Minimal example file

_No response_

### Minimal init.lua

```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 = {
        c = { "uncrustify" },
        h = { "uncrustify" },
    },
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

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

Additional context

No response

chalbin73 commented 3 months ago

Ok so i manually checked the filetype with :lua vim.bo[<bufnr>].filetype, and according to vim, an h file is of the 'cpp' file type. So enabling the formatter for this filetype worked. So now it works. Maybe this could be something to add to the documentation somewhere ? Because I thought formatters_by_ft would match with the actual filename itself, I did not know that vim had an internal filetype.

chalbin73 commented 3 months ago

I just saw that there was an option in Neovim to detect .h header files as C files, just set :

vim.g.c_syntax_for_h = 1

somewhere in your config and the file type of .h files will be C.

stevearc commented 3 months ago

I don't want to replicate the entirety of the vim help docs in this project, so I have to draw the line somewhere. For that reason, I do assume some familiarity with basic vim concepts such as the filetype. I am happy to answer these questions when they come in, though. Glad you figured it out!