]> git.eshelyaron.com Git - emacs.git/commitdiff
Properly delete inserted text after completion
authorMichal Krzywkowski <k.michal@zoho.com>
Wed, 21 Nov 2018 14:54:22 +0000 (15:54 +0100)
committerMichal Krzywkowski <k.michal@zoho.com>
Wed, 21 Nov 2018 19:24:16 +0000 (20:24 +0100)
* 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

index d9c1c3ab97e531e4c08453b69e31b3c7162d198f..c65d908978421ea7217369946615e519183dcf65 100644 (file)
@@ -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)))