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:
I'd like it to be sorted like this:
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
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.
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)