6

I'm using emacs with company-mode and I'd like the 'private' methods (method that start with underline) to be shown last, since they are kind of useless for imported libraries in python. See the image:
underline methods

I'd like it to be sorted like this:
desired way

  • 2
    Have you looked into company-transformers? Looks like what you want could be achieved by setting this to a function of your own. On the other hand, this is something more people should want, so there's likely an easier way. – Meaningful Username May 13 '15 at 21:22
  • Well I don't know elisp but I spent an hour hacking around anaconda functions and I managed to remove the underline methods. When I have the time I'll try to push them at the end of the list. I'll also take a look at this function you mentioned and share the code once it's ready. Thanks – Jesse May 13 '15 at 22:11
  • @MeaningfulUsername done with company-transformers. Thanks again! – Jesse May 14 '15 at 13:22
  • No problems, just happened to stumble over that variable some days ago. – Meaningful Username May 14 '15 at 13:37
4

I've managed to do it myself. Here's the code

(defun company-transform-python (candidates)
  (let ((deleted))
    (mapcar #'(lambda (c)
        (if (or (string-prefix-p "_" c) (string-prefix-p "._" c))
            (progn
              (add-to-list 'deleted c)
              (setq candidates (delete c candidates)))))
        candidates) 
    (append candidates (nreverse deleted))))


(defun my-python-conf()
  (setq-local company-transformers
          (append company-transformers '(company-transform-python))))

(add-hook 'python-mode-hook 'my-python-conf)

It's kind of my first contact more deeply with elisp, so please excuse the bad code.

  • 2
    You might want to consider setq-local in my-python-config. In that way it will only affect python buffers. In this way I believe it will overwrite the global company-transformers. You might want to add it to the list too instead of disregarding the previous value. Just some thoughts :). – Meaningful Username May 14 '15 at 13:36
  • @MeaningfulUsername done :) – Jesse May 14 '15 at 13:58
  • 2
    The built-in string-prefix-p does what your string/starts-with does. – erikstokes May 15 '15 at 1:08
  • @erikstokes nice! I updated the code. Thanks – Jesse May 15 '15 at 13:26
  • You can remove the (setq deleted '()) since let variables are initialized to nil by default (and nil is identical to '()) – alexmurray May 22 '15 at 0:15
1

The following has worked well for me:

(defun python--private-lessp (x y)
  (cond
   ((and (string-prefix-p "_" x)
         (not (string-prefix-p "_" y))) nil)
   ((and (string-prefix-p "_" y)
         (not (string-prefix-p "_" x))) t)
   (t (string-lessp x y))))

(defun company-transform-python (candidates)
  "De-prioritize internal/private Python variables (e.g. 
  'var._blah') in completion list ordering.

  See `company-transformers'."
  (seq-sort-by 'company-strip-prefix 'python--private-lessp
               candidates))

(add-hook 'python-mode-hook 'company-transform-python)
(add-hook 'inferior-python-mode-hook 'company-transform-python)

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.