feat(editor): add project-buffer marginalia annotator

This is useful for annotating completion-read buffer results which all
belong to a single project, as it removes the project root from the
file path field, showing a relative path to each buffer's file from the
project root.
This commit is contained in:
2021-10-06 01:33:25 +01:00
parent 9661c1e317
commit f4e1c403b0

View File

@@ -7,25 +7,47 @@
;;; Code:
(use-package marginalia
:demand t
:bind (:map minibuffer-local-map
("M-A" . marginalia-cycle))
:init
(marginalia-mode +1)
(defun siren-marginalia-setup ()
(interactive)
(mapc
(lambda (x)
(pcase (car x)
;; Default command category to 'marginalia-annotate-binding instead of
;; 'marginalia-annotate-command which has a slight performance impact
;; when filtering M-x candidates.
('command (setcdr x (cons 'marginalia-annotate-binding
(remq 'marginalia-annotate-binding (cdr x)))))))
marginalia-annotator-registry))
:init
(defun marginalia-annotate-project-buffer (cand)
"Annotate project buffer CAND with modification status, file name and major
mode."
(when-let (buffer (get-buffer cand))
(marginalia--fields
((marginalia--buffer-status buffer))
((marginalia--project-buffer-file buffer)
:truncate (/ marginalia-truncate-width 2)
:face 'marginalia-file-name))))
(defun marginalia--project-buffer-file (buffer)
"Return the file or process name of BUFFER relative to project root, if it
is within project root."
(let ((root (marginalia--project-root))
(file (marginalia--buffer-file buffer)))
(if (string-equal root file) file
(string-remove-prefix root file))))
:config
(siren-marginalia-setup))
;; Default command category to 'marginalia-annotate-binding instead of
;; 'marginalia-annotate-command which has a slight performance impact when
;; filtering M-x candidates.
(mapc
(lambda (x)
(pcase (car x)
('command
(setcdr x (cons 'marginalia-annotate-binding
(remq 'marginalia-annotate-binding (cdr x)))))))
marginalia-annotator-registry)
;; Add project-buffer annotator.
(add-to-list 'marginalia-annotator-registry
'(project-buffer marginalia-annotate-project-buffer))
;; Enable marginalia.
(marginalia-mode +1))
(provide 'siren-marginalia)
;;; siren-marginalia.el ends here