Skip to content

Commit

Permalink
feat: utility function to extend the built-in formatter args (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 16, 2023
1 parent aa38b05 commit cb5f939
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand Down
28 changes: 28 additions & 0 deletions doc/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand Down Expand Up @@ -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 })
})
```
42 changes: 42 additions & 0 deletions lua/conform/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cb5f939

Please sign in to comment.