From 0a6daa141220a99bae591ae5935a43b7710254bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Thu, 25 Dec 2025 16:40:03 +0000 Subject: [PATCH] Eglot: speed up symbol highlighting (bug#80072) In large buffers, responses to textDocument/documentHighlight requests may return thousands of occurrences. Avoid incurring expensive eglot--lsp-position-to-point calls, by restricting the highlights to visible region in the active window. * lisp/progmodes/eglot.el (eglot-highlight-eldoc-function): Rework. --- lisp/progmodes/eglot.el | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 415444d0c47..96f1c7e9820 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -3936,19 +3936,27 @@ for which LSP on-type-formatting should be requested." :success-fn (lambda (highlights) (mapc #'delete-overlay eglot--highlights) - (setq eglot--highlights - (eglot--when-buffer-window buf - (mapcar - (eglot--lambda ((DocumentHighlight) range) - (pcase-let ((`(,beg . ,end) - (eglot-range-region range))) - (let ((ov (make-overlay beg end))) - (overlay-put ov 'face 'eglot-highlight-symbol-face) - (overlay-put ov 'eglot--overlay t) - (overlay-put ov 'modification-hooks - `(,(lambda (o &rest _) (delete-overlay o)))) - ov))) - highlights)))) + (setq eglot--highlights nil) + (eglot--when-buffer-window buf + ;; Don't highlight occurrences that aren't + ;; visible. (bug#80072). + (let* ((w (car (get-buffer-window-list))) + (ws (window-start w)) (we (window-end w)) + (ls (1- (line-number-at-pos ws t))) + (le (1- (line-number-at-pos we t)))) + (mapc + (eglot--lambda ((DocumentHighlight) range) + (when-let* ((l (cl-getf (cl-getf range :start) :line)) + (_ (and (>= l ls) (<= l le)))) + (pcase-let ((`(,beg . ,end) + (eglot-range-region range))) + (let ((ov (make-overlay beg end))) + (overlay-put ov 'face 'eglot-highlight-symbol-face) + (overlay-put ov 'eglot--overlay t) + (overlay-put ov 'modification-hooks + `(,(lambda (o &rest _) (delete-overlay o)))) + (push ov eglot--highlights))))) + highlights)))) :hint :textDocument/documentHighlight) nil)))