diff --git a/eglot.el b/eglot.el index b7910be3..c12a3eb6 100644 --- a/eglot.el +++ b/eglot.el @@ -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. @@ -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 @@ -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))))))