Skip to content

Commit a109f34

Browse files
committed
chore: make suggestion insertion undoable
1 parent b4cff81 commit a109f34

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

lsp-inline-completion.el

+54-21
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@
3333
(require 'cl-lib)
3434
(require 'fringe)
3535

36+
(if (version< emacs-version "29.1")
37+
;; Undo macro probably introduced in 29.1
38+
(defmacro lsp-inline-completion--with-undo-amalgamate (&rest body)
39+
"Like `progn' but perform BODY with amalgamated undo barriers.
40+
41+
This allows multiple operations to be undone in a single step.
42+
When undo is disabled this behaves like `progn'."
43+
(declare (indent 0) (debug t))
44+
(let ((handle (make-symbol "--change-group-handle--")))
45+
`(let ((,handle (prepare-change-group))
46+
;; Don't truncate any undo data in the middle of this,
47+
;; otherwise Emacs might truncate part of the resulting
48+
;; undo step: we want to mimic the behavior we'd get if the
49+
;; undo-boundaries were never added in the first place.
50+
(undo-outer-limit nil)
51+
(undo-limit most-positive-fixnum)
52+
(undo-strong-limit most-positive-fixnum))
53+
(unwind-protect
54+
(progn
55+
(activate-change-group ,handle)
56+
,@body)
57+
(progn
58+
(accept-change-group ,handle)
59+
(undo-amalgamate-change-group ,handle))))))
60+
(defalias 'lsp-inline-completion--with-undo-amalgamate 'with-undo-amalgamate))
61+
3662
(defun lsp-inline-completion--params (implicit &optional identifier position)
3763
"Returns a InlineCompletionParams instance"
3864
(lsp-make-inline-completion-params
@@ -251,27 +277,12 @@ text range that was updated by the completion"
251277
(lsp-inline-completion--show-keys)
252278
(run-hooks 'lsp-inline-completion-shown-hook)))
253279

254-
(defun lsp-inline-completion-accept ()
255-
"Accepts the current suggestion"
256-
(interactive)
257-
(unless (lsp-inline-completion--overlay-visible)
258-
(error "Not showing suggestions"))
259-
260-
(lsp-inline-completion--clear-overlay)
261-
(-let* ((suggestion (elt lsp-inline-completion--items lsp-inline-completion--current))
262-
((&InlineCompletionItem? :insert-text :range? :command?) suggestion)
263-
((kind . text) (cond
264-
((lsp-markup-content? insert-text)
265-
(cons 'snippet (lsp:markup-content-value insert-text) ))
266-
(t (cons 'text insert-text))))
267-
((start . end) (when range?
268-
(-let (((&RangeToPoint :start :end) range?)) (cons start end))))
269-
(text-insert-start (or start lsp-inline-completion--start-point))
270-
text-insert-end
271-
(completion-is-substr (string-equal
272-
(buffer-substring text-insert-start lsp-inline-completion--start-point)
273-
(substring text 0 (- lsp-inline-completion--start-point text-insert-start)))))
274-
280+
(defun lsp-inline-completion--insert-sugestion (text kind start end command?)
281+
(let* ((text-insert-start (or start lsp-inline-completion--start-point))
282+
text-insert-end
283+
(completion-is-substr (string-equal
284+
(buffer-substring text-insert-start lsp-inline-completion--start-point)
285+
(substring text 0 (- lsp-inline-completion--start-point text-insert-start)))))
275286
(when text-insert-start
276287
(goto-char text-insert-start))
277288

@@ -282,6 +293,7 @@ text range that was updated by the completion"
282293

283294
;; Insert suggestion, keeping the cursor at the start point
284295
(insert text)
296+
285297
(setq text-insert-end (point))
286298

287299
;; If a template, format it -- keep track of the end position!
@@ -304,6 +316,27 @@ text range that was updated by the completion"
304316
;; hooks
305317
(run-hook-with-args-until-failure 'lsp-inline-completion-accepted-hook text text-insert-start text-insert-end)))
306318

319+
(defun lsp-inline-completion-accept ()
320+
"Accepts the current suggestion"
321+
(interactive)
322+
(unless (lsp-inline-completion--overlay-visible)
323+
(error "Not showing suggestions"))
324+
325+
(lsp-inline-completion--clear-overlay)
326+
(-let* ((suggestion (elt lsp-inline-completion--items lsp-inline-completion--current))
327+
((&InlineCompletionItem? :insert-text :range? :command?) suggestion)
328+
((kind . text) (cond
329+
((lsp-markup-content? insert-text)
330+
(cons 'snippet (lsp:markup-content-value insert-text) ))
331+
(t (cons 'text insert-text))))
332+
((start . end) (when range?
333+
(-let (((&RangeToPoint :start :end) range?)) (cons start end)))))
334+
335+
(with-no-warnings
336+
;; Compiler does not believes this macro is defined
337+
(lsp-inline-completion--with-undo-amalgamate
338+
(lsp-inline-completion--insert-sugestion text kind start end command?)))))
339+
307340
(defun lsp-inline-completion-accept-on-click (event)
308341
(interactive "e")
309342

0 commit comments

Comments
 (0)