Skip to content

Commit

Permalink
chore: make suggestion insertion undoable
Browse files Browse the repository at this point in the history
  • Loading branch information
kassick committed Nov 29, 2024
1 parent b4cff81 commit c784b70
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lsp-inline-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@
(require 'cl-lib)
(require 'fringe)

(if (version< emacs-version "29.1")
;; Undo macro probably introduced in 29.1
(defmacro lsp-inline-completion--with-undo-amalgamate (&rest body)
"Like `progn' but perform BODY with amalgamated undo barriers.
This allows multiple operations to be undone in a single step.
When undo is disabled this behaves like `progn'."
(declare (indent 0) (debug t))
(let ((handle (make-symbol "--change-group-handle--")))
`(let ((,handle (prepare-change-group))
;; Don't truncate any undo data in the middle of this,
;; otherwise Emacs might truncate part of the resulting
;; undo step: we want to mimic the behavior we'd get if the
;; undo-boundaries were never added in the first place.
(undo-outer-limit nil)
(undo-limit most-positive-fixnum)
(undo-strong-limit most-positive-fixnum))
(unwind-protect
(progn
(activate-change-group ,handle)
,@body)
(progn
(accept-change-group ,handle)
(undo-amalgamate-change-group ,handle))))))
(defalias 'lsp-inline-completion--with-undo-amalgamate 'with-undo-amalgamate))

(defun lsp-inline-completion--params (implicit &optional identifier position)
"Returns a InlineCompletionParams instance"
(lsp-make-inline-completion-params
Expand Down Expand Up @@ -281,7 +307,9 @@ text range that was updated by the completion"
(delete-region start end))

;; Insert suggestion, keeping the cursor at the start point
(insert text)
(lsp-inline-completion--with-undo-amalgamate
(insert text))

(setq text-insert-end (point))

;; If a template, format it -- keep track of the end position!
Expand Down

0 comments on commit c784b70

Please sign in to comment.