From 4d3cf3330671a3df4433d1f35efda779ebb72460 Mon Sep 17 00:00:00 2001 From: Trevor Murphy Date: Fri, 24 Apr 2020 11:52:01 -0700 Subject: [PATCH] Create match xrefs when possible "Match xrefs" are created with `xref-make-match' instead of `xref-make'. Match xrefs support `xref-query-replace-in-results' from the results buffer. * eglot.el (eglot--xref-make-match): Calculate xref match length from the eglot range. GitHub-reference: close https://github.com/joaotavora/eglot/issues/435 --- lisp/progmodes/eglot.el | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 3ea839115c1..c7c45518125 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -1833,8 +1833,8 @@ Calls REPORT-FN maybe if server publishes diagnostics in time." (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. +(defun eglot--xref-make-match (name uri range) + "Like `xref-make-match' but with LSP's NAME, URI and RANGE. Try to visit the target file for a richer summary line." (pcase-let* ((file (eglot--uri-to-path uri)) @@ -1849,8 +1849,9 @@ Try to visit the target file for a richer summary line." (hi-end (- (min (point-at-eol) end) bol))) (add-face-text-property hi-beg hi-end 'highlight t substring) - (list substring (1+ (current-line)) (eglot-current-column)))))) - (`(,summary ,line ,column) + (list substring (1+ (current-line)) (eglot-current-column) + (- end beg)))))) + (`(,summary ,line ,column ,length) (cond (visiting (with-current-buffer visiting (funcall collect))) ((file-readable-p file) (with-current-buffer @@ -1859,9 +1860,12 @@ Try to visit the target file for a richer summary line." (insert-file-contents file) (funcall collect))) (t ;; fall back to the "dumb strategy" - (let ((start (cl-getf range :start))) - (list name (1+ (cl-getf start :line)) (cl-getf start :character))))))) - (xref-make summary (xref-make-file-location file line column)))) + (let* ((start (cl-getf range :start)) + (line (1+ (cl-getf start :line))) + (start-pos (cl-getf start :character)) + (end-pos (cl-getf (cl-getf range :end) :character))) + (list name line start-pos (- end-pos start-pos))))))) + (xref-make-match summary (xref-make-file-location file line column) length))) (cl-defmethod xref-backend-identifier-completion-table ((_backend (eql eglot))) (eglot--error "cannot (yet) provide reliable completion table for LSP symbols")) @@ -1892,7 +1896,7 @@ Try to visit the target file for a richer summary line." (eglot--collecting-xrefs (collect) (mapc (eglot--lambda ((Location) uri range) - (collect (eglot--xref-make (symbol-at-point) uri range))) + (collect (eglot--xref-make-match (symbol-at-point) uri range))) (if (vectorp response) response (list response)))))) (cl-defun eglot--lsp-xref-helper (method &key extra-params capability ) @@ -1935,7 +1939,7 @@ Try to visit the target file for a richer summary line." (mapc (eglot--lambda ((SymbolInformation) name location) (eglot--dbind ((Location) uri range) location - (collect (eglot--xref-make name uri range)))) + (collect (eglot--xref-make-match name uri range)))) (jsonrpc-request (eglot--current-server-or-lose) :workspace/symbol `(:query ,pattern)))))) -- 2.39.2