Pārlūkot izejas kodu

Clear flx-ido cache after updaing dynamic collections

flx-ido-mode uses a cache that needs to be cleared each time
"ido-cur-list" changes.

Fixes #134.
Ryan C. Thompson 7 gadi atpakaļ
vecāks
revīzija
e719475333
3 mainītis faili ar 123 papildinājumiem un 15 dzēšanām
  1. 1 0
      Cask
  2. 10 2
      ido-completing-read+.el
  3. 112 13
      tests/test-ido-completing-read+.el

+ 1 - 0
Cask

@@ -9,6 +9,7 @@
 (depends-on "memoize" "1.1")
 
 (development
+ (depends-on "flx-ido")
  (depends-on "with-simulated-input"
              :git "https://github.com/DarwinAwardWinner/with-simulated-input.git"
              :files ("*.el"))

+ 10 - 2
ido-completing-read+.el

@@ -137,6 +137,10 @@ using it, so the initial value shouldn't matter.")))
 (define-ido-internal-var ido-require-match)
 (define-ido-internal-var ido-process-ignore-lists)
 
+;; Vars and functions from flx-ido package
+(defvar flx-ido-mode)
+(declare-function flx-ido-reset "ext:flx-ido.el")
+
 ;;;###autoload
 (defvar ido-cr+-minibuffer-depth -1
   "Minibuffer depth of the most recent ido-cr+ activation.
@@ -868,7 +872,7 @@ result."
               (setq need-reverse (not need-reverse))
               restriction-matches))
    ;; Each run of `ido-set-matches-1' reverses the order, so reverse
-   ;; it one more time if it had an odd number of reverses
+   ;; it one more time if it had an odd number of reverses.
    finally return
    (if need-reverse
        (nreverse filtered-collection)
@@ -908,7 +912,11 @@ This has no effect unless `ido-cr+-dynamic-collection' is non-nil."
               string ido-cr+-dynamic-collection predicate)
              into result
              finally return result)))
