[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master dc693c3 280/348: Make ivy--flx-sort more intelligent
From: |
Oleh Krehel |
Subject: |
[elpa] master dc693c3 280/348: Make ivy--flx-sort more intelligent |
Date: |
Sat, 8 Apr 2017 11:04:15 -0400 (EDT) |
branch: master
commit dc693c37dae89e9a4302a5cce42f5321f83946c8
Author: PythonNut <address@hidden>
Commit: PythonNut <address@hidden>
Make ivy--flx-sort more intelligent
---
ivy.el | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 62 insertions(+), 20 deletions(-)
diff --git a/ivy.el b/ivy.el
index 85d1c4a..2eb5292 100644
--- a/ivy.el
+++ b/ivy.el
@@ -2703,26 +2703,68 @@ no sorting is done.")
(defun ivy--flx-sort (name cands)
"Sort according to closeness to string NAME the string list CANDS."
(condition-case nil
- (if (and cands
- (< (length cands) ivy-flx-limit))
- (let* ((flx-name (if (string-match "^\\^" name)
- (substring name 1)
- name))
- (cands-with-score
- (delq nil
- (mapcar
- (lambda (x)
- (let ((score (flx-score x flx-name ivy--flx-cache)))
- (and score
- (cons score x))))
- cands))))
- (if cands-with-score
- (mapcar #'cdr
- (sort cands-with-score
- (lambda (x y)
- (> (caar x) (caar y)))))
- cands))
- cands)
+ (let* (;; an optimized regex for fuzzy matching
+ ;; "abc" → "\\`[^a]*a[^b]*b[^c]*c"
+ (fuzzy-regex (if (= (elt name 0) ?^)
+ (concat "^"
+ (regexp-quote (substring name 1 2))
+ (mapconcat
+ (lambda (x)
+ (setq x (string x))
+ (concat "[^" x "]*" (regexp-quote x)))
+ (substring name 2)
+ ""))
+ (concat "^"
+ (mapconcat
+ (lambda (x)
+ (setq x (string x))
+ (concat "[^" x "]*" (regexp-quote x)))
+ name
+ ""))))
+
+ ;; strip off the leading "^" for flx matching
+ (flx-name (if (string-match "^\\^" name)
+ (substring name 1)
+ name))
+
+ (cands-left)
+ (cands-to-sort))
+
+ ;; filter out non-matching candidates
+ (dolist (cand cands)
+ (when (string-match fuzzy-regex cand)
+ (push cand cands-left)))
+
+ ;; pre-sort the candidates by length before partitioning
+ (setq cands-left (sort cands-left
+ (lambda (c1 c2)
+ (< (length c1)
+ (length c2)))))
+
+ ;; partition the candidates into sorted and unsorted groups
+ (dotimes (_n (min (length cands-left) ivy-flx-limit))
+ (push (pop cands-left) cands-to-sort))
+
+ (append
+ ;; compute all of the flx scores in one pass and sort
+ (mapcar #'car
+ (sort (mapcar
+ (lambda (cand)
+ (cons cand
+ (car (flx-score cand
+ flx-name
+ ivy--flx-cache))))
+ cands-to-sort)
+ (lambda (c1 c2)
+ ;; break ties by length
+ (if (/= (cdr c1) (cdr c2))
+ (> (cdr c1)
+ (cdr c2))
+ (< (length (car c1))
+ (length (car c2)))))))
+
+ ;; add the unsorted candidates
+ cands-left))
(error
cands)))
- [elpa] master 7100925 302/348: ivy.el (ivy-completing-read-handlers-alist): New defcustom, (continued)
- [elpa] master 7100925 302/348: ivy.el (ivy-completing-read-handlers-alist): New defcustom, Oleh Krehel, 2017/04/08
- [elpa] master b894874 309/348: ivy.el (ivy-occur-grep-mode): Call wgrep-setup if available, Oleh Krehel, 2017/04/08
- [elpa] master 80d3fe3 310/348: Fix compilation warnings, Oleh Krehel, 2017/04/08
- [elpa] master b0ccc7a 312/348: README.md: Recommend enable-recursive-minibuffers, Oleh Krehel, 2017/04/08
- [elpa] master 1ac1cc1 316/348: ivy.el (ivy-completing-read): Don't require-match for null, Oleh Krehel, 2017/04/08
- [elpa] master 7ca301b 334/348: swiper.el (swiper-goto-start-of-match) New defcustom, Oleh Krehel, 2017/04/08
- [elpa] master fc2e198 335/348: swiper.el (swiper--action): Set evil search direction to forward, Oleh Krehel, 2017/04/08
- [elpa] master 718a06b 345/348: doc/Changelog.org: Fix links, Oleh Krehel, 2017/04/08
- [elpa] master 802c9da 271/348: counsel.el: Add counsel-command-history, Oleh Krehel, 2017/04/08
- [elpa] master ee91a25 283/348: ivy.el (ivy--sort-maybe): Fix byte-compiler warning, Oleh Krehel, 2017/04/08
- [elpa] master dc693c3 280/348: Make ivy--flx-sort more intelligent,
Oleh Krehel <=
- [elpa] master 182e35e 298/348: Apply search highlighting for evil when applicable, Oleh Krehel, 2017/04/08
- [elpa] master aedea1a 289/348: counsel.el (counsel-package): Add func to manage packages., Oleh Krehel, 2017/04/08
- [elpa] master b9c52be 301/348: counsel.el (counsel-git): Add "x" action, Oleh Krehel, 2017/04/08
- [elpa] master dbeb5b1 303/348: Ensure counsel-M-x preserves last-command, Oleh Krehel, 2017/04/08
- [elpa] master ae3ca26 117/348: counsel.el (counsel-locate-action-extern): Add w32 support, Oleh Krehel, 2017/04/08
- [elpa] master 3e651a3 116/348: Make swiper-all use point positions instead of line positions, Oleh Krehel, 2017/04/08
- [elpa] master ef3f010 128/348: ivy.el (ivy--insert-prompt): Improve the extra "\n" logic, Oleh Krehel, 2017/04/08
- [elpa] master 870112e 176/348: counsel.el (counsel-ag-function): Use sync on remote, Oleh Krehel, 2017/04/08
- [elpa] master 1914ecd 203/348: counsel.el (counsel-recoll-function): Add shell-quote-argument, Oleh Krehel, 2017/04/08
- [elpa] master f0e49d5 213/348: Improve recursive minibuffers with two emacsclients, Oleh Krehel, 2017/04/08