From cb5f939ab27b2c2ef2e1d4ac6fe16c5ba6332f39 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Fri, 15 Sep 2023 17:43:14 -0700 Subject: feat: utility function to extend the built-in formatter args (#50) --- doc/recipes.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'doc') 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) @@ -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 }) +}) +``` -- cgit v1.2.3-70-g09d2