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)))