Skip to content

Commit

Permalink
Add option to sort fully matched candidates first. (#95)
Browse files Browse the repository at this point in the history
* Add option to sort fully matched candidates first.

* Mention `prescient-sort-full-matches-first` in README.

* Refactor a bit for clarity, use 'length'

Co-authored-by: okamsn <[email protected]>
Co-authored-by: Radon Rosborough <[email protected]>
  • Loading branch information
3 people authored Feb 22, 2021
1 parent e271321 commit 44be76c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,19 @@ The format is based on [Keep a Changelog].
`selectrum-secondary-highlight` are now a part of
`selectrum-prescient.el` rather than Selectrum proper ([#94])

* The user option `prescient-sort-full-matches-first` was added. If
non-nil, candidates that are fully matched are sorted before
partially matched candidates, though all candidates still follow the
order of recency, frequency, and length. See [#95].

[#66]: https://github.com/raxod502/prescient.el/pull/66
[#67]: https://github.com/raxod502/prescient.el/pull/67
[#70]: https://github.com/raxod502/prescient.el/pull/70
[#72]: https://github.com/raxod502/prescient.el/pull/72
[#76]: https://github.com/raxod502/prescient.el/pull/76
[#77]: https://github.com/raxod502/prescient.el/pull/77
[#94]: https://github.com/raxod502/prescient.el/pull/94
[#95]: https://github.com/raxod502/prescient.el/pull/95

## 5.0 (release 2020-07-16)
### Breaking changes
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ different one by customizing `prescient-filter-method`.
to define your own custom filter methods, and use them by adding the
appropriate symbol to `prescient-filter-method`.

* `prescient-sort-full-matches-first`: Whether `prescient.el` sorts
candidates that are fully matched before candidates that are
partially matched.

### Company-specific

* `company-prescient-sort-length-enable`: By default, the standard
Expand Down
34 changes: 26 additions & 8 deletions prescient.el
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ after the cache data is updated by `prescient-remember' when
`prescient-persist-mode' is activated."
:type 'boolean)

(defcustom prescient-sort-full-matches-first nil
"Whether to sort fully matched candidates before others.
Prescient can sort by recency, frequency, and candidate length.
With this option, fully matched candidates will be sorted before
partially matched candidates, but candidates in each group will
still be sorted like normal."
:type 'boolean)

;;;; Caches

(defvar prescient--history (make-hash-table :test 'equal)
Expand Down Expand Up @@ -525,18 +534,27 @@ must match each subquery, either using substring or initialism
matching. Discard any that do not, and return the resulting list.
Do not modify CANDIDATES; always make a new copy of the list."
(let ((regexps (prescient-filter-regexps query))
(results nil))
(results nil)
(prioritized-results nil))
(save-match-data
;; Use named block in case somebody loads `cl' accidentally
;; which causes `dolist' to turn into `cl-dolist' which creates
;; a nil block implicitly.
;; which causes `dolist' to turn into `cl-dolist' which
;; creates a nil block implicitly.
(dolist (candidate candidates)
(cl-block done
(dolist (regexp regexps)
(unless (string-match regexp candidate)
(cl-return-from done)))
(push candidate results)))
(nreverse results))))
(let ((fully-matched nil))
(dolist (regexp regexps)
(unless (string-match regexp candidate)
(cl-return-from done))
(when (and
prescient-sort-full-matches-first
(equal (length candidate)
(length (match-string 0 candidate))))
(setq fully-matched t)))
(if fully-matched
(push candidate prioritized-results)
(push candidate results)))))
(nconc (nreverse prioritized-results) (nreverse results)))))

(defmacro prescient--sort-compare ()
"Hack used to cause the byte-compiler to produce faster code.
Expand Down

0 comments on commit 44be76c

Please sign in to comment.