]> code.delx.au - gnu-emacs-elpa/commitdiff
Fix company-occurrence-weight-function
authorDmitry Gutov <dgutov@yandex.ru>
Mon, 21 Jul 2014 13:10:39 +0000 (16:10 +0300)
committerDmitry Gutov <dgutov@yandex.ru>
Mon, 21 Jul 2014 13:10:39 +0000 (16:10 +0300)
#152

company-tests.el
company.el

index 8bc8572d571c4066cf84ae040b84f2021148b051..fc7aec170bc8a6e1b514be942716a89280d1497b 100644 (file)
       (let ((company-backend (list immediate)))
         (should (equal '("f") (company-call-backend 'candidates "foo")))))))
 
+;;; Transformers
+
+(ert-deftest company-occurrence-prefer-closest-above ()
+  (with-temp-buffer
+    (insert "foo0
+foo1
+")
+    (save-excursion
+      (insert "
+foo3
+foo2"))
+    (let ((company-backend 'company-dabbrev)
+          (company-occurrence-weight-function
+           'company-occurrence-prefer-closest-above))
+      (should (equal '("foo1" "foo0" "foo3" "foo2" "foo4")
+                     (company-sort-by-occurrence
+                      '("foo0" "foo1" "foo2" "foo3" "foo4")))))))
+
+(ert-deftest company-occurrence-prefer-any-closest ()
+  (with-temp-buffer
+    (insert "foo0
+foo1
+")
+    (save-excursion
+      (insert "
+foo3
+foo2"))
+    (let ((company-backend 'company-dabbrev)
+          (company-occurrence-weight-function
+           'company-occurrence-prefer-any-closest))
+      (should (equal '("foo1" "foo3" "foo0" "foo2" "foo4")
+                     (company-sort-by-occurrence
+                      '("foo0" "foo1" "foo2" "foo3" "foo4")))))))
+
 ;;; Template
 
 (ert-deftest company-template-removed-after-the-last-jump ()
index 8bc2791464014795816d8eb9cc918c54e3c37b6c..7ebc214d92fee26d2e63c5d7003b37b03c2affec 100644 (file)
@@ -1194,22 +1194,23 @@ can retrieve meta-data for them."
 (defcustom company-occurrence-weight-function
   #'company-occurrence-prefer-closest-above
   "Function to weigh matches in `company-sort-by-occurrence'.
-It's called with two arguments: the beginning and the end of the match."
+It's called with three arguments: cursor position, the beginning and the
+end of the match."
   :type '(choice
           (const :tag "First above point, then below point"
                  company-occurrence-prefer-closest-above)
           (const :tag "Prefer closest in any direction"
                  company-occurrence-prefer-any-closest)))
 
-(defun company-occurrence-prefer-closest-above (match-beg match-end)
+(defun company-occurrence-prefer-closest-above (pos match-beg match-end)
   "Give priority to the matches above point, then those below point."
-  (if (< match-beg (point))
-      (- (point) match-end)
+  (if (< match-beg pos)
+      (- pos match-end)
     (- match-beg (window-start))))
 
-(defun company-occurrence-prefer-any-closest (_match-beg match-end)
+(defun company-occurrence-prefer-any-closest (pos _match-beg match-end)
   "Give priority to the matches closest to the point."
-  (abs (- (point) match-end)))
+  (abs (- pos match-end)))
 
 (defun company-sort-by-occurrence (candidates)
   "Sort CANDIDATES according to their occurrences.
@@ -1235,6 +1236,7 @@ Keywords and function definition names are ignored."
                  (push
                   (cons candidate
                         (funcall company-occurrence-weight-function
+                                 start-point
                                  (match-beginning 0)
                                  (match-end 0)))
                   occurs)