Skip to content

Commit 117e9ca

Browse files
committed
doc: add recipe for command to run tweaked formatter
1 parent 94e6811 commit 117e9ca

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ The following magic strings are available in `args` and `range_args`. They will
524524
- [Lazy loading with lazy.nvim](doc/recipes.md#lazy-loading-with-lazynvim)
525525
- [Leave visual mode after range format](doc/recipes.md#leave-visual-mode-after-range-format)
526526
- [Run the first available formatter followed by more formatters](doc/recipes.md#run-the-first-available-formatter-followed-by-more-formatters)
527+
- [Create a separate command to do special formatting you don't want to happen on save](doc/recipes.md#create-a-separate-command-to-do-special-formatting-you-dont-want-to-happen-on-save)
527528

528529
<!-- /RECIPES -->
529530

doc/recipes.md

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Lazy loading with lazy.nvim](#lazy-loading-with-lazynvim)
1010
- [Leave visual mode after range format](#leave-visual-mode-after-range-format)
1111
- [Run the first available formatter followed by more formatters](#run-the-first-available-formatter-followed-by-more-formatters)
12+
- [Create a separate command to do special formatting you don't want to happen on save](#create-a-separate-command-to-do-special-formatting-you-dont-want-to-happen-on-save)
1213

1314
<!-- /TOC -->
1415

@@ -114,30 +115,30 @@ You can run LSP commands before formatting a file. This example uses the `ts_ls`
114115

115116
```lua
116117
vim.api.nvim_create_autocmd("BufWritePre", {
117-
desc = "Format before save",
118-
pattern = "*",
119-
group = vim.api.nvim_create_augroup("FormatConfig", { clear = true }),
120-
callback = function(ev)
121-
local conform_opts = { bufnr = ev.buf, lsp_format = "fallback", timeout_ms = 2000 }
122-
local client = vim.lsp.get_clients({ name = "ts_ls", bufnr = ev.buf })[1]
123-
124-
if not client then
125-
require("conform").format(conform_opts)
126-
return
127-
end
128-
129-
local request_result = client:request_sync("workspace/executeCommand", {
130-
command = "_typescript.organizeImports",
131-
arguments = { vim.api.nvim_buf_get_name(ev.buf) },
132-
})
133-
134-
if request_result and request_result.err then
135-
vim.notify(request_result.err.message, vim.log.levels.ERROR)
136-
return
137-
end
138-
139-
require("conform").format(conform_opts)
140-
end,
118+
desc = "Format before save",
119+
pattern = "*",
120+
group = vim.api.nvim_create_augroup("FormatConfig", { clear = true }),
121+
callback = function(ev)
122+
local conform_opts = { bufnr = ev.buf, lsp_format = "fallback", timeout_ms = 2000 }
123+
local client = vim.lsp.get_clients({ name = "ts_ls", bufnr = ev.buf })[1]
124+
125+
if not client then
126+
require("conform").format(conform_opts)
127+
return
128+
end
129+
130+
local request_result = client:request_sync("workspace/executeCommand", {
131+
command = "_typescript.organizeImports",
132+
arguments = { vim.api.nvim_buf_get_name(ev.buf) },
133+
})
134+
135+
if request_result and request_result.err then
136+
vim.notify(request_result.err.message, vim.log.levels.ERROR)
137+
return
138+
end
139+
140+
require("conform").format(conform_opts)
141+
end,
141142
})
142143
```
143144

@@ -238,3 +239,29 @@ require("conform").setup({
238239
},
239240
})
240241
```
242+
243+
## Create a separate command to do special formatting you don't want to happen on save
244+
245+
Some formatters have multiple modes, or perform some operations that you don't want to happen on
246+
every save, but you do want to run them on-demand. For these, you can declare them as a separate
247+
formatter and create a keymap that invokes it directly. In this example, we will use
248+
`gdscript-formatter` to format on save and have a special keymap to use it to reorder code.
249+
250+
```lua
251+
require("conform").setup({
252+
formatters_by_ft = {
253+
gdscript = { "gdscript-formatter" },
254+
},
255+
formatters = {
256+
["gdscript-reorder"] = {
257+
inherit = "gdscript-formatter",
258+
prepend_args = { "--reorder-code" },
259+
},
260+
},
261+
format_on_save = {},
262+
})
263+
264+
vim.keymap.set("n", "<leader>fo", function()
265+
require("conform").format({ formatters = { "gdscript-reorder" } })
266+
end, { desc = "[F]ormat Re[O]rder" })
267+
```

0 commit comments

Comments
 (0)