|
9 | 9 | - [Lazy loading with lazy.nvim](#lazy-loading-with-lazynvim) |
10 | 10 | - [Leave visual mode after range format](#leave-visual-mode-after-range-format) |
11 | 11 | - [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) |
12 | 13 |
|
13 | 14 | <!-- /TOC --> |
14 | 15 |
|
@@ -114,30 +115,30 @@ You can run LSP commands before formatting a file. This example uses the `ts_ls` |
114 | 115 |
|
115 | 116 | ```lua |
116 | 117 | 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, |
141 | 142 | }) |
142 | 143 | ``` |
143 | 144 |
|
@@ -238,3 +239,29 @@ require("conform").setup({ |
238 | 239 | }, |
239 | 240 | }) |
240 | 241 | ``` |
| 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