Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Incorrect formatter picked in cases of nested filetypes #630

Open
2 tasks done
mehalter opened this issue Jan 23, 2025 · 1 comment · May be fixed by #631
Open
2 tasks done

bug: Incorrect formatter picked in cases of nested filetypes #630

mehalter opened this issue Jan 23, 2025 · 1 comment · May be fixed by #631
Labels
bug Something isn't working

Comments

@mehalter
Copy link
Contributor

Neovim version (nvim -v)

NVIM v0.10.3

Operating system/version

Arch Linux

Read debugging tips

Add the debug logs

  • I have set log_level = vim.log.levels.DEBUG and pasted the log contents below.

Log file

With the file test.hcl opened:

Log file: /home/[email protected]/Documents/nvim_sandbox/.repro//state/nvim/conform.log

Formatters for this buffer:
hcl unavailable: Command not found

Other formatters:
packer_fmt ready (packer) /usr/sbin/packer

With the file test.pkr.hcl opened:

Log file: /home/[email protected]/Documents/nvim_sandbox/.repro//state/nvim/conform.log

Formatters for this buffer:
hcl unavailable: Command not found

Other formatters:
packer_fmt ready (packer) /usr/sbin/packer

Describe the bug

Conform.nvim has support for detecting filetypes both root filetypes and nested filetypes such as hcl vs hcl.packer. So if I only configure packer in the formatters_by_ft then it correctly identifies hcl.packer as a compatible packer filetype. But I noticed if I configure both hcl and hcl.packer then the hcl formatters are the only ones that get attached to the buffer. I think the priority here should favor the more specific filetype.

What is the severity of this bug?

breaking (some functionality is broken)

Steps To Reproduce

  1. nvim -u repro.lua test.hcl, Open up an .hcl file
  2. :ConformInfo and see that hcl is set up
  3. :e test.pkr.hcl, open up a .pkr.hcl file
  4. :ConformInfo and see that hcl formatters are still used and the packer formatters are ignored.

Note: If the hcl formatters are removed and packer only exists, then opening a test.pkr.hcl file and running :ConformInfo shows that the formatters are correctly picked up for packer.

Expected Behavior

The formatter configured for the most specific filetype should be used. In this case, use the packer formatters for the filetype hcl.packer and the hcl formatters for the filetype hcl or any other hcl.X unless X is configured.

Minimal example file

Just needs files test.hcl and test.hcl.packer. Contents doesn't matter.

Also the main reason for using this nested filetype notation is to get stuff like treesitter and LSP to work nicely with the files without having to manually tell each that those nested filetypes should also be treated like their parent filetype.

Minimal init.lua

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system "curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua")()

vim.keymap.set("n", "<c-a>", function()
  vim.ui.input({ prompt = "Enter text: " }, function(input) vim.notify(input) end)
end)

-- Add a new filetype for `.pkr.hcl` files
vim.filetype.add {
  pattern = {
    [".*%.pkr%.hcl"] = "hcl.packer",
  },
}

require("lazy.minit").repro {
  spec = {
    {
      "stevearc/conform.nvim",
      opts = {
        log_level = vim.log.levels.DEBUG,
        -- add your config here
        formatters_by_ft = {
          hcl = { "hcl" },
          packer = { "packer_fmt" },
        },
      },
    },
  },
}

Additional context

In the example my logs show hcl command is not found. I just wanted to point out that this doesn't matter in the testing case just because I'm looking at how the buffer is configured and not having any problems with the formatters actually running.

Thanks so much for your maintenance of this plugin as it truly is an amazing formatting setup!

@mehalter mehalter added the bug Something isn't working label Jan 23, 2025
@mehalter
Copy link
Contributor Author

It is worth mentioning a workaround I am currently using if anyone else runs into this:

{
  "stevearc/conform.nvim",
  opts = {
    formatters_by_ft = {
      hcl = function(bufnr)
        if vim.bo[bufnr].filetype == "hcl.packer" then return { "packer_fmt" } end
        return { "hcl" }
      end,
    },
  },
},

which definitely gets the job done but I think the table notation is quite nice and makes maintenance easier.

@mehalter mehalter linked a pull request Jan 23, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant