]> git.eshelyaron.com Git - emacs.git/commitdiff
Actually make completion sorting work
authorJoão Távora <joaotavora@gmail.com>
Sat, 22 Dec 2018 15:23:41 +0000 (15:23 +0000)
committerJoão Távora <joaotavora@gmail.com>
Sun, 23 Dec 2018 13:53:18 +0000 (13:53 +0000)
* eglot.el (eglot-completion-at-point): Complicate severely.

GitHub-reference: fix https://github.com/joaotavora/eglot/issues/190

lisp/progmodes/eglot.el

index 5861b05fcdf4aa5d24a63aef77f976783da68342..d60fc0776a8118833747ba6adf8626694df42a76 100644 (file)
@@ -1806,37 +1806,49 @@ is not active."
 
 (defun eglot-completion-at-point ()
   "EGLOT's `completion-at-point' function."
-  (let ((bounds (bounds-of-thing-at-point 'symbol))
+  (let* ((bounds (bounds-of-thing-at-point 'symbol))
         (server (eglot--current-server-or-lose))
         (completion-capability (eglot--server-capable :completionProvider))
+        (sort-completions (lambda (completions)
+                            (sort completions
+                                  (lambda (a b)
+                                    (string-lessp
+                                     (or (get-text-property 0 :sortText a) "")
+                                     (or (get-text-property 0 :sortText b) ""))))))
+        (metadata `(metadata . ((display-sort-function . ,sort-completions))))
         strings)
     (when completion-capability
       (list
        (or (car bounds) (point))
        (or (cdr bounds) (point))
-       (completion-table-dynamic
-        (lambda (_ignored)
-          (let* ((resp (jsonrpc-request server
-                                        :textDocument/completion
-                                        (eglot--CompletionParams)
-                                        :deferred :textDocument/completion
-                                        :cancel-on-input t))
-                 (items (if (vectorp resp) resp (plist-get resp :items))))
-            (setq
-             strings
-             (mapcar
-              (jsonrpc-lambda (&rest all &key label insertText insertTextFormat
-                                     &allow-other-keys)
-                (let ((completion
-                       (cond ((and (eql insertTextFormat 2)
-                                   (eglot--snippet-expansion-fn))
-                              (string-trim-left label))
-                             (t
-                              (or insertText (string-trim-left label))))))
-                  (add-text-properties 0 1 all completion)
-                  (put-text-property 0 1 'eglot--lsp-completion all completion)
-                  completion))
-              items)))))
+       (lambda (string pred action)
+         (if (eq action 'metadata) metadata
+           (funcall
+            (completion-table-dynamic
+             (lambda (_ignored)
+               (let* ((resp (jsonrpc-request server
+                                             :textDocument/completion
+                                             (eglot--CompletionParams)
+                                             :deferred :textDocument/completion
+                                             :cancel-on-input t))
+                      (items (if (vectorp resp) resp (plist-get resp :items))))
+                 (setq
+                  strings
+                  (mapcar
+                   (jsonrpc-lambda
+                       (&rest all &key label insertText insertTextFormat
+                              &allow-other-keys)
+                     (let ((completion
+                            (cond ((and (eql insertTextFormat 2)
+                                        (eglot--snippet-expansion-fn))
+                                   (string-trim-left label))
+                                  (t
+                                   (or insertText (string-trim-left label))))))
+                       (add-text-properties 0 1 all completion)
+                       (put-text-property 0 1 'eglot--lsp-completion all completion)
+                       completion))
+                   items)))))
+            string pred action)))
        :annotation-function
        (lambda (obj)
          (eglot--dbind ((CompletionItem) detail kind insertTextFormat)
@@ -1854,12 +1866,6 @@ is not active."
                        (and (eql insertTextFormat 2)
                             (eglot--snippet-expansion-fn)
                             " (snippet)"))))))
-       :display-sort-function
-       (lambda (items)
-         (sort items (lambda (a b)
-                       (string-lessp
-                        (or (get-text-property 0 :sortText a) "")
-                        (or (get-text-property 0 :sortText b) "")))))
        :company-doc-buffer
        (lambda (obj)
          (let* ((documentation