6

enter image description here

If i select first option it will become

l.remove

But as the completion candidate is function type, how can i make it to

l.remove()

so that i don't have to manually type parens?

Chillar Anand
  • 4,042
  • 1
  • 23
  • 52

2 Answers2

4

I'm not very familiar with the innards of Company, but here's how to do it with Ivy (you can re-use some code for Company as well, if you know how):

(defun counsel-jedi ()
  "Python completion at point."
  (interactive)
  (let ((bnd (bounds-of-thing-at-point 'symbol)))
    (if bnd
        (progn
          (setq counsel-completion-beg (car bnd))
          (setq counsel-completion-end (cdr bnd)))
      (setq counsel-completion-beg nil)
      (setq counsel-completion-end nil)))
  (deferred:sync!
   (jedi:complete-request))
  (ivy-read "Symbol name: " (jedi:ac-direct-matches)
            :action #'counsel--py-action))

(defun counsel--py-action (symbol)
  "Insert SYMBOL, erasing the previous one."
  (when (stringp symbol)
    (when counsel-completion-beg
      (delete-region
       counsel-completion-beg
       counsel-completion-end))
    (setq counsel-completion-beg
          (move-marker (make-marker) (point)))
    (insert symbol)
    (setq counsel-completion-end
          (move-marker (make-marker) (point)))
    (when (equal (get-text-property 0 'symbol symbol) "f")
      (insert "()")
      (backward-char 1))))
abo-abo
  • 13,943
  • 1
  • 29
  • 43
1

It will tricky to do this generally since company itself does not care about semantics of completion candidates as such the method to detect the whether a symbol is a function would be backend specific.

You can add a hook to company-completion-finished-hook which inspects the candidate inserted and adds the parens if needed. As an example the following code will work with company-jedi

(defun my-setup-python-mode ()
  ;; Make sure we add to hook locally so that we do not mess up completion in other major-modes
  (add-hook 'company-completion-finished-hook #'my-company-insert-parens-function t))

(defun my-company-insert-parens-function (candidate)
  ;; This part will be different for different backends
  (when (string= (plist-get (text-properties-at 0 candidate) :symbol) "f")
    (insert "()")
    (backward-char)))

(add-hook 'python-mode #'my-setup-python-mode)
Iqbal Ansari
  • 7,468
  • 1
  • 28
  • 31