4

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?

  • Why don't you file a feature request for the author of the Company backend you're using? – Dmitry Sep 1 '15 at 20:54
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))))
|improve this answer|||||
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)
|improve this answer|||||

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.