I would like emacs-jedi to detect when I am editing files in different projects, and use the corresponding virtualenv if it is available. By convention my virtualenvs have the same name as my projects. They are located in $HOME/.virtualenvs/
I found kenobi.el but it assumes that virtualenvs are found in the bin directory in the project root. It also has a couple of other features that I don't need at all.
With inspiration from kenobi.el, I wrote the following initialisation for jedi. It works pretty well, but not perfectly.
If I import library A
from my project, and A
imports B
. I am able to jump into definitions defined by A
, but once there, I'm not able to continue jumping into definitions from B
.
My initialisation:
(defun project-directory (buffer-name)
(let ((git-dir (file-name-directory buffer-name)))
(while (and (not (file-exists-p (concat git-dir ".git")))
git-dir)
(setq git-dir
(if (equal git-dir "/")
nil
(file-name-directory (directory-file-name git-dir)))))
git-dir))
(defun project-name (buffer-name)
(let ((git-dir (project-directory buffer-name)))
(if git-dir
(file-name-nondirectory
(directory-file-name git-dir))
nil)))
(defun virtualenv-directory (buffer-name)
(let ((venv-dir (expand-file-name
(concat "~/.virtualenvs/" (project-name buffer-name)))))
(if (and venv-dir (file-exists-p venv-dir))
venv-dir
nil)))
(defun jedi-setup-args ()
(let ((venv-dir (virtualenv-directory buffer-file-name)))
(when venv-dir
(set (make-local-variable 'jedi:server-args) (list "--virtual-env" venv-dir)))))
(setq jedi:setup-keys t)
(setq jedi:complete-on-dot t)
(add-hook 'python-mode-hook 'jedi-setup-args)
(add-hook 'python-mode-hook 'jedi:setup)
What is wrong with how I initialise jedi?