Skip to content

Commit

Permalink
Replace eglot--handling-xrefs with eglot--collecting-xrefs
Browse files Browse the repository at this point in the history
Slightly more idiomatic like this, i.e. less danger of messing up.

* eglot.el (eglot--collecting-xrefs): Rework eglot--handling-xrefs.
(eglot--handling-xrefs): Remove.
(xref-backend-apropos, eglot--lsp-xrefs-for-method): Use eglot--collecting-xrefs.
  • Loading branch information
joaotavora committed Oct 9, 2019
1 parent de7f2bc commit aba7cc5
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -1713,12 +1713,16 @@ Calls REPORT-FN maybe if server publishes diagnostics in time."
(defvar eglot-xref-lessp-function #'ignore
"Compare two `xref-item' objects for sorting.")

(defmacro eglot--handling-xrefs (&rest body)
"Properly sort and handle xrefs produced and returned by BODY."
`(unwind-protect
(sort (progn ,@body) eglot-xref-lessp-function)
(maphash (lambda (_uri buf) (kill-buffer buf)) eglot--temp-location-buffers)
(clrhash eglot--temp-location-buffers)))
(cl-defmacro eglot--collecting-xrefs ((collector) &rest body)
"Sort and handle xrefs collected with COLLECTOR in BODY."
(let ((collected (cl-gensym "collected")))
`(unwind-protect
(let (,collected)
(cl-flet ((,collector (xref) (push xref ,collected)))
,@body)
(sort ,collected eglot-xref-lessp-function))
(maphash (lambda (_uri buf) (kill-buffer buf)) eglot--temp-location-buffers)
(clrhash eglot--temp-location-buffers))))

(defun eglot--xref-make (name uri range)
"Like `xref-make' but with LSP's NAME, URI and RANGE.
Expand Down Expand Up @@ -1772,8 +1776,8 @@ Try to visit the target file for a richer summary line."
(cadr (split-string (symbol-name method)
"/"))))))
(eglot--error "Sorry, this server doesn't do %s" method))
(eglot--handling-xrefs
(mapcar
(eglot--collecting-xrefs (collect)
(mapc
(eglot--lambda ((Location) uri range)
(eglot--xref-make (symbol-at-point) uri range))
(jsonrpc-request
Expand Down Expand Up @@ -1815,11 +1819,11 @@ Try to visit the target file for a richer summary line."

(cl-defmethod xref-backend-apropos ((_backend (eql eglot)) pattern)
(when (eglot--server-capable :workspaceSymbolProvider)
(eglot--handling-xrefs
(mapcar
(eglot--collecting-xrefs (collect)
(mapc
(eglot--lambda ((SymbolInformation) name location)
(eglot--dbind ((Location) uri range) location
(eglot--xref-make name uri range)))
(collect (eglot--xref-make name uri range))))
(jsonrpc-request (eglot--current-server-or-lose)
:workspace/symbol
`(:query ,pattern))))))
Expand Down

0 comments on commit aba7cc5

Please sign in to comment.