Subject: bug#46859: 28.0.50; [PATCH]: Add option to truncate long lines in xref.el

From: Dmitry Gutov <[email-masked]>
Date: Wed, 10 Mar 2021 04:06:11 +0200

On 08.03.2021 04:56, Dmitry Gutov wrote:
> If you still get lines that are loo long in these cases, even with all 
> extra text snipped away, hiding parts of the summary using text 
> properties should be possible. I just tried putting 'invisible' on the 
> whole line after column 600, and scrolling became instantaneous again.
> 
> As long as we undo these properties (or, perhaps, scroll the visible 
> part?) when xref-next-line is called, the user would still be able to 
> visit all matches.

Here's an experimental patch that does this.

As long as there are no matches near the end of the long string, 
everything seems snappy. If we do end up scrolling to near its end, 
though, moving around that line becomes slower.
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index c066d9dc02..8decfcbb6c 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -891,6 +891,39 @@ xref--mouse-2
       (xref--search-property 'xref-item))
   (xref-show-location-at-point))
 
+(defcustom xref-truncate-to-width 400
+  "The column to visually \"truncate\" an Xref buffer line."
+  :type 'integer)
+
+(defun xref--apply-truncation ()
+  (add-to-invisibility-spec '(ellipsis . t))
+  (let (;; FIXME: Treat the line number prefix specially.
+        (bol (line-beginning-position))
+        (eol (line-end-position))
+        (inhibit-read-only t)
+        pos)
+    (when (and (> (- eol bol)
+                  xref-truncate-to-width)
+               ;; Either truncation not applied yet, or it hides the current
+               ;; position: need to refresh.
+               (or (and (null (get-text-property (1- eol) 'invisible))
+                        (null (get-text-property bol 'invisible)))
+                   (get-text-property (point) 'invisible)))
+      (cond
+       ((< (- (point) bol) xref-truncate-to-width)
+        (setq pos (+ bol xref-truncate-to-width))
+        (remove-text-properties bol pos '(invisible))
+        (put-text-property pos eol 'invisible 'ellipsis))
+       ((< (- eol (point)) xref-truncate-to-width)
+        (setq pos (- eol xref-truncate-to-width))
+        (remove-text-properties pos eol '(invisible))
+        (put-text-property bol pos 'invisible 'ellipsis))
+       (t
+        (setq pos (- (point) (/ xref-truncate-to-width 2)))
+        (put-text-property bol pos 'invisible 'ellipsis)
+        (remove-text-properties pos (+ pos xref-truncate-to-width) '(invisible))
+        (put-text-property (+ pos xref-truncate-to-width) eol 'invisible 'ellipsis))))))
+
 (defun xref--insert-xrefs (xref-alist)
   "Insert XREF-ALIST in the current-buffer.
 XREF-ALIST is of the form ((GROUP . (XREF ...)) ...), where
@@ -934,6 +967,11 @@ xref--insert-xrefs
                         (setq prev-line line
                               prev-group group))))
            (insert "\n"))
+  (save-excursion
+    (goto-char (point-min))
+    (while (not (eobp))
+      (forward-line 1)
+      (xref--apply-truncation)))
   (run-hooks 'xref-after-update-hook))
 
 (defun xref--analyze (xrefs)
@@ -971,6 +1009,7 @@ xref--show-common-initialize
         (buffer-undo-list t))
     (erase-buffer)
     (xref--insert-xrefs xref-alist)
+    (add-hook 'post-command-hook 'xref--apply-truncation nil t)
     (goto-char (point-min))
     (setq xref--original-window (assoc-default 'window alist)
           xref--original-window-intent (assoc-default 'display-action alist))