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

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

[elpa] 13/16: Fix company-occurrence-weight-function


From: Dmitry Gutov
Subject: [elpa] 13/16: Fix company-occurrence-weight-function
Date: Sat, 26 Jul 2014 04:53:24 +0000

dgutov pushed a commit to branch master
in repository elpa.

commit 3e3815c5d5b1b73da70f5930ebd3130acc565fbc
Author: Dmitry Gutov <address@hidden>
Date:   Mon Jul 21 16:10:39 2014 +0300

    Fix company-occurrence-weight-function
    
    #152
---
 company-tests.el |   34 ++++++++++++++++++++++++++++++++++
 company.el       |   14 ++++++++------
 2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/company-tests.el b/company-tests.el
index 8bc8572..fc7aec1 100644
--- a/company-tests.el
+++ b/company-tests.el
@@ -714,6 +714,40 @@
       (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 ()
diff --git a/company.el b/company.el
index 8bc2791..7ebc214 100644
--- a/company.el
+++ b/company.el
@@ -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)



reply via email to

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