-      (when new-completions
+      (when (not (equal new-completions ido-cur-list))
+        (when (and (bound-and-true-p flx-ido-mode)
+                   (functionp 'flx-ido-reset))
+          ;; Reset flx-ido since the set of completions has changed
+          (funcall 'flx-ido-reset))
         (setq ido-cur-list (delete-dups new-completions))
         (when ido-cr+-active-restrictions
           (setq ido-cur-list (ido-cr+-apply-restrictions

+ 112 - 13
tests/test-ido-completing-read+.el

@@ -1,6 +1,7 @@
 ;;; -*- lexical-binding: t -*-
 
 (require 'ido)
+(require 'flx-ido)
 (require 'ido-completing-read+)
 (require 'buttercup)
 (require 'cl-lib)
@@ -114,18 +115,20 @@ also accept a quoted list for the sake of convenience."
   ;; test, saving the previous values for later restoration.
   (before-each
     (shadow-vars
-      ((ido-mode t)
-       (ido-ubiquitous-mode t)
-       ido-cr+-debug-mode
-       ido-cr+-auto-update-blacklist
-       ido-cr+-fallback-function
-       ido-cr+-max-items
-       ido-cr+-function-blacklist
-       ido-cr+-function-whitelist
-       ido-cr+-nil-def-alternate-behavior-list
-       ido-cr+-replace-completely
-       ido-confirm-unique-completion
-       ido-enable-flex-matching)))
+     ((ido-mode t)
+      (ido-ubiquitous-mode t)
+      ido-cr+-debug-mode
+      ido-cr+-auto-update-blacklist
+      ido-cr+-fallback-function
+      ido-cr+-max-items
+      ido-cr+-function-blacklist
+      ido-cr+-function-whitelist
+      ido-cr+-nil-def-alternate-behavior-list
+      ido-cr+-replace-completely
+      ido-confirm-unique-completion
+      ido-enable-flex-matching
+      ido-enable-dot-prefix
+      flx-ido-mode)))
 
   ;; Restore the saved values after each test
   (after-each
@@ -465,7 +468,103 @@ also accept a quoted list for the sake of convenience."
            (with-simulated-input "eee C-SPC bbb C-SPC ggg RET"
              (ido-completing-read+
               "Pick: " (collection-as-function collection) nil t nil nil (car collection)))
-           :to-equal "bbb-eee-ggg"))))
+           :to-equal "bbb-eee-ggg")))
+
+      (describe "with flx-ido-mode"
+        (before-each
+          (flx-ido-mode 1)
+          (flx-ido-reset))
+
+        (it "should allow selection of dynamically-added completions"
+          (expect
+           (with-simulated-input "hello-w RET"
+             (ido-completing-read+ "Say something: " my-dynamic-collection))
+           :to-equal "hello-world")
+          (expect 'ido-cr+-update-dynamic-collection
+                  :to-have-been-called))
+
+        (it "should allow ido flex-matching of dynamically-added completions"
+          (expect
+           (with-simulated-input "hello-ld RET"
+             (ido-completing-read+ "Say something: " my-dynamic-collection))
+           :to-equal
+           "hello-world")
+          (expect 'ido-cr+-update-dynamic-collection
+                  :to-have-been-called))
+        (it "should do a dynamic update when pressing TAB"
+          (expect
+           (with-simulated-input "h TAB -ld RET"
+             (ido-completing-read+ "Say something: " my-dynamic-collection))
+           :to-equal
+           "hello-world")
+          (expect 'ido-cr+-update-dynamic-collection
+                  :to-have-been-called))
+        (it "should do a dynamic update when idle"
+          (expect
+           (with-simulated-input
+               '("h"
+                 (wsi-simulate-idle-time (1+ ido-cr+-dynamic-update-idle-time))
+                 "-ld RET")
+             (ido-completing-read+ "Say something: " my-dynamic-collection))
+           :to-equal
+           "hello-world")
+          (expect 'ido-cr+-update-dynamic-collection
+                  :to-have-been-called))
+        (it "should do a dynamic update when there is only one match remaining"
+          (expect
+           (with-simulated-input "hell-ld RET"
+             (ido-completing-read+ "Say something: " my-dynamic-collection))
+           :to-equal
+           "hello-world")
+          (expect 'ido-cr+-update-dynamic-collection
+                  :to-have-been-called))
+        (it "should not exit with a unique match if new matches are dynamically added"
+          (expect
+           (with-simulated-input '("hell TAB -ld RET")
+             (ido-completing-read+ "Say something: " my-dynamic-collection))
+           :to-equal
+           "hello-world")
+          (expect 'ido-cr+-update-dynamic-collection
+                  :to-have-been-called))
+        (it "should exit with a match that is still unique after dynamic updating"
+          (expect
+           (with-simulated-input '("helic TAB")
+             (ido-completing-read+ "Say something: " my-dynamic-collection))
+           :to-equal
+           "helicopter")
+          (expect 'ido-cr+-update-dynamic-collection
+                  :to-have-been-called))
+        (it "should respect `ido-restrict-to-matches' when doing dynamic updates"
+          (let ((collection
+                 (list "aaa-ddd-ggg" "aaa-eee-ggg" "aaa-fff-ggg"
+                       "bbb-ddd-ggg" "bbb-eee-ggg" "bbb-fff-ggg"
+                       "ccc-ddd-ggg" "ccc-eee-ggg" "ccc-fff-ggg"
+                       "aaa-ddd-hhh" "aaa-eee-hhh" "aaa-fff-hhh"
+                       "bbb-ddd-hhh" "bbb-eee-hhh" "bbb-fff-hhh"
+                       "ccc-ddd-hhh" "ccc-eee-hhh" "ccc-fff-hhh"
+                       "aaa-ddd-iii" "aaa-eee-iii" "aaa-fff-iii"
+                       "bbb-ddd-iii" "bbb-eee-iii" "bbb-fff-iii"
+                       "ccc-ddd-iii" "ccc-eee-iii" "ccc-fff-iii")))
+            ;; Test the internal function
+            (expect
+             (ido-cr+-apply-restrictions
+              collection
+              (list (cons nil "bbb")
+                    (cons nil "eee")))
+             :to-equal '("bbb-eee-ggg" "bbb-eee-hhh" "bbb-eee-iii"))
+
+            ;; First verify it without a dynamic collection
+            (expect
+             (with-simulated-input "eee C-SPC bbb C-SPC ggg RET"
+               (ido-completing-read+
+                "Pick: " collection nil t nil nil (car collection)))
+             :to-equal "bbb-eee-ggg")
+            ;; Now test the same with a dynamic collection
+            (expect
+             (with-simulated-input "eee C-SPC bbb C-SPC ggg RET"
+               (ido-completing-read+
+                "Pick: " (collection-as-function collection) nil t nil nil (car collection)))
+             :to-equal "bbb-eee-ggg")))))
 
     (describe "with unusual inputs"
       (it "should accept a COLLECTION of symbols"