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 communityI'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:
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.
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
string-prefix-p
does what your string/starts-with
does.
– erikstokes
May 15 '15 at 1:08
(setq deleted '())
since let variables are initialized to nil
by default (and nil
is identical to '()
)
– alexmurray
May 22 '15 at 0:15
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)
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