From d657be1744a1481dc4646d0b62d5ee1d3e75d1d8 Mon Sep 17 00:00:00 2001 From: Dev380 <49997896+Dev380@users.noreply.github.com> Date: Fri, 8 Mar 2024 20:21:36 -0500 Subject: [PATCH] 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`. --- modules/completion/vertico/config.el | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/modules/completion/vertico/config.el b/modules/completion/vertico/config.el index 44d853b80..89f30a5c1 100644 --- a/modules/completion/vertico/config.el +++ b/modules/completion/vertico/config.el @@ -341,3 +341,43 @@ orderless." :hook (vertico-mode . vertico-posframe-mode) :config (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))))