fix(editor/fussy): improve completion performance and simplify setup

The fussy package now comes with helper functions to strip down an input
string, removing all extra "tofu" characters added by Consult. This is
also faster than the old hand-rolled solution I had come up with
previously.

Hence I've switch to simpler variant that uses a custom scoring function
that calls `flx-rs-score`, with `str` having been sanitized with the
function assigned to `fussy-remove-bar-char-fn`, which by default is
`fussy-without-tofu-char`.

I've also submitted a PR to fussy that will cleanup the input string
when using flx-rs:

https://github.com/jojojames/fussy/pull/36
This commit is contained in:
2023-11-20 19:26:47 +00:00
parent 862b7fc26e
commit e74a3bd3ad

View File

@@ -6,32 +6,6 @@
;;; Code:
(use-package flx-rs
:straight (flx-rs :repo "jcs-elpa/flx-rs" :fetcher github
:files (:defaults "bin"))
:config
(with-eval-after-load 'consult
(defun siren-flx-rs-score-consult-fix (orig-fun str query &rest args)
"Fix input string provided by consult to be compatible with flx-rs.
Some commands that use consult seem to add metadata to candidates
in the form of some extra bytes at the end of the string. These
extra bytes causes flx-rs to not match against it correctly.
Consult uses the `invisible' text property to mark these extra
bytes so that they are not displayed to the user in completion
frameworks.
This function attempts to extract the original input string by
looking for where the `invisible' text property begins, and
grabbing all text before it."
(let ((end (next-single-property-change 0 'invisible str)))
(apply orig-fun (if end (substring str 0 end) str) query args)))
(advice-add 'flx-rs-score :around 'siren-flx-rs-score-consult-fix))
(flx-rs-load-dyn))
(use-package fussy
:demand t
:custom
@@ -41,7 +15,7 @@ grabbing all text before it."
(fussy-ignore-case t)
(fussy-filter-fn 'fussy-filter-default)
(fussy-score-fn 'flx-rs-score)
(fussy-remove-bad-char-fn #'fussy-without-tofu-char)
:preface
(defun siren-fussy--company-transform-advice (f &rest args)
@@ -55,7 +29,23 @@ grabbing all text before it."
:around 'siren-fussy--company-transform-advice)
(setq completion-category-defaults nil))
(use-package flx-rs
:straight (flx-rs :repo "jcs-elpa/flx-rs" :fetcher github
:files (:defaults "bin"))
:custom
(fussy-score-fn #'siren-flx-rs-score)
:preface
(defun siren-flx-rs-score (str query &rest args)
"Score STR for QUERY using `flx-rs-score'.
This will no longer be needed when used with fussy after this PR
is merged: https://github.com/jojojames/fussy/pull/36"
(flx-rs-score (funcall fussy-remove-bad-char-fn str) query args))
:config
(flx-rs-load-dyn))
(provide 'siren-fussy)
;;; siren-fussy.el ends here