Skip to content

Commit

Permalink
Actually make completion sorting work
Browse files Browse the repository at this point in the history
* eglot.el (eglot-completion-at-point): Complicate severely.

GitHub-reference: fix joaotavora/eglot#190
  • Loading branch information
joaotavora committed Dec 23, 2018
1 parent f5e3279 commit 9ffa6a9
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions lisp/progmodes/eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 9ffa6a9

Please sign in to comment.