]> code.delx.au - gnu-emacs-elpa/commitdiff
Improve duplicates removal
authorDmitry Gutov <dgutov@yandex.ru>
Tue, 13 Jan 2015 02:28:20 +0000 (05:28 +0300)
committerDmitry Gutov <dgutov@yandex.ru>
Tue, 13 Jan 2015 02:28:20 +0000 (05:28 +0300)
Remove items with equal annotations, even when separated by item(s) with
different annotation(s). Provided the string values match, of course.

company.el
test/core-tests.el

index 6d5cc6d0be1dd3df9a7bc65895404880c181ce57..41291891aa0a55e3e1019553df5a72f07759bbfd 100644 (file)
@@ -1161,21 +1161,25 @@ can retrieve meta-data for them."
   (company--transform-candidates candidates))
 
 (defun company--strip-duplicates (candidates)
-  (let ((c2 candidates))
+  (let ((c2 candidates)
+        (annos 'unk))
     (while c2
       (setcdr c2
-              (let ((str (car c2))
-                    (anno 'unk))
-                (pop c2)
+              (let ((str (pop c2)))
                 (while (let ((str2 (car c2)))
                          (if (not (equal str str2))
-                             nil
-                           (when (eq anno 'unk)
-                             (setq anno (company-call-backend
-                                         'annotation str)))
-                           (equal anno
-                                  (company-call-backend
-                                   'annotation str2))))
+                             (progn
+                               (setq annos 'unk)
+                               nil)
+                           (when (eq annos 'unk)
+                             (setq annos (list (company-call-backend
+                                                'annotation str))))
+                           (let ((anno2 (company-call-backend
+                                         'annotation str2)))
+                             (if (member anno2 annos)
+                                 t
+                               (push anno2 annos)
+                               nil))))
                   (pop c2))
                 c2)))))
 
index b395c9f0926e9a3b71105774ff716a6ca1e447ff..13e547e3d36b15ac566ad3dcc9119ea675a7c580 100644 (file)
       (company-complete-selection)
       (should (string= "tea-cup" (buffer-string))))))
 
+(defvar ct-sorted nil)
+
+(defun ct-equal-including-properties (list1 list2)
+  (or (and (not list1) (not list2))
+      (and (ert-equal-including-properties (car list1) (car list2))
+           (ct-equal-including-properties (cdr list1) (cdr list2)))))
+
+(ert-deftest company-strips-duplicates-within-groups ()
+  (let* ((kvs '(("a" . "b")
+                ("a" . nil)
+                ("a" . "b")
+                ("a" . "c")
+                ("a" . "b")
+                ("b" . "c")
+                ("b" . nil)
+                ("a" . "b")))
+         (fn (lambda (kvs)
+               (mapcar (lambda (kv) (propertize (car kv) 'ann (cdr kv)))
+                       kvs)))
+         (company-backend
+          (lambda (command &optional arg)
+            (pcase command
+              (`prefix "")
+              (`sorted ct-sorted)
+              (`duplicates t)
+              (`annotation (get-text-property 0 'ann arg)))))
+         (reference '(("a" . "b")
+                      ("a" . nil)
+                      ("a" . "c")
+                      ("b" . "c")
+                      ("b" . nil)
+                      ("a" . "b"))))
+    (let ((ct-sorted t))
+      (should (ct-equal-including-properties
+               (company--preprocess-candidates (funcall fn kvs))
+               (funcall fn reference))))
+    (should (ct-equal-including-properties
+             (company--preprocess-candidates (funcall fn kvs))
+             (funcall fn (butlast reference))))))
+
 ;;; Row and column
 
 (ert-deftest company-column-with-composition ()