]> git.eshelyaron.com Git - emacs.git/commitdiff
Eglot: display completion label when safe
authorJoão Távora <joaotavora@gmail.com>
Thu, 23 Feb 2023 13:58:38 +0000 (13:58 +0000)
committerJoão Távora <joaotavora@gmail.com>
Thu, 23 Feb 2023 13:58:54 +0000 (13:58 +0000)
Originally reported in
https://github.com/joaotavora/eglot/discussions/1141 by "Mintsoup".

Eglot doesn't always show the LSP :label property of a CompletionItem
in the completion candidates.  That is because label is sometimes not
what should be inserted in the buffer in the end, the :insertText
property supercedes it.

But the label is usually more suitable for display nevertheless and if
the LSP CompletionItem contains either a snippet or a textEdit, it's
safe to display the label, since :exit-function will guarantee that a
suitable buffer insertion is performed.

This change reflects that awareness that when a textEdit is available,
it's acceptable to show the label.

* lisp/progmodes/eglot.el (eglot-completion-at-point): Adjust.

lisp/progmodes/eglot.el

index df755dfa43a1516a76376a9c6edfb9696989a39f..7d61f6ad78ed28eefe21d4e5b86c2849baa15495 100644 (file)
@@ -2824,16 +2824,20 @@ for which LSP on-type-formatting should be requested."
                       (mapcar
                        (jsonrpc-lambda
                            (&rest item &key label insertText insertTextFormat
-                                  &allow-other-keys)
+                                  textEdit &allow-other-keys)
                          (let ((proxy
-                                (cond ((and (eql insertTextFormat 2)
-                                            (eglot--snippet-expansion-fn))
+                                ;; Snippet or textEdit, it's safe to
+                                ;; display/insert the label since
+                                ;; it'll be adjusted.  If no usable
+                                ;; insertText at all, label is best,
+                                ;; too.
+                                (cond ((or (and (eql insertTextFormat 2)
+                                                (eglot--snippet-expansion-fn))
+                                           textEdit
+                                           (null insertText)
+                                           (string-empty-p insertText))
                                        (string-trim-left label))
-                                      ((and insertText
-                                            (not (string-empty-p insertText)))
-                                       insertText)
-                                      (t
-                                       (string-trim-left label)))))
+                                      (t insertText))))
                            (unless (zerop (length proxy))
                              (put-text-property 0 1 'eglot--lsp-item item proxy))
                            proxy))