diff options
author | Grolaf <75337003+Grolaf@users.noreply.github.com> | 2024-07-16 06:07:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-15 21:07:55 -0700 |
commit | 1d1362b0261d06a0b91872e916c172320bbb988a (patch) | |
tree | 599c2cfe050dafb4e824fc83ab505613c0c80517 /lua/conform/lsp_format.lua | |
parent | cc1ba956b61543641ddeeb7694c7cdaa33cd157c (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/lsp_format.lua')
-rw-r--r-- | lua/conform/lsp_format.lua | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/lua/conform/lsp_format.lua b/lua/conform/lsp_format.lua index a4f9556..fe26656 100644 --- a/lua/conform/lsp_format.lua +++ b/lua/conform/lsp_format.lua @@ -4,7 +4,7 @@ local util = require("vim.lsp.util") local M = {} -local function apply_text_edits(text_edits, bufnr, offset_encoding, dry_run) +local function apply_text_edits(text_edits, bufnr, offset_encoding, dry_run, undojoin) if #text_edits == 1 and text_edits[1].range.start.line == 0 @@ -25,11 +25,15 @@ local function apply_text_edits(text_edits, bufnr, offset_encoding, dry_run) new_lines, nil, false, - dry_run + dry_run, + undojoin ) elseif dry_run then return #text_edits > 0 else + if undojoin then + vim.cmd.undojoin() + end vim.lsp.util.apply_text_edits(text_edits, bufnr, offset_encoding) return #text_edits > 0 end @@ -124,8 +128,13 @@ function M.format(options, callback) ) ) else - local this_did_edit = - apply_text_edits(result, ctx.bufnr, client.offset_encoding, options.dry_run) + local this_did_edit = apply_text_edits( + result, + ctx.bufnr, + client.offset_encoding, + options.dry_run, + options.undojoin + ) changedtick = vim.b[bufnr].changedtick if options.dry_run and this_did_edit then @@ -145,8 +154,13 @@ function M.format(options, callback) local params = set_range(client, util.make_formatting_params(options.formatting_options)) local result, err = client.request_sync(method, params, timeout_ms, bufnr) if result and result.result then - local this_did_edit = - apply_text_edits(result.result, bufnr, client.offset_encoding, options.dry_run) + local this_did_edit = apply_text_edits( + result.result, + bufnr, + client.offset_encoding, + options.dry_run, + options.undojoin + ) did_edit = did_edit or this_did_edit if options.dry_run and did_edit then |