[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] 08/16: company-sort-by-occurrence: allow customizing the weighing
From: |
Dmitry Gutov |
Subject: |
[elpa] 08/16: company-sort-by-occurrence: allow customizing the weighing logic |
Date: |
Sat, 26 Jul 2014 04:53:21 +0000 |
dgutov pushed a commit to branch master
in repository elpa.
commit fd35107ea9dbc1ef858e18e35e0e1a1c8ef6820e
Author: Dmitry Gutov <address@hidden>
Date: Sun Jul 20 05:49:03 2014 +0300
company-sort-by-occurrence: allow customizing the weighing logic
Closes #152
---
NEWS.md | 2 ++
company.el | 36 ++++++++++++++++++++++++++++--------
2 files changed, 30 insertions(+), 8 deletions(-)
diff --git a/NEWS.md b/NEWS.md
index e547f79..d5184c3 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -2,6 +2,8 @@
## Next
+* New user option `company-occurrence-weight-function`, allowing to tweak the
+ behavior of the transformer `company-sort-by-occurrence`.
* Setting `company-idle-delay` to `t` is deprecated. Use the value 0 instead.
## 2014-07-01 (0.8.1)
diff --git a/company.el b/company.el
index ceb815a..662a220 100644
--- a/company.el
+++ b/company.el
@@ -1191,11 +1191,31 @@ can retrieve meta-data for them."
(setq c (funcall tr c)))
c))
+(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."
+ :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)
+ "Give priority to the matches above point, then those below point."
+ (if (< match-beg (point))
+ (- (point) match-end)
+ (- match-beg (window-start))))
+
+(defun company-occurrence-prefer-any-closest (_match-beg match-end)
+ "Give priority to the matches closest to the point."
+ (abs (- (point) match-end)))
+
(defun company-sort-by-occurrence (candidates)
"Sort CANDIDATES according to their occurrences.
Searches for each in the currently visible part of the current buffer and
-gives priority to the closest ones above point, then closest ones below
-point. The rest of the list is appended unchanged.
+prioritizes the matches according to `company-occurrence-weight-function'.
+The rest of the list is appended unchanged.
Keywords and function definition names are ignored."
(let* (occurs
(noccurs
@@ -1203,8 +1223,8 @@ Keywords and function definition names are ignored."
(lambda (candidate)
(when (or
(save-excursion
- (progn (forward-char (- (length company-prefix)))
- (search-backward candidate (window-start) t)))
+ (forward-char (- (length company-prefix)))
+ (search-backward candidate (window-start) t))
(save-excursion
(search-forward candidate (window-end) t)))
(let ((beg (match-beginning 0))
@@ -1218,10 +1238,10 @@ Keywords and function definition names are ignored."
(company-call-backend 'prefix))))
(and (stringp prefix)
(= (length prefix) (- end beg))))))
- (push (cons candidate (if (< beg (point))
- (- (point) end)
- (- beg (window-start))))
- occurs)
+ (push
+ (cons candidate
+ (funcall company-occurrence-weight-function beg end))
+ occurs)
t))))
candidates)))
(nconc
- [elpa] branch master updated (68ccff3 -> ca001a5), Dmitry Gutov, 2014/07/26
- [elpa] 03/16: company-pseudo-tooltip-frontend: Small rewrite, Dmitry Gutov, 2014/07/26
- [elpa] 06/16: +1 char, -1 line, Dmitry Gutov, 2014/07/26
- [elpa] 05/16: company-pseudo-tooltip-guard: Stabilize when the popup is displayed up, Dmitry Gutov, 2014/07/26
- [elpa] 09/16: company-sort-by-occurrence: continue search after predicate fails, Dmitry Gutov, 2014/07/26
- [elpa] 07/16: Deprecate setting company-idle-delay to t, Dmitry Gutov, 2014/07/26
- [elpa] 08/16: company-sort-by-occurrence: allow customizing the weighing logic,
Dmitry Gutov <=
- [elpa] 11/16: company-css-property-value-regexp: prohibit opening curlies, Dmitry Gutov, 2014/07/26
- [elpa] 13/16: Fix company-occurrence-weight-function, Dmitry Gutov, 2014/07/26
- [elpa] 12/16: company-css-property-values: delete duplicates, Dmitry Gutov, 2014/07/26
- [elpa] 10/16: company-capf: get rid of caching last capf-data, Dmitry Gutov, 2014/07/26
- [elpa] 14/16: Fix tests in batch mode, Dmitry Gutov, 2014/07/26
- [elpa] 01/16: company-frontends-set: never modify VALUE destructively, Dmitry Gutov, 2014/07/26
- [elpa] 15/16: Release 0.8.2, Dmitry Gutov, 2014/07/26
- [elpa] 16/16: Merge commit 'b1d019a4c815ac8bdc240d69eaa74eb4e34640e8' from company-master, Dmitry Gutov, 2014/07/26
- [elpa] 04/16: company-pseudo-tooltip-guard: Invalidate after crossing line continuation, Dmitry Gutov, 2014/07/26
- [elpa] 02/16: Fix popup when prefix is split by line continuation, Dmitry Gutov, 2014/07/26