Skip to content

Commit

Permalink
feat: '_' filetype to define fallback formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 16, 2023
1 parent 705d363 commit a589750
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,14 @@ require("conform").setup({
python = { "isort", "black" },
-- Use a sub-list to run only the first available formatter
javascript = { { "prettierd", "prettier" } },
-- Use the "*" filetype to run formatters on all files.
-- Use the "*" filetype to run formatters on all filetypes.
-- Note that if you use this, you may want to set lsp_fallback = "always"
-- (see :help conform.format)
["*"] = { "trim_whitespace" },
["*"] = { "codespell" },
-- Use the "_" filetype to run formatters on all filetypes
-- that don't have other formatters configured. Again, you may want to
-- set lsp_fallback = "always" when using this value.
["_"] = { "trim_whitespace" },
},
-- If this is set, Conform will run the formatter on save.
-- It will pass the table to conform.format().
Expand Down
8 changes: 6 additions & 2 deletions doc/conform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ OPTIONS *conform-option
python = { "isort", "black" },
-- Use a sub-list to run only the first available formatter
javascript = { { "prettierd", "prettier" } },
-- Use the "*" filetype to run formatters on all files.
-- Use the "*" filetype to run formatters on all filetypes.
-- Note that if you use this, you may want to set lsp_fallback = "always"
-- (see :help conform.format)
["*"] = { "trim_whitespace" },
["*"] = { "codespell" },
-- Use the "_" filetype to run formatters on all filetypes
-- that don't have other formatters configured. Again, you may want to
-- set lsp_fallback = "always" when using this value.
["_"] = { "trim_whitespace" },
},
-- If this is set, Conform will run the formatter on save.
-- It will pass the table to conform.format().
Expand Down
13 changes: 12 additions & 1 deletion lua/conform/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ M.list_formatters_for_buffer = function(bufnr)
end
end

table.insert(filetypes, "*")
table.insert(filetypes, "_")
for _, filetype in ipairs(filetypes) do
---@type conform.FormatterUnit[]
local ft_formatters = M.formatters_by_ft[filetype]
Expand All @@ -183,7 +183,18 @@ M.list_formatters_for_buffer = function(bufnr)
end

dedupe_formatters(ft_formatters, formatters)
break
end
end

local ft_formatters = M.formatters_by_ft["*"]
if ft_formatters then
-- support the old structure where formatters could be a subkey
if not vim.tbl_islist(ft_formatters) then
---@diagnostic disable-next-line: undefined-field
ft_formatters = ft_formatters.formatters
end
dedupe_formatters(ft_formatters, formatters)
end

return formatters
Expand Down
8 changes: 6 additions & 2 deletions scripts/options_doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ require("conform").setup({
python = { "isort", "black" },
-- Use a sub-list to run only the first available formatter
javascript = { { "prettierd", "prettier" } },
-- Use the "*" filetype to run formatters on all files.
-- Use the "*" filetype to run formatters on all filetypes.
-- Note that if you use this, you may want to set lsp_fallback = "always"
-- (see :help conform.format)
["*"] = { "trim_whitespace" },
["*"] = { "codespell" },
-- Use the "_" filetype to run formatters on all filetypes
-- that don't have other formatters configured. Again, you may want to
-- set lsp_fallback = "always" when using this value.
["_"] = { "trim_whitespace" },
},
-- If this is set, Conform will run the formatter on save.
-- It will pass the table to conform.format().
Expand Down

0 comments on commit a589750

Please sign in to comment.