aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--doc/recipes.md28
-rw-r--r--lua/conform/util.lua42
3 files changed, 71 insertions, 0 deletions
diff --git a/README.md b/README.md
index 6592bff..1d7ec64 100644
--- a/README.md
+++ b/README.md
@@ -321,6 +321,7 @@ require("conform").formatters.my_formatter = {
- [Autoformat with extra features](doc/recipes.md#autoformat-with-extra-features)
- [Command to toggle format-on-save](doc/recipes.md#command-to-toggle-format-on-save)
- [Automatically run slow formatters async](doc/recipes.md#automatically-run-slow-formatters-async)
+- [Add extra arguments to a formatter command](doc/recipes.md#add-extra-arguments-to-a-formatter-command)
<!-- /RECIPES -->
diff --git a/doc/recipes.md b/doc/recipes.md
index 8dae5fc..c235475 100644
--- a/doc/recipes.md
+++ b/doc/recipes.md
@@ -7,6 +7,7 @@
- [Autoformat with extra features](#autoformat-with-extra-features)
- [Command to toggle format-on-save](#command-to-toggle-format-on-save)
- [Automatically run slow formatters async](#automatically-run-slow-formatters-async)
+- [Add extra arguments to a formatter command](#add-extra-arguments-to-a-formatter-command)
<!-- /TOC -->
@@ -164,3 +165,30 @@ require("conform").setup({
end,
})
```
+
+## Add extra arguments to a formatter command
+
+The official recommended way to change the arguments of a formatter is to just copy the default
+values and mutate them however you like. For example:
+
+```lua
+require("conform.formatters.shfmt").args = { "-i", "4", "-filename", "$FILENAME" }
+```
+
+But if you really want to _add_ arguments instead of replacing them, there is a utility function to
+make this easier:
+
+```lua
+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" }),
+})
+
+-- Pass append=true to append the extra arguments to the end
+local deno_fmt = require("conform.formatters.deno_fmt")
+require("conform").formatters.deno_fmt = vim.tbl_deep_extend('force', deno_fmt, {
+ args = util.extend_args(deno_fmt.args, { "--use-tabs" }, { append = true })
+})
+```
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