1

Edit: this question is a duplicate of this one; I'm leaving it because I got a helpful answer and I didn't find the other one after reasonable amount of searching (so I hope this one being here will double the liklihood of future searchers finding a solution)

I use elpy with elpy-module-company as one of its modules. when I type "self._", the sorting causes it to propose all the dunders (__annotations__, __class__, &c) that I don't usually care about.

enter image description here

I name some attributes starting with single underscore. I would like to see self._method_i_wrote sort before self.__annotations__

I see that maybe company-sort-predicate is what I want, but I don't know how to write the predicate or tell company to use it. Can anyone give an example?

I think hooking in the sort predicate I could figure out, but defining the sort predicate is beyond my elisp powers.

An i-could-live-with-it solution (for what is admittedly a first world problem) would be a filter that tosses out dunders completely.

1

You might find what you are looking for here: How to make private python methods the last company-mode choices?

Here is a way I got strings to sort the way you described:

(sort '("_two" "_one" "__two" "__one" "one")
      (lambda (s1 s2)
    (if (and (string-prefix-p "_" s1)
         (string-prefix-p "_" s2))       
        ;; sort _ words by number of leading _, then lexically
        (let ((n1 (progn (string-match "\\(^_+\\)" s1)
                 (length (match-string 0 s1))))
          (n2 (progn (string-match "\\(^_+\\)" s2)
                 (length (match-string 0 s2)))))
          (if (= n1 n2)
          ;; sort lexically
          (string< (substring s1 n1) (substring s2 n2))
        ;; else by number of _
        (< n1 n2)))
      ;; regular sorting          
      (string< s1 s2))))

This outputs ("_one" "_two" "__one" "__two" "one")

1
  • Thank you. My question should get marked as a duplicate. Your sort logic will be helpful I think, I haven't gotten company to use it yet. Sep 18 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.