aboutsummaryrefslogtreecommitdiffstats
path: root/lua/conform
diff options
context:
space:
mode:
authorSteven Arcangeli <stevearc@stevearc.com>2023-09-15 17:43:14 -0700
committerSteven Arcangeli <stevearc@stevearc.com>2023-09-15 17:44:03 -0700
commitcb5f939ab27b2c2ef2e1d4ac6fe16c5ba6332f39 (patch)
tree546ce64c755ad04b916f00c8ca398af8739d206c /lua/conform
parentaa38b05575dab57b813ddcd14780f65ff20a6d49 (diff)
feat: utility function to extend the built-in formatter args (#50)
Diffstat (limited to 'lua/conform')
-rw-r--r--lua/conform/util.lua42
1 files changed, 42 insertions, 0 deletions
diff --git a/lua/conform/util.lua b/lua/conform/util.lua
index ea13e2d..5813736 100644
--- a/lua/conform/util.lua
+++ b/lua/conform/util.lua
@@ -72,4 +72,46 @@ M.wrap_callback = function(cb, wrapper)
end
end
+---Helper function to add to the default args of a formatter.
+---@param args string|string[]|fun(ctx: conform.Context): string|string[]
+---@param extra_args string|string[]|fun(ctx: conform.Context): string|string[]
+---@param opts? { append?: boolean }
+---@example
+--- local util = require("conform.util")
+--- local prettier = require("conform.formatters.prettier")
+--- require("conform").formatters.prettier = vim.tbl_deep_extend("force", prettier, {
+--- args = util.extend_args(prettier.args, { "--tab", "--indent", "2" }),
+--- range_args = util.extend_args(prettier.range_args, { "--tab", "--indent", "2" }),
+--- })
+M.extend_args = function(args, extra_args, opts)
+ opts = opts or {}
+ return function(ctx)
+ if type(args) == "function" then
+ args = args(ctx)
+ end
+ if type(extra_args) == "function" then
+ extra_args = extra_args(ctx)
+ end
+ if type(args) == "string" then
+ if type(extra_args) ~= "string" then
+ extra_args = table.concat(extra_args, " ")
+ end
+ if opts.append then
+ return args .. " " .. extra_args
+ else
+ return extra_args .. " " .. args
+ end
+ else
+ if type(extra_args) == "string" then
+ error("extra_args must be a table when args is a table")
+ end
+ if opts.append then
+ return vim.tbl_flatten({ args, extra_args })
+ else
+ return vim.tbl_flatten({ extra_args, args })
+ end
+ end
+ end
+end
+
return M