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

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

[elpa] 40/52: Define company-sort-by-backend-importance


From: Dmitry Gutov
Subject: [elpa] 40/52: Define company-sort-by-backend-importance
Date: Tue, 01 Jul 2014 11:53:30 +0000

dgutov pushed a commit to branch master
in repository elpa.

commit 8a7120b84d0a45295c403b50caa430bf58e63649
Author: Dmitry Gutov <address@hidden>
Date:   Sun Jun 22 06:57:46 2014 +0300

    Define company-sort-by-backend-importance
    
    Closes #110
---
 NEWS.md    |    1 +
 company.el |   20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/NEWS.md b/NEWS.md
index 4c2ffe1..e9738c8 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -2,6 +2,7 @@
 
 ## Next
 
+* New transformer, `company-sort-by-backend-importance`.
 * When grouped back-ends are used, the back-end of the current candidate is
   indicated in the mode-line, enclosed in angle brackets.
 * New user option `company-gtags-insert-arguments`, t by default.
diff --git a/company.el b/company.el
index c0e6a98..feea415 100644
--- a/company.el
+++ b/company.el
@@ -452,6 +452,8 @@ Each function gets called with the return value of the 
previous one."
   :type '(choice
           (const :tag "None" nil)
           (const :tag "Sort by occurrence" (company-sort-by-occurrence))
+          (const :tag "Sort by back-end importance"
+                 (company-sort-by-backend-importance))
           (repeat :tag "User defined" (function))))
 
 (defcustom company-completion-started-hook nil
@@ -1221,6 +1223,24 @@ Keywords and function definition names are ignored."
      (mapcar #'car (sort occurs (lambda (e1 e2) (<= (cdr e1) (cdr e2)))))
      noccurs)))
 
+(defun company-sort-by-backend-importance (candidates)
+  "Sort CANDIDATES as two priority groups.
+If `company-backend' is a function, do nothing.  If it's a list, move
+candidates from back-ends before keyword `:with' to the front.  Candidates
+from the rest of the back-ends in the group, if any, will be left at the end."
+  (if (functionp company-backend)
+      candidates
+    (let ((low-priority (cdr (memq :with company-backend))))
+      (if (null low-priority)
+          candidates
+        (sort candidates
+              (lambda (c1 c2)
+                (and
+                 (let ((b2 (get-text-property 0 'company-backend c2)))
+                   (and b2 (memq b2 low-priority)))
+                 (let ((b1 (get-text-property 0 'company-backend c1)))
+                   (or (not b1) (not (memq b1 low-priority)))))))))))
+
 (defun company-idle-begin (buf win tick pos)
   (and (eq buf (current-buffer))
        (eq win (selected-window))



reply via email to

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