aboutsummaryrefslogtreecommitdiffstats
path: root/lua/conform/runner.lua
diff options
context:
space:
mode:
authorGrolaf <75337003+Grolaf@users.noreply.github.com>2024-07-16 06:07:55 +0200
committerGitHub <noreply@github.com>2024-07-15 21:07:55 -0700
commit1d1362b0261d06a0b91872e916c172320bbb988a (patch)
tree599c2cfe050dafb4e824fc83ab505613c0c80517 /lua/conform/runner.lua
parentcc1ba956b61543641ddeeb7694c7cdaa33cd157c (diff)
feat: add undojoin as a format option (#488)
* feat: add new format option: undojoin This option allow user to automatically perform undojoin command before formatting. This is useful if the user uses an autosave plugin + format on save, because in case of undo, it will undo the last change AND the formatting. Without this option, it will only undo the formatting. * fix: passed linting * fix: apply undojoin for LSP formatting * doc: fix type annotations for apply_format * doc: regenerate documentation --------- Co-authored-by: Steven Arcangeli <stevearc@stevearc.com>
Diffstat (limited to 'lua/conform/runner.lua')
-rw-r--r--lua/conform/runner.lua22
1 files changed, 19 insertions, 3 deletions
diff --git a/lua/conform/runner.lua b/lua/conform/runner.lua
index c0a19cd..333c16f 100644
--- a/lua/conform/runner.lua
+++ b/lua/conform/runner.lua
@@ -9,6 +9,7 @@ local M = {}
---@class (exact) conform.RunOpts
---@field exclusive boolean If true, ensure only a single formatter is running per buffer
---@field dry_run boolean If true, do not apply changes and stop after the first formatter attempts to do so
+---@field undojoin boolean Use undojoin to merge formatting changes with previous edit
---@param formatter_name string
---@param ctx conform.Context
@@ -168,8 +169,18 @@ end
---@param new_lines string[]
---@param range? conform.Range
---@param only_apply_range boolean
+---@param dry_run boolean
+---@param undojoin boolean
---@return boolean any_changes
-M.apply_format = function(bufnr, original_lines, new_lines, range, only_apply_range, dry_run)
+M.apply_format = function(
+ bufnr,
+ original_lines,
+ new_lines,
+ range,
+ only_apply_range,
+ dry_run,
+ undojoin
+)
if bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end
@@ -251,6 +262,9 @@ M.apply_format = function(bufnr, original_lines, new_lines, range, only_apply_ra
if not dry_run then
log.trace("Applying text edits: %s", text_edits)
+ if undojoin then
+ vim.cmd.undojoin()
+ end
vim.lsp.util.apply_text_edits(text_edits, bufnr, "utf-8")
log.trace("Done formatting %s", bufname)
end
@@ -535,7 +549,8 @@ M.format_async = function(bufnr, formatters, range, opts, callback)
output_lines,
range,
not all_support_range_formatting,
- opts.dry_run
+ opts.dry_run,
+ opts.undojoin
)
end
callback(err, did_edit)
@@ -609,7 +624,8 @@ M.format_sync = function(bufnr, formatters, timeout_ms, range, opts)
final_result,
range,
not all_support_range_formatting,
- opts.dry_run
+ opts.dry_run,
+ opts.undojoin
)
return err, did_edit
end