Skip to content

Commit

Permalink
Recreate pattern for highlighting in the cached case
Browse files Browse the repository at this point in the history
  • Loading branch information
jojojames committed Dec 2, 2024
1 parent 69a5c3c commit dd66f96
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,17 @@ To use [[https://github.com/oantolin/orderless][orderless]] filtering:
:commands (orderless-filter))

(setq fussy-filter-fn 'fussy-filter-orderless)

;; Highlight matches with `company-mode'.
(with-eval-after-load 'orderless
;; https://www.reddit.com/r/emacs/comments/nichkl/how_to_use_different_completion_styles_in_the/
;; https://github.com/oantolin/orderless#company
(defun orderless-just-one-face (fn &rest args)
(let ((orderless-match-faces [completions-common-part]))
(ignore orderless-match-faces)
(apply fn args)))
(advice-add 'company-capf--candidates
:around #'orderless-just-one-face))
#+end_src
* Caching
Results and filtering can be cached for improved performance by setting
Expand Down
46 changes: 44 additions & 2 deletions fussy.el
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,11 @@ Implement `all-completions' interface with additional fuzzy / `flx' scoring."
;; (message "using cache for filter")
(list
cached-all
(fussy-make-pcm-highlight-pattern
beforepoint afterpoint bounds)
(if (fussy--orderless-p)
(fussy--recreate-orderless-pattern
string table pred point)
(fussy--recreate-regex-pattern
beforepoint afterpoint bounds))
prefix))
(funcall fussy-filter-fn
string table pred point))))
Expand Down Expand Up @@ -917,6 +920,45 @@ Check C1 and C2 in `minibuffer-history-variable' which is stored in
;; (@* "Utils" )
;;

(defun fussy--recreate-orderless-pattern (string table pred _point)
"See `fussy--recreate-regex-pattern'."
;; This implementation from `orderless-all-completions'.
(if (fboundp 'orderless--compile)
(pcase-let
((`(,_prefix ,regexps ,_ignore-case ,_pred)
(if (eq fussy-filter-fn 'fussy-filter-orderless-flex)
(let ((orderless-matching-styles '(orderless-flex)))
(ignore orderless-matching-styles)
(orderless--compile string table pred))
(orderless--compile string table pred))))
regexps)
nil))

(defun fussy--recreate-regex-pattern (beforepoint afterpoint bounds)
"Utility function to create regex pattern for highlighting.
`fussy--highlight-collection' consumes this pattern.
This usually comes out as a result of the initial filtering of candidates,
but when we're pulling from the cache, the pattern is not there, so we
rebuild it here. We could also try caching the pattern instead of creating it
again."
(cond
((eq fussy-filter-fn 'fussy-filter-flex)
;; This comes from `completion-substring--all-completions'
;; Look at `fussy-filter-flex'.
(let* ((basic-pattern (completion-basic--pattern
beforepoint afterpoint bounds))
(pattern (if (not (stringp (car basic-pattern)))
basic-pattern
(cons 'prefix basic-pattern)))
(pattern
(completion-pcm--optimize-pattern
(completion-flex--make-flex-pattern pattern))))
pattern))
(:default ;; `fussy-filter-default'
(fussy-make-pcm-highlight-pattern
beforepoint afterpoint bounds))))

(defun fussy--orderless-p ()
"Return whether or not we're using `orderless' for filtering."
(or (eq fussy-filter-fn 'fussy-filter-orderless)
Expand Down

0 comments on commit dd66f96

Please sign in to comment.