From fafb9c4b3ea518085f9159d195bf0dfa9bc0a7e0 Mon Sep 17 00:00:00 2001 From: Tu Do Date: Thu, 21 May 2015 12:39:57 +0700 Subject: [PATCH] Enable company in Python REPL for code completion - Company should be enabled in Python REPL for displaying completion candidates. Note that in Python REPL, we must use company-capf to display the candidates from Python REPL, since in the REPL it uses completion-at-point to display candidates from the REPL. Also, we locally set company-minimum-prefix-length to 0, so that completion starts immediately after the first character. We also need to set the idle delay a bit higher (0.5) compared with Spacemacs default (0.2), since setting it too low may cause company unable to retrieve completion candidates. - Anaconda should NOT be enabled in Python REPL for retrieving proper completion candidates. This can be tested in Python REPL: when anaconda-mode is enabled and without any prefix string before point, run the command completion-at-point (bound to C-M-i by default), completion candidates from Anaconda are displayed but NOT the ones from the real REPL. So, when you import a module i.e. os and try to access the functions in the module with "os." prefix, nothing inside the module is displayed because Anaconda does not work in Python REPL. When anaconda-mode is disabled, candidates in "os." are displayed properly with company-capf. company-anaconda backend cannot be used in Python REPL since it is buggy: it cannot retrieve any prefix before point and narrow properly. --- contrib/!lang/python/config.el | 1 + contrib/!lang/python/packages.el | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/contrib/!lang/python/config.el b/contrib/!lang/python/config.el index c5d70645b..3fea72374 100644 --- a/contrib/!lang/python/config.el +++ b/contrib/!lang/python/config.el @@ -13,6 +13,7 @@ ;; variables (spacemacs|defvar-company-backends python-mode) +(spacemacs|defvar-company-backends inferior-python-mode) (spacemacs|defvar-company-backends pip-requirements-mode) (defvar python-enable-yapf-format-on-save nil diff --git a/contrib/!lang/python/packages.el b/contrib/!lang/python/packages.el index 30c0be12e..e43c5382f 100644 --- a/contrib/!lang/python/packages.el +++ b/contrib/!lang/python/packages.el @@ -257,7 +257,12 @@ (when (configuration-layer/layer-usedp 'auto-completion) (defun python/post-init-company () - (spacemacs|add-company-hook python-mode)) + (spacemacs|add-company-hook python-mode) + (spacemacs|add-company-hook inferior-python-mode) + (push 'company-capf company-backends-inferior-python-mode) + (add-hook 'inferior-python-mode-hook (lambda () + (setq-local company-minimum-prefix-length 0) + (setq-local company-idle-delay 0.5)))) (defun python/init-company-anaconda () (use-package company-anaconda