From 0097d8d8327a783c77bed2860c4283541c6608b7 Mon Sep 17 00:00:00 2001 From: Michal Krzywkowski Date: Wed, 21 Nov 2018 15:54:22 +0100 Subject: [PATCH] Properly delete inserted text after completion * eglot.el (eglot-completion-at-point): In :exit-function, delete only the region of buffer that was inserted by completion. GitHub-reference: fix https://github.com/joaotavora/eglot/issues/160 --- lisp/progmodes/eglot.el | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index d9c1c3ab97e..c65d9089784 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -1543,6 +1543,7 @@ is not active." (string-trim-left label)) (t (or insertText (string-trim-left label)))))) + (setq all (append all `(:bounds ,bounds))) (add-text-properties 0 1 all completion) (put-text-property 0 1 'eglot--lsp-completion all completion) completion)) @@ -1604,13 +1605,19 @@ is not active." insertText textEdit additionalTextEdits + bounds &allow-other-keys) (text-properties-at 0 comp) (let ((fn (and (eql insertTextFormat 2) (eglot--snippet-expansion-fn)))) (when (or fn textEdit) - ;; Undo the completion - (delete-region (- (point) (length comp)) (point))) + ;; Undo the completion. If before completion the buffer was + ;; "foo.b" and now is "foo.bar", `comp' will be "bar". We + ;; want to delete only "ar" (`comp' minus the symbol whose + ;; bounds we've calculated before) (github#160). + (delete-region (+ (- (point) (length comp)) + (if bounds (- (cdr bounds) (car bounds)) 0)) + (point))) (cond (textEdit (cl-destructuring-bind (&key range newText) textEdit (pcase-let ((`(,beg . ,end) (eglot--range-region range))) -- 2.39.2