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

feature request: #504

Open
1 task done
beaumccartney opened this issue Jul 27, 2024 · 3 comments
Open
1 task done

feature request: #504

beaumccartney opened this issue Jul 27, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@beaumccartney
Copy link

Did you check existing requests?

  • I have searched the existing issues

Describe the feature

in the old syntax, I could list a few formatters to try in order, and stop at the first just in the setup call i.e. per filetype

conform.setup({
    formatters_by_ft = {
        javascript = { { "prettierd", "prettier" } },
    },
})

for js files, this would use prettierd if available, and prettier otherwise.

afaik that api's deprecated (message everytime I use it), but from my reading and code spelunking, its not actually possible to specify stop_after_first in the table

is it possible to use the old functionality without using the deprecated api?

Provide background

provided above

What is the significance of this feature?

strongly desired

Additional details

was an old feature, was removed(??)

@beaumccartney beaumccartney added the enhancement New feature or request label Jul 27, 2024
@shubham-cpp
Copy link

My question is, how to now run multiple formatters + conditionally run first available?
In my config I had the following
tldr; for go files run goimports + gofumpt (if available) or gofmt

require('conform').setup({
  formatters_by_ft = {
    go = { 'goimports', { 'gofumpt', 'gofmt' } },
    javascript = { { 'prettierd', 'prettier' }, 'eslint_d' },
    javascriptreact = { { 'prettierd', 'prettier' }, 'eslint_d' },
})

How can I now achieve this in stop_after_first ?

@bpinto
Copy link

bpinto commented Jul 30, 2024

@RingOfStorms
Copy link

RingOfStorms commented Oct 4, 2024

In addition to the function linked above, I also made this expandFormatters so that you can just keep the old format, because calling the first in a custom function every time is silly

---@param bufnr integer
---@param ... string
---@return string
local function first(bufnr, ...)
	local conform = require("conform")
	for i = 1, select("#", ...) do
		local formatter = select(i, ...)
		if conform.get_formatter_info(formatter, bufnr).available then
			return formatter
		end
	end
	return select(1, ...)
end

local function expandFormatters(formatters)
	return function(bufnr)
		local result = {}
		for i = 1, #formatters do
			local formatter = formatters[i]
			if type(formatter) == "table" then
				result[i] = first(bufnr, unpack(formatter))
			else
				result[i] = formatter
			end
		end
		return result
	end
end
typescript = expandFormatters({ { "prettierd", "prettier" }, "rustywind", lsp_format = "fallback" })

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

4 participants