13

我希望 emacs-jedi 检测我何时在不同项目中编辑文件,并使用相应的 virtualenv(如果可用)。按照惯例,我的 virtualenvs 与我的项目具有相同的名称。它们位于$HOME/.virtualenvs/

我找到了 kenobi.el,但它假设 virtualenvs 位于项目根目录的 bin 目录中。它还有一些我根本不需要的其他功能。

在 kenobi.el 的启发下,我为绝地编写了以下初始化。它工作得很好,但并不完美。

如果我A从我的项目中导入库,然后A导入B. 我可以跳转到 定义的定义A,但是一旦到了那里,我就无法继续从 跳转到定义B

我的初始化:

(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)

我如何初始化绝地有什么问题?

4

1 回答 1

14

我现在已经确定了一个使用virtualenvwrapper ELPA 包来激活 virtualenvs 的解决方案,允许 emacs-jedi 从 VIRTUAL_ENV 环境变量中获取 virtualenv 路径。

这是一个完整的、工作的、emacs-jedi 初始化:

(defun project-directory (buffer-name)
  "Return the root directory of the project that contain the
given BUFFER-NAME. Any directory with a .git or .jedi file/directory
is considered to be a project root."
  (interactive)
  (let ((root-dir (file-name-directory buffer-name)))
    (while (and root-dir
                (not (file-exists-p (concat root-dir ".git")))
                (not (file-exists-p (concat root-dir ".jedi"))))
      (setq root-dir
            (if (equal root-dir "/")
                nil
              (file-name-directory (directory-file-name root-dir)))))
    root-dir))

(defun project-name (buffer-name)
  "Return the name of the project that contain the given BUFFER-NAME."
  (let ((root-dir (project-directory buffer-name)))
    (if root-dir
        (file-name-nondirectory
         (directory-file-name root-dir))
      nil)))

(defun jedi-setup-venv ()
  "Activates the virtualenv of the current buffer."
  (let ((project-name (project-name buffer-file-name)))
    (when project-name (venv-workon project-name))))

(setq jedi:setup-keys t)
(setq jedi:complete-on-dot t)
(add-hook 'python-mode-hook 'jedi-setup-venv)
(add-hook 'python-mode-hook 'jedi:setup)

请记住,您必须先安装 virtualenvwrapper。

阅读virtualenvwrapper 文档,了解自动激活项目虚拟环境的另一种方法。简而言之,您可以.dir-locals.el在项目的根目录中创建一个文件,其内容如下:

((python-mode . ((project-venv-name . "myproject-env"))))

更改"myproject-env"为您的 virtualenv 的名称并使用python-mode钩子激活虚拟环境:

(add-hook 'python-mode-hook (lambda ()
                              (hack-local-variables)
                              (venv-workon project-venv-name)))
(add-hook 'python-mode-hook 'jedi:setup)
于 2014-01-20T23:28:32.133 回答