#################################################################################### Jedi - 一个出色的 Python 自动补全、静态分析和重构库 ####################################################################################
我发布了 Jedi 的继任者:一个用 Rust 构建的 Mypy 兼容 Python 语言服务器 - Zuban <https://github.com/zubanls/zuban>_
.. image:: http://isitmaintained.com/badge/open/davidhalter/jedi.svg :target: https://github.com/davidhalter/jedi/issues :alt: 开放问题和拉取请求的百分比
.. image:: http://isitmaintained.com/badge/resolution/davidhalter/jedi.svg :target: https://github.com/davidhalter/jedi/issues :alt: 解决时间是问题或拉取请求保持开放时间的中位数。
.. image:: https://github.com/davidhalter/jedi/actions/workflows/ci.yml/badge.svg?branch=master :target: https://github.com/davidhalter/jedi/actions :alt: 测试
.. image:: https://pepy.tech/badge/jedi :target: https://pepy.tech/project/jedi :alt: PyPI 下载量
Jedi 是一个用于 Python 的静态分析工具,通常用于 IDE/编辑器插件。Jedi 专注于自动补全和跳转 功能。其他功能包括重构、代码搜索和查找 引用。
Jedi 拥有一个简单的 API。有一个参考实现作为
VIM-Plugin <https://github.com/davidhalter/jedi-vim>_。在你的
REPL 中也可以使用自动补全,IPython 原生支持它,对于 CPython REPL 你
可以安装它。Jedi 经过了充分测试,bug 应该很少见。
Jedi 目前可以与以下编辑器/项目一起使用:
- Vim (jedi-vim_, YouCompleteMe_, deoplete-jedi_, completor.vim_)
Visual Studio Code_ (通过Python Extension <https://marketplace.visualstudio.com/items?itemName=ms-python.python>_)- Emacs (Jedi.el_, company-mode_, elpy_, anaconda-mode_, ycmd_)
- Sublime Text (SublimeJEDI_ [ST2 + ST3], anaconda_ [仅限 ST3])
- TextMate_ (不确定它是否真的可用)
- Kate_ 4.13+ 版本原生支持它,不过你需要启用它。 [
见 <https://projects.kde.org/projects/kde/applications/kate/repository/show?rev=KDE%2F4.13>_] - Atom_ (autocomplete-python-jedi_)
GNOME Builder_ (支持 GObject Introspection)- Gedit (gedi_)
- wdb_ - Web Debugger
Eric IDE_IPython 6.0.0+ <https://ipython.readthedocs.io/en/stable/whatsnew/version6.html>_xonsh shell <https://xon.sh/contents.html>_ 拥有jedi extension <https://xon.sh/xontribs.html#jedi>_
以及更多!
有一些使用 Jedi 的语言服务器:
jedi-language-server <https://github.com/pappasam/jedi-language-server>_python-language-server <https://github.com/palantir/python-language-server>_ (目前无人维护)python-lsp-server <https://github.com/python-lsp/python-lsp-server>_ (从 python-language-server 分叉而来)anakin-language-server <https://github.com/muffinmad/anakin-language-server>_
以下是一些取自 jedi-vim_ 的截图:
.. image:: https://github.com/davidhalter/jedi/raw/master/docs/_screenshots/screenshot_complete.png
几乎任何内容的补全:
.. image:: https://github.com/davidhalter/jedi/raw/master/docs/_screenshots/screenshot_function.png
文档:
.. image:: https://github.com/davidhalter/jedi/raw/master/docs/_screenshots/screenshot_pydoc.png
从 github <https://github.com/davidhalter/jedi>_
获取最新版本
(master 分支应该总是相对稳定/可用的)。
文档可在 https://jedi.readthedocs.org/en/latest/ <https://jedi.readthedocs.org/en/latest/>_ 获取。欢迎提交包含增强
和/或修复的 Pull requests。Jedi 使用 语义化版本控制 <https://semver.org/>_。
如果您希望保持与发布的同步,请订阅此
邮件列表:https://groups.google.com/g/jedi-announce。要订阅,您只需
向 jedi-announce+subscribe@googlegroups.com 发送一封空邮件即可。
问题与疑问
您可以在 issue tracker <https://github.com/davidhalter/jedi/> 中提交问题和疑问。或者,您也可以
在
Stack Overflow <https://stackoverflow.com/questions/tagged/python-jedi>_ 上提问,
并添加标签 python-jedi。
安装
Check out the docs <https://jedi.readthedocs.org/en/latest/docs/installation.html>_.
特性与限制
Jedi 的特性列表如下:
Features <https://jedi.readthedocs.org/en/latest/docs/features.html>_.
你可以在 Python 3.10+ 上运行 Jedi,但它也应该
能够理解比这些版本更旧的代码。此外,你应该
能够很好地使用 Virtualenvs <https://jedi.readthedocs.org/en/latest/docs/api.html#environments>_。
关于如何高效使用 Jedi 的技巧可以在 here <https://jedi.readthedocs.org/en/latest/docs/features.html#recipes>_ 找到。
API
你可以在
API here <https://jedi.readthedocs.org/en/latest/docs/api.html>_. 找到全面的文档。
自动补全 / 跳转 / 文档
以下是可用的命令:
jedi.Script.gotojedi.Script.inferjedi.Script.helpjedi.Script.completejedi.Script.get_referencesjedi.Script.get_signaturesjedi.Script.get_context
返回的对象功能非常强大,实际上可能正是你所需要的一切。
在 REPL(IPython 等)中的自动补全
Jedi 是 IPython 的一个依赖项。因此,在 IPython 中使用 Jedi 进行自动补全无需额外配置。
下面是一个 example video <https://vimeo.com/122332037>,展示了 REPL 补全
可能呈现的样子。
对于 python shell you can enable tab completion in a REPL <https://jedi.readthedocs.org/en/latest/docs/usage.html#tab-completion-in-the-python-shell>。
静态分析
对于许多形式的静态分析,你可以尝试使用
jedi.Script(...).get_names。它将返回一个名称列表,你可以
对其进行过滤和处理。还有一种方法可以列出文件中的语法错误:jedi.Script.get_syntax_errors。
重构
Jedi 支持以下重构:
jedi.Script.inlinejedi.Script.renamejedi.Script.extract_functionjedi.Script.extract_variable
代码搜索
支持使用 jedi.Script.search 进行模块搜索,以及使用 jedi.Project.search 进行项目
搜索。搜索方式可以是提供类似 foo or by using dotted syntax like foo.bar 的名称。此外,
你还可以提供 API 类型,例如 class foo.bar.Bar。还有
函数 jedi.Script.complete_search and jedi.Project.complete_search。
开发
有一份相当完善且详尽的 开发文档 <https://jedi.readthedocs.org/en/latest/docs/development.html>_。
测试
测试套件使用 pytest::
pip install pytest
如果你只想测试特定的 Python 版本(例如 Python 3.14),操作 非常简单::
python3.14 -m pytest
如需更详细的信息,请访问 测试文档 <https://jedi.readthedocs.org/en/latest/docs/testing.html>_。
致谢
非常感谢所有
contributors <https://jedi.readthedocs.org/en/latest/docs/acknowledgements.html>_!
.. _jedi-vim: https://github.com/davidhalter/jedi-vim .. _youcompleteme: https://github.com/ycm-core/YouCompleteMe .. _deoplete-jedi: https://github.com/zchee/deoplete-jedi .. _completor.vim: https://github.com/maralla/completor.vim .. _Jedi.el: https://github.com/tkf/emacs-jedi .. _company-mode: https://github.com/syohex/emacs-company-jedi .. _elpy: https://github.com/jorgenschaefer/elpy .. _anaconda-mode: https://github.com/proofit404/anaconda-mode .. _ycmd: https://github.com/abingham/emacs-ycmd .. _sublimejedi: https://github.com/srusskih/SublimeJEDI .. _anaconda: https://github.com/DamnWidget/anaconda .. _wdb: https://github.com/Kozea/wdb .. _TextMate: https://github.com/lawrenceakka/python-jedi.tmbundle .. _Kate: https://kate-editor.org .. _Atom: https://atom.io/ .. _autocomplete-python-jedi: https://atom.io/packages/autocomplete-python-jedi .. _GNOME Builder: https://wiki.gnome.org/Apps/Builder .. _Visual Studio Code: https://code.visualstudio.com/ .. _gedi: https://github.com/isamert/gedi .. _Eric IDE: https://eric-ide.python-projects.org