Skip to content

Commit

Permalink
cleanup!: remove deprecated syntax and functions
Browse files Browse the repository at this point in the history
Removes support for the alternation syntax in formatters_by_ft to run
the first formatter in a group (e.g. `javascript = { { "prettierd",
"prettier" } }`. This has been replaced by the `stop_after_first` option
with a migration notification for a long time now.

Also removes `conform.will_fallback_lsp`. This can be replaced by
`conform.list_formatters_to_run`.
  • Loading branch information
stevearc committed Jan 23, 2025
1 parent 80b57f6 commit f8d743c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 90 deletions.
72 changes: 16 additions & 56 deletions lua/conform/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,11 @@ M.list_formatters_for_buffer = function(bufnr)

local function dedupe_formatters(names, collect)
for _, name in ipairs(names) do
if type(name) == "table" then
notify_once(
"deprecated[conform]: The nested {} syntax to run the first formatter has been replaced by the stop_after_first option (see :help conform.format).\nSupport for the old syntax will be dropped on 2025-01-01.",
vim.log.levels.WARN
)
local alternation = {}
dedupe_formatters(name, alternation)
if not vim.tbl_isempty(alternation) then
table.insert(collect, alternation)
end
elseif not seen[name] then
assert(
type(name) == "string",
"The nested {} syntax to run the first formatter has been replaced by the stop_after_first option (see :help conform.format)."
)
if not seen[name] then
table.insert(collect, name)
seen[name] = true
end
Expand Down Expand Up @@ -369,22 +363,12 @@ M.resolve_formatters = function(names, bufnr, warn_on_missing, stop_after_first)
end

for _, name in ipairs(names) do
if type(name) == "string" then
local info = M.get_formatter_info(name, bufnr)
add_info(info, warn_on_missing)
else
notify_once(
"deprecated[conform]: The nested {} syntax to run the first formatter has been replaced by the stop_after_first option (see :help conform.format).\nSupport for the old syntax will be dropped on 2025-01-01.",
vim.log.levels.WARN
)
-- If this is an alternation, take the first one that's available
for i, v in ipairs(name) do
local info = M.get_formatter_info(v, bufnr)
if add_info(info, warn_on_missing and i == #name) then
break
end
end
end
assert(
type(name) == "string",
"The nested {} syntax to run the first formatter has been replaced by the stop_after_first option (see :help conform.format)."
)
local info = M.get_formatter_info(name, bufnr)
add_info(info, warn_on_missing)

if stop_after_first and #all_info > 0 then
break
Expand Down Expand Up @@ -685,17 +669,11 @@ M.list_all_formatters = function()
end

for _, formatter in ipairs(ft_formatters) do
if type(formatter) == "table" then
notify_once(
"deprecated[conform]: The nested {} syntax to run the first formatter has been replaced by the stop_after_first option (see :help conform.format).\nSupport for the old syntax will be dropped on 2025-01-01.",
vim.log.levels.WARN
)
for _, v in ipairs(formatter) do
formatters[v] = true
end
else
formatters[formatter] = true
end
assert(
type(formatter) == "string",
"The nested {} syntax to run the first formatter has been replaced by the stop_after_first option (see :help conform.format)."
)
formatters[formatter] = true
end
end

Expand Down Expand Up @@ -834,24 +812,6 @@ M.get_formatter_info = function(formatter, bufnr)
}
end

---Check if the buffer will use LSP formatting when lsp_format = "fallback"
---@deprecated
---@param options? table Options passed to |vim.lsp.buf.format|
---@return boolean
M.will_fallback_lsp = function(options)
notify_once(
"deprecated[conform]: will_fallback_lsp is deprecated. Use list_formatters_to_run instead.\nThis method will be removed on 2025-01-01.",
vim.log.levels.WARN
)
options = vim.tbl_deep_extend("keep", options or {}, {
bufnr = vim.api.nvim_get_current_buf(),
})
if options.bufnr == 0 then
options.bufnr = vim.api.nvim_get_current_buf()
end
return not has_filetype_formatters(options.bufnr) and has_lsp_formatter(options)
end

M.formatexpr = function(opts)
-- Change the defaults slightly from conform.format
opts = vim.tbl_deep_extend("keep", opts or {}, {
Expand Down
43 changes: 9 additions & 34 deletions tests/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,16 @@ describe("api", function()
end, formatters)
assert.are.same({ "stylua", "lua-format", "trim_whitespace" }, formatter_names)
end)

it("flattens formatters in alternation groups", function()
conform.formatters_by_ft.lua = { { "stylua", "lua-format" }, "trim_whitespace" }
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_current_buf(bufnr)
vim.bo[bufnr].filetype = "lua"
local formatters = conform.list_formatters()
local formatter_names = vim.tbl_map(function(f)
return f.name
end, formatters)
assert.are.same({ "stylua", "trim_whitespace" }, formatter_names)
end)
end)

describe("list_all_formatters", function()
it("lists all formatters configured for all buffers", function()
conform.formatters_by_ft.lua = { "stylua", "lua-format" }
conform.formatters_by_ft["*"] = { "trim_whitespace" }
local formatters = conform.list_all_formatters()
local formatter_names = vim.tbl_map(function(f)
return f.name
end, formatters)
table.sort(formatter_names)
assert.are.same({ "lua-format", "stylua", "trim_whitespace" }, formatter_names)
end)

it("flattens formatters in alternation groups", function()
conform.formatters_by_ft.lua = { { "stylua", "lua-format" } }
conform.formatters_by_ft["*"] = { "trim_whitespace" }
local formatters = conform.list_all_formatters()
local formatter_names = vim.tbl_map(function(f)
return f.name
end, formatters)
table.sort(formatter_names)
assert.are.same({ "lua-format", "stylua", "trim_whitespace" }, formatter_names)
end)
it("lists_all_formatters configured for all buffers", function()
conform.formatters_by_ft.lua = { "stylua", "lua-format" }
conform.formatters_by_ft["*"] = { "trim_whitespace" }
local formatters = conform.list_all_formatters()
local formatter_names = vim.tbl_map(function(f)
return f.name
end, formatters)
table.sort(formatter_names)
assert.are.same({ "lua-format", "stylua", "trim_whitespace" }, formatter_names)
end)
end)

0 comments on commit f8d743c

Please sign in to comment.