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: formatters should merge #634

Closed
winkee01 opened this issue Jan 29, 2025 · 1 comment
Closed

bug: formatters should merge #634

winkee01 opened this issue Jan 29, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@winkee01
Copy link

winkee01 commented Jan 29, 2025

Neovim version (nvim -v)

NVIM v0.11.0

Operating system/version

MacOS 15.1.1

Describe the bug

in lua/conform/formatters/init.lua's M.format() function, the below statement is not logically very correct.

local formatter_names = opts.formatters or M.list_formatters_for_buffer(opts.bufnr)

Because, when opts.formatters = {}, then even if M.list_formatters_for_buffer(opts.bufnr) has value, which is totally correct and useful (when you configured formatters_by_ft = {lua = { "stylua" }, it will not be applied. this will cause formatter_names to be {} and thus all the later formatters be {}. but obviously you have the formatter stylua available.

Therefore, I think you should merge opts.formatters and M.list_formatters_for_buffer(opts.bufnr) instead of pick only one of them.

or at least, test if opts.formatters is empty, then use M.list_formatters_for_buffer(opts.bufnr) otherwise. like below:

local formatter_names = is_empty(opts.formatters) or M.list_formatters_for_buffer(opts.bufnr)

Warn on missing

also in lua/conform/init.lua, the warn message (3rd arg) is hardcodet to false (M.resolve_formatters(formatters, bufnr, false, false)`, this will mute errors when an incorrect formatter is configured. this setting may be improved.

M.list_formatters = function(bufnr)
  if not bufnr or bufnr == 0 then
    bufnr = vim.api.nvim_get_current_buf()
  end
  local formatters = M.list_formatters_for_buffer(bufnr)
  return M.resolve_formatters(formatters, bufnr, hardcoded_false, false)
end
@winkee01 winkee01 added the bug Something isn't working label Jan 29, 2025
@stevearc
Copy link
Owner

These are both intentional. There is no fancy merging logic for the formatters parameter because I want the behavior of format() to be very easy to understand when you pass in explicit arguments. If the built-in mechanisms of setting formatters_by_ft cover your use case, great. If not, then it's up to you to decide what formatters to run and when, and to pass them into the function.

We do not warn on missing formatters in list_formatters because that function is designed to be used for purposes like a statusline, where spamming notifications is not desired. If you want to find missing formatters programmatically, you can use list_all_formatters or get_formatter_info

@stevearc stevearc added enhancement New feature or request and removed bug Something isn't working labels Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants