Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions consult-imenu.el
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,45 @@ ARGS are the arguments to the special item function."
(list pos #'consult-imenu--switch-buffer (current-buffer)
imenu-default-goto-function)))))

(defun consult-imenu--decorate (name prefix face kind)
"Return imenu NAME decorated with PREFIX, FACE and KIND metadata."
(let ((key (concat (if prefix (concat prefix " " name) name))))
(when (and prefix face)
(add-face-text-property (1+ (length prefix)) (length key)
face 'append key))
(when kind
(add-face-text-property (if prefix (1+ (length prefix)) 0) (length key)
(nth 2 kind) 'append key)
(setq key (concat (car kind) " " key))
(put-text-property 0 (length (car kind)) 'consult--type (nth 1 kind) key))
key))

(defun consult-imenu--flatten (prefix face list types)
"Flatten imenu LIST.
PREFIX is prepended in front of all items.
FACE is the item face.
TYPES is the mode-specific types configuration."
(mapcan
(lambda (item)
(if (imenu--subalist-p item)
(let* ((name (concat (car item)))
(next-prefix name)
(next-face face))
(add-face-text-property 0 (length name)
'consult-imenu-prefix 'append name)
(if prefix
(setq next-prefix (concat prefix "/" name))
(when-let* ((type (cdr (assoc name types))))
(put-text-property 0 (length name) 'consult--type (car type) name)
(setq next-face (cadr type))))
(consult-imenu--flatten next-prefix next-face (cdr item) types))
(list (cons
(if prefix
(let ((key (concat prefix " " (car item))))
(add-face-text-property (1+ (length prefix)) (length key)
face 'append key)
key)
(car item))
(consult-imenu--normalize (cdr item))))))
(let* ((name (concat (car item)))
(kind (assoc (get-text-property 0 'imenu-kind name) types))
(key (consult-imenu--decorate name prefix face kind)))
(if (imenu--subalist-p item)
(let* ((next-prefix name)
(next-face face)
(region (get-text-property 0 'imenu-region name)))
(add-face-text-property 0 (length name)
'consult-imenu-prefix 'append name)
(if prefix
(setq next-prefix (concat prefix "/" name))
(when-let* ((type (cdr (assoc name types))))
(put-text-property 0 (length name) 'consult--type (car type) name)
(setq next-face (cadr type))))
(nconc
(and region
(list (cons key (consult-imenu--normalize (car region)))))
(consult-imenu--flatten next-prefix next-face (cdr item) types)))
(list (cons key (consult-imenu--normalize (cdr item)))))))
list))

(defun consult-imenu--compute ()
Expand Down