33
33
(require 'cl-lib )
34
34
(require 'fringe )
35
35
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
+
36
62
(defun lsp-inline-completion--params (implicit &optional identifier position )
37
63
" Returns a InlineCompletionParams instance"
38
64
(lsp-make-inline-completion-params
@@ -251,27 +277,12 @@ text range that was updated by the completion"
251
277
(lsp-inline-completion--show-keys)
252
278
(run-hooks 'lsp-inline-completion-shown-hook )))
253
279
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)))))
275
286
(when text-insert-start
276
287
(goto-char text-insert-start))
277
288
@@ -282,6 +293,7 @@ text range that was updated by the completion"
282
293
283
294
; ; Insert suggestion, keeping the cursor at the start point
284
295
(insert text)
296
+
285
297
(setq text-insert-end (point ))
286
298
287
299
; ; If a template, format it -- keep track of the end position!
@@ -304,6 +316,27 @@ text range that was updated by the completion"
304
316
; ; hooks
305
317
(run-hook-with-args-until-failure 'lsp-inline-completion-accepted-hook text text-insert-start text-insert-end)))
306
318
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
+
307
340
(defun lsp-inline-completion-accept-on-click (event )
308
341
(interactive " e" )
309
342
0 commit comments