Skip to content

Commit 9228b2f

Browse files
authored
feat!: expand options for LSP formatting (#456)
1 parent 6e5d476 commit 9228b2f

File tree

6 files changed

+161
-132
lines changed

6 files changed

+161
-132
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ require("conform").setup({
153153
format_on_save = {
154154
-- These options will be passed to conform.format()
155155
timeout_ms = 500,
156-
lsp_fallback = true,
156+
lsp_format = "fallback",
157157
},
158158
})
159159
```
@@ -466,14 +466,14 @@ require("conform").setup({
466466
-- This can also be a function that returns the table.
467467
format_on_save = {
468468
-- I recommend these options. See :help conform.format for details.
469-
lsp_fallback = true,
469+
lsp_format = "fallback",
470470
timeout_ms = 500,
471471
},
472472
-- If this is set, Conform will run the formatter asynchronously after save.
473473
-- It will pass the table to conform.format().
474474
-- This can also be a function that returns the table.
475475
format_after_save = {
476-
lsp_fallback = true,
476+
lsp_format = "fallback",
477477
},
478478
-- Set the log level. Use `:ConformInfo` to see the location of the log file.
479479
log_level = vim.log.levels.ERROR,
@@ -574,21 +574,21 @@ require("conform").formatters.my_formatter = {
574574
`format(opts, callback): boolean` \
575575
Format a buffer
576576

577-
| Param | Type | Desc | |
578-
| -------- | ---------------------------------------------------- | ------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
579-
| opts | `nil\|conform.FormatOpts` | | |
580-
| | timeout_ms | `nil\|integer` | Time in milliseconds to block for formatting. Defaults to 1000. No effect if async = true. |
581-
| | bufnr | `nil\|integer` | Format this buffer (default 0) |
582-
| | async | `nil\|boolean` | If true the method won't block. Defaults to false. If the buffer is modified before the formatter completes, the formatting will be discarded. |
583-
| | dry_run | `nil\|boolean` | If true don't apply formatting changes to the buffer |
584-
| | formatters | `nil\|string[]` | List of formatters to run. Defaults to all formatters for the buffer filetype. |
585-
| | lsp_fallback | `nil\|boolean\|"always"` | Attempt LSP formatting if no formatters are available. Defaults to false. If "always", will attempt LSP formatting even if formatters are available. |
586-
| | quiet | `nil\|boolean` | Don't show any notifications for warnings or failures. Defaults to false. |
587-
| | range | `nil\|table` | Range to format. Table must contain `start` and `end` keys with {row, col} tuples using (1,0) indexing. Defaults to current selection in visual mode |
588-
| | id | `nil\|integer` | Passed to vim.lsp.buf.format when lsp_fallback = true |
589-
| | name | `nil\|string` | Passed to vim.lsp.buf.format when lsp_fallback = true |
590-
| | filter | `nil\|fun(client: table): boolean` | Passed to vim.lsp.buf.format when lsp_fallback = true |
591-
| callback | `nil\|fun(err: nil\|string, did_edit: nil\|boolean)` | Called once formatting has completed | |
577+
| Param | Type | Desc | |
578+
| -------- | ---------------------------------------------------- | ----------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
579+
| opts | `nil\|conform.FormatOpts` | | |
580+
| | timeout_ms | `nil\|integer` | Time in milliseconds to block for formatting. Defaults to 1000. No effect if async = true. |
581+
| | bufnr | `nil\|integer` | Format this buffer (default 0) |
582+
| | async | `nil\|boolean` | If true the method won't block. Defaults to false. If the buffer is modified before the formatter completes, the formatting will be discarded. |
583+
| | dry_run | `nil\|boolean` | If true don't apply formatting changes to the buffer |
584+
| | formatters | `nil\|string[]` | List of formatters to run. Defaults to all formatters for the buffer filetype. |
585+
| | lsp_format | `nil\|"never"\|"fallback"\|"prefer"\|"first"\|"last"` | "fallback" LSP formatting when no other formatters are available, "prefer" only LSP formatting when available, "first" LSP formatting then other formatters, "last" other formatters then LSP. |
586+
| | quiet | `nil\|boolean` | Don't show any notifications for warnings or failures. Defaults to false. |
587+
| | range | `nil\|table` | Range to format. Table must contain `start` and `end` keys with {row, col} tuples using (1,0) indexing. Defaults to current selection in visual mode |
588+
| | id | `nil\|integer` | Passed to vim.lsp.buf.format when using LSP formatting |
589+
| | name | `nil\|string` | Passed to vim.lsp.buf.format when using LSP formatting |
590+
| | filter | `nil\|fun(client: table): boolean` | Passed to vim.lsp.buf.format when using LSP formatting |
591+
| callback | `nil\|fun(err: nil\|string, did_edit: nil\|boolean)` | Called once formatting has completed | |
592592

593593
Returns:
594594

@@ -624,7 +624,7 @@ Get information about a formatter (including availability)
624624
### will_fallback_lsp(options)
625625

626626
`will_fallback_lsp(options): boolean` \
627-
Check if the buffer will use LSP formatting when lsp_fallback = true
627+
Check if the buffer will use LSP formatting when lsp_format = "fallback"
628628

629629
| Param | Type | Desc |
630630
| ------- | ------------ | ------------------------------------ |

doc/conform.txt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ OPTIONS *conform-option
3838
-- This can also be a function that returns the table.
3939
format_on_save = {
4040
-- I recommend these options. See :help conform.format for details.
41-
lsp_fallback = true,
41+
lsp_format = "fallback",
4242
timeout_ms = 500,
4343
},
4444
-- If this is set, Conform will run the formatter asynchronously after save.
4545
-- It will pass the table to conform.format().
4646
-- This can also be a function that returns the table.
4747
format_after_save = {
48-
lsp_fallback = true,
48+
lsp_format = "fallback",
4949
},
5050
-- Set the log level. Use `:ConformInfo` to see the location of the log file.
5151
log_level = vim.log.levels.ERROR,
@@ -142,32 +142,32 @@ format({opts}, {callback}): boolean *conform.forma
142142

143143
Parameters:
144144
{opts} `nil|conform.FormatOpts`
145-
{timeout_ms} `nil|integer` Time in milliseconds to block for
146-
formatting. Defaults to 1000. No effect if async =
147-
true.
148-
{bufnr} `nil|integer` Format this buffer (default 0)
149-
{async} `nil|boolean` If true the method won't block. Defaults
150-
to false. If the buffer is modified before the
151-
formatter completes, the formatting will be discarded.
152-
{dry_run} `nil|boolean` If true don't apply formatting changes to
153-
the buffer
154-
{formatters} `nil|string[]` List of formatters to run. Defaults to
155-
all formatters for the buffer filetype.
156-
{lsp_fallback} `nil|boolean|"always"` Attempt LSP formatting if no
157-
formatters are available. Defaults to false. If
158-
"always", will attempt LSP formatting even if
159-
formatters are available.
160-
{quiet} `nil|boolean` Don't show any notifications for warnings
161-
or failures. Defaults to false.
162-
{range} `nil|table` Range to format. Table must contain `start`
163-
and `end` keys with {row, col} tuples using (1,0)
164-
indexing. Defaults to current selection in visual mode
165-
{id} `nil|integer` Passed to |vim.lsp.buf.format| when
166-
lsp_fallback = true
167-
{name} `nil|string` Passed to |vim.lsp.buf.format| when
168-
lsp_fallback = true
169-
{filter} `nil|fun(client: table): boolean` Passed to
170-
|vim.lsp.buf.format| when lsp_fallback = true
145+
{timeout_ms} `nil|integer` Time in milliseconds to block for
146+
formatting. Defaults to 1000. No effect if async = true.
147+
{bufnr} `nil|integer` Format this buffer (default 0)
148+
{async} `nil|boolean` If true the method won't block. Defaults to
149+
false. If the buffer is modified before the formatter
150+
completes, the formatting will be discarded.
151+
{dry_run} `nil|boolean` If true don't apply formatting changes to
152+
the buffer
153+
{formatters} `nil|string[]` List of formatters to run. Defaults to all
154+
formatters for the buffer filetype.
155+
{lsp_format} `nil|"never"|"fallback"|"prefer"|"first"|"last"` "fallbac
156+
k" LSP formatting when no other formatters are available,
157+
"prefer" only LSP formatting when available, "first" LSP
158+
formatting then other formatters, "last" other formatters
159+
then LSP.
160+
{quiet} `nil|boolean` Don't show any notifications for warnings
161+
or failures. Defaults to false.
162+
{range} `nil|table` Range to format. Table must contain `start`
163+
and `end` keys with {row, col} tuples using (1,0)
164+
indexing. Defaults to current selection in visual mode
165+
{id} `nil|integer` Passed to |vim.lsp.buf.format| when using
166+
LSP formatting
167+
{name} `nil|string` Passed to |vim.lsp.buf.format| when using
168+
LSP formatting
169+
{filter} `nil|fun(client: table): boolean` Passed to
170+
|vim.lsp.buf.format| when using LSP formatting
171171
{callback} `nil|fun(err: nil|string, did_edit: nil|boolean)` Called once
172172
formatting has completed
173173
Returns:
@@ -191,7 +191,7 @@ get_formatter_info({formatter}, {bufnr}): conform.FormatterInfo *conform.get_for
191191
{bufnr} `nil|integer`
192192

193193
will_fallback_lsp({options}): boolean *conform.will_fallback_lsp*
194-
Check if the buffer will use LSP formatting when lsp_fallback = true
194+
Check if the buffer will use LSP formatting when lsp_format = "fallback"
195195

196196
Parameters:
197197
{options} `nil|table` Options passed to |vim.lsp.buf.format|

doc/recipes.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ vim.api.nvim_create_user_command("Format", function(args)
2424
["end"] = { args.line2, end_line:len() },
2525
}
2626
end
27-
require("conform").format({ async = true, lsp_fallback = true, range = range })
27+
require("conform").format({ async = true, lsp_format = "fallback", range = range })
2828
end, { range = true })
2929
```
3030

@@ -53,7 +53,7 @@ require("conform").setup({
5353
return
5454
end
5555
-- ...additional logic...
56-
return { timeout_ms = 500, lsp_fallback = true }
56+
return { timeout_ms = 500, lsp_format = "fallback" }
5757
end,
5858
})
5959

@@ -65,7 +65,7 @@ require("conform").setup({
6565
return
6666
end
6767
-- ...additional logic...
68-
return { lsp_fallback = true }
68+
return { lsp_format = "fallback" }
6969
end,
7070
})
7171
```
@@ -83,7 +83,7 @@ require("conform").setup({
8383
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
8484
return
8585
end
86-
return { timeout_ms = 500, lsp_fallback = true }
86+
return { timeout_ms = 500, lsp_format = "fallback" }
8787
end,
8888
})
8989

@@ -123,14 +123,14 @@ require("conform").setup({
123123
end
124124
end
125125

126-
return { timeout_ms = 200, lsp_fallback = true }, on_format
126+
return { timeout_ms = 200, lsp_format = "fallback" }, on_format
127127
end,
128128

129129
format_after_save = function(bufnr)
130130
if not slow_format_filetypes[vim.bo[bufnr].filetype] then
131131
return
132132
end
133-
return { lsp_fallback = true }
133+
return { lsp_format = "fallback" }
134134
end,
135135
})
136136
```
@@ -149,7 +149,7 @@ return {
149149
-- Customize or remove this keymap to your liking
150150
"<leader>f",
151151
function()
152-
require("conform").format({ async = true, lsp_fallback = true })
152+
require("conform").format({ async = true, lsp_format = "fallback" })
153153
end,
154154
mode = "",
155155
desc = "Format buffer",
@@ -164,7 +164,7 @@ return {
164164
javascript = { { "prettierd", "prettier" } },
165165
},
166166
-- Set up format-on-save
167-
format_on_save = { timeout_ms = 500, lsp_fallback = true },
167+
format_on_save = { timeout_ms = 500, lsp_format = "fallback" },
168168
-- Customize formatters
169169
formatters = {
170170
shfmt = {

0 commit comments

Comments
 (0)