emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master e98883f 162/348: Improve fuzzy swiper highlight


From: Oleh Krehel
Subject: [elpa] master e98883f 162/348: Improve fuzzy swiper highlight
Date: Sat, 8 Apr 2017 11:03:47 -0400 (EDT)

branch: master
commit e98883f50f5794c3bc79634a1fab6963a016a472
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Improve fuzzy swiper highlight
    
    * ivy.el (ivy--regex-fuzzy): Is non-greedy now - flx does the same.
    (ivy--format-minibuffer-line): Ensure flx is available.
    
    * ivy-test.el (ivy--regex-fuzzy): Update test.
    
    * swiper.el (swiper--add-overlays): Join contiguous matches together.
    (swiper--add-overlay): New defun.
    
    Fixes #651
---
 ivy-test.el |  6 +++---
 ivy.el      | 19 +++++++++++--------
 swiper.el   | 44 ++++++++++++++++++++++++++------------------
 3 files changed, 40 insertions(+), 29 deletions(-)

diff --git a/ivy-test.el b/ivy-test.el
index f49982c..cb2befa 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -137,11 +137,11 @@
 
 (ert-deftest ivy--regex-fuzzy ()
   (should (string= (ivy--regex-fuzzy "tmux")
-                   "\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
+                   "\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)"))
   (should (string= (ivy--regex-fuzzy "^tmux")
-                   "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
+                   "^\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)"))
   (should (string= (ivy--regex-fuzzy "^tmux$")
-                   "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)$"))
+                   "^\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)$"))
   (should (string= (ivy--regex-fuzzy "")
                    ""))
   (should (string= (ivy--regex-fuzzy "^")
diff --git a/ivy.el b/ivy.el
index aa90250..e74c0c5 100644
--- a/ivy.el
+++ b/ivy.el
@@ -1976,7 +1976,8 @@ Insert .* between each char."
                   (mapconcat
                    (lambda (x)
                      (format "\\(%c\\)" x))
-                   (string-to-list (match-string 2 str)) ".*")
+                   (string-to-list (match-string 2 str))
+                   ".*?")
                   (match-string 3 str))
         (setq ivy--subexps (length (match-string 2 str))))
     str))
@@ -2693,13 +2694,15 @@ SEPARATOR is used to join the candidates."
                            ivy-minibuffer-faces)
                       str))
                    (cl-incf i)))))
-            ((or (eq ivy--regex-function 'ivy--regex-fuzzy)
-                 (and (eq ivy--regex-function 'swiper--re-builder)
-                      (let ((caller (ivy-state-caller ivy-last)))
-                        (eq (or (and caller
-                                     (cdr (assoc caller 
ivy-re-builders-alist)))
-                                (cdr (assoc t ivy-re-builders-alist)))
-                            'ivy--regex-fuzzy))))
+            ((and
+              (require 'flx nil 'noerror)
+              (or (eq ivy--regex-function 'ivy--regex-fuzzy)
+                  (and (eq ivy--regex-function 'swiper--re-builder)
+                       (let ((caller (ivy-state-caller ivy-last)))
+                         (eq (or (and caller
+                                      (cdr (assoc caller 
ivy-re-builders-alist)))
+                                 (cdr (assoc t ivy-re-builders-alist)))
+                             'ivy--regex-fuzzy)))))
              (let ((flx-name (if (string-match "^\\^" ivy-text)
                                  (substring ivy-text 1)
                                ivy-text)))
diff --git a/swiper.el b/swiper.el
index d43244b..ed72d58 100644
--- a/swiper.el
+++ b/swiper.el
@@ -613,24 +613,32 @@ WND, when specified is the window."
           ;; RE can become an invalid regexp
           (while (and (ignore-errors (re-search-forward re end t))
                       (> (- (match-end 0) (match-beginning 0)) 0))
-            (let ((i 0))
-              (while (<= i ivy--subexps)
-                (when (match-beginning i)
-                  (let ((overlay (make-overlay (match-beginning i)
-                                               (match-end i)))
-                        (face
-                         (cond ((zerop ivy--subexps)
-                                (cadr swiper-faces))
-                               ((zerop i)
-                                (car swiper-faces))
-                               (t
-                                (nth (1+ (mod (+ i 2) (1- (length 
swiper-faces))))
-                                     swiper-faces)))))
-                    (push overlay swiper--overlays)
-                    (overlay-put overlay 'face face)
-                    (overlay-put overlay 'window wnd)
-                    (overlay-put overlay 'priority i)))
-                (cl-incf i)))))))))
+            (swiper--add-overlay (match-beginning 0) (match-end 0)
+                                 (if (zerop ivy--subexps)
+                                     (cadr swiper-faces)
+                                   (car swiper-faces))
+                                 wnd 0)
+            (let ((i 1)
+                  (j 0))
+              (while (<= (cl-incf j) ivy--subexps)
+                (let ((bm (match-beginning j))
+                      (em (match-end j)))
+                  (while (and (< j ivy--subexps)
+                              (= em (match-beginning (+ j 1))))
+                    (setq em (match-end (cl-incf j))))
+                  (swiper--add-overlay
+                   bm em
+                   (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
+                        swiper-faces)
+                   wnd i)
+                  (cl-incf i))))))))))
+
+(defun swiper--add-overlay (beg end face wnd priority)
+  (let ((overlay (make-overlay beg end)))
+    (push overlay swiper--overlays)
+    (overlay-put overlay 'face face)
+    (overlay-put overlay 'window wnd)
+    (overlay-put overlay 'priority priority)))
 
 (defcustom swiper-action-recenter nil
   "When non-nil, recenter after exiting `swiper'."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]