branch: externals/vertico
commit be6d80c6628d592fee47d2aa4fa3f57fd7ecacd9
Author: Daniel Mendler <m...@daniel-mendler.de>
Commit: Daniel Mendler <m...@daniel-mendler.de>

    Improve vertico--format-candidates, such that start argument is correct
---
 vertico.el | 51 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 21 deletions(-)

diff --git a/vertico.el b/vertico.el
index 205fce5..3816bb9 100644
--- a/vertico.el
+++ b/vertico.el
@@ -231,7 +231,7 @@ See `resize-mini-windows' for documentation."
                               suffix
                             (propertize suffix 'face 
'completions-annotations)))))
                 candidates)
-      candidates)))
+      (mapcar (lambda (cand) (list cand "" "")) candidates))))
 
 (defun vertico--move-to-front (elem list)
   "Move ELEM to front of LIST."
@@ -439,34 +439,43 @@ See `resize-mini-windows' for documentation."
          (group-format (and group-fun vertico-group-format (concat 
vertico-group-format "\n")))
          (start (min (max 0 (- vertico--index (/ vertico-count 2) (if 
group-format -1 0)))
                      (max 0 (- vertico--total vertico-count))))
-         (index start)
          (candidates
-          (thread-last (seq-subseq vertico--candidates index
-                                   (min (+ index vertico-count) 
vertico--total))
+          (thread-last (seq-subseq vertico--candidates start
+                                   (min (+ start vertico-count) 
vertico--total))
             (funcall vertico--highlight)
             (vertico--affixate metadata)))
-         (max-width (- (window-width) 4))
-         (curr-line 0) (title) (lines))
-    (dolist (cand candidates)
-      (let ((prefix "") (suffix ""))
-        (when (consp cand)
-          (setq prefix (cadr cand) suffix (caddr cand) cand (car cand)))
-        (when-let (new-title (and group-format (funcall group-fun cand nil)))
+         (curr-line 0) (lines))
+    ;; Compute group titles
+    (let ((index start) (title))
+      (dolist (cand candidates)
+        (when-let (new-title (and group-format (funcall group-fun (car cand) 
nil)))
           (unless (equal title new-title)
             (push (format group-format (setq title new-title)) lines))
-          (setq cand (funcall group-fun cand 'transform)))
+          (setcar cand (funcall group-fun (car cand) 'transform)))
         (when (= index vertico--index)
           (setq curr-line (length lines)))
-        (when (string-match-p "\n" cand)
-          (setq cand (vertico--truncate-multiline cand max-width)))
-        (push (vertico--format-candidate cand prefix suffix index start) lines)
+        (push (cons index cand) lines)
         (setq index (1+ index))))
-    (setq lines (nreverse lines) index (length lines))
-    (while (> index vertico-count)
-      (if (< curr-line (/ index 2))
-          (nbutlast lines)
-        (setq curr-line (1- curr-line) lines (cdr lines)))
-      (setq index (1- index)))
+    ;; Drop excess lines
+    (setq lines (nreverse lines))
+    (let ((count (length lines)))
+      (while (> count vertico-count)
+        (if (< curr-line (/ count 2))
+            (nbutlast lines)
+          (setq curr-line (1- curr-line) lines (cdr lines)))
+        (setq count (1- count))))
+    ;; Format candidates
+    (let ((max-width (- (window-width) 4))
+          (line lines) (start))
+      (while line
+        (pcase (car line)
+          (`(,index ,cand ,prefix ,suffix)
+           (setq start (or start index))
+           (when (string-match-p "\n" cand)
+             (setq cand (vertico--truncate-multiline cand max-width)))
+           (setcar line (vertico--format-candidate cand prefix suffix index 
start))
+           (setq index (1+ index))))
+        (pop line)))
     lines))
 
 (defun vertico--display-candidates (lines)

Reply via email to