feat(vertico): completion highlights a la ivy

* feat(vertico): completion highlights a la ivy

Adds completion highlighting that works similarly to ivy/counsel's
one (which is enabled by default). It'll highlight enabled major/minor
modes and directories in a different face. On by default.

Ref: https://github.com/minad/vertico/wiki#candidate-display-transformations-custom-candidate-highlighting

* fix(vertico): major mode not being highlighted

The major mode was not being highlighted correctly;
it should work now that the buffer is set correctly in
`+vertico-highlight-enabled-mode`.

* fix(vertico): make font lock prioritize match over type

The mode and directory highlights were [overriding the match
font-lock](https://github.com/doomemacs/doomemacs/pull/7706#issuecomment-1977722188).
This should resolve that by prioritizing the match font lock using
`'append` on `add-face-text-property` instead of `propertize`.
This commit is contained in:
Dev380 2024-03-08 20:21:36 -05:00 committed by Yann Esposito (Yogsototh)
parent 2c8921cf7d
commit bd9bf18d73
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646

View file

@ -341,3 +341,43 @@ orderless."
:hook (vertico-mode . vertico-posframe-mode) :hook (vertico-mode . vertico-posframe-mode)
:config :config
(add-hook 'doom-after-reload-hook #'posframe-delete-all)) (add-hook 'doom-after-reload-hook #'posframe-delete-all))
;; From https://github.com/minad/vertico/wiki#candidate-display-transformations-custom-candidate-highlighting
;;
;; Uses `add-face-text-property' instead of `propertize' unlike the above snippet
;; because `'append' is necessary to not override the match font lock
;; See: https://github.com/minad/vertico/issues/389
(use-package! vertico-multiform
:hook (vertico-mode . vertico-multiform-mode)
:config
(defvar +vertico-transform-functions nil)
(cl-defmethod vertico--format-candidate :around
(cand prefix suffix index start &context ((not +vertico-transform-functions) null))
(dolist (fun (ensure-list +vertico-transform-functions))
(setq cand (funcall fun cand)))
(cl-call-next-method cand prefix suffix index start))
(defun +vertico-highlight-directory (file)
"If FILE ends with a slash, highlight it as a directory."
(when (string-suffix-p "/" file)
(add-face-text-property 0 (length file) 'marginalia-file-priv-dir 'append file))
file)
(defun +vertico-highlight-enabled-mode (cmd)
"If MODE is enabled, highlight it as font-lock-constant-face."
(let ((sym (intern cmd)))
(with-current-buffer (nth 1 (buffer-list))
(if (or (eq sym major-mode)
(and
(memq sym minor-mode-list)
(boundp sym)))
(add-face-text-property 0 (length cmd) 'font-lock-constant-face 'append cmd)))
cmd))
(add-to-list 'vertico-multiform-categories
'(file
(+vertico-transform-functions . +vertico-highlight-directory)))
(add-to-list 'vertico-multiform-commands
'(execute-extended-command
(+vertico-transform-functions . +vertico-highlight-enabled-mode))))