Vim Python 补全

发布于 2024-11-30 03:04:12 字数 1202 浏览 2 评论 0原文

我在 Vim 和 Python 补全方面遇到问题。 事实上,我完全困惑这是如何工作的。

我有通用的 gvim 7.3,在 Windows 7 上(使用 python/dyn) 我正在使用 SuperTab 插件等,其中一些 是特定于 python 的,在 vimrc 中具有以下设置:

au FileType python set omnifunc=pythoncomplete#Complete
let g:SuperTabDefaultCompletionType = "context"
let g:SuperTabContextDefaultCompletionType = "<c-n>"

我没有设置 PYTHONPATH 环境变量。

对于系统模块来说,完成工作正常。

起初我认为它对于非系统根本不起作用 代码,但事实并非如此。 下面的代码最好地显示了发生的情况:

import numpy.random   # if this line is commented completion in last line works

class C(object):
    def __init__(self, x_):
        self.x=x_

    def getX(self):
        return self.x

    def pr(self):
        print 'ok'

a=C(10)  # nothing changes if I put C() instead, even though it would be wrong
a.  # here is completion in question

问题是,如果导入行被注释,则补全工作正常(a.建议 getX 和 pr)。但如果有 import numpy.random,完成会停止。 注意:当我运行代码时,此导入正常工作。

Python 完成的先决条件是什么? 发生了什么以及我应该做什么才能完成 Python 工作。

由于我(相对)对 Vim 很陌生,因此我们将不胜感激。

编辑: 问题似乎出在导入中使用 ab 形式。如果我执行 from numpy import random,一切都会好的。如果这个问题很容易解决,我也想让 ab 继续工作。但现在我知道了如何解决这个问题,这并不那么重要。

是否还有更多像这样的不寻常问题,以便我知道将来会发生什么?

I'm having trouble with Vim and Python completion.
In fact I'm totally confused how does this work.

I have generic gvim 7.3, on windows 7 (with python/dyn)
I'm using SuperTab plugin, amongst many others, some of which
are python-specific, with following settings in vimrc:

au FileType python set omnifunc=pythoncomplete#Complete
let g:SuperTabDefaultCompletionType = "context"
let g:SuperTabContextDefaultCompletionType = "<c-n>"

I did not set PYTHONPATH enviroment varariable.

Completion works ok for system modules.

At first I thought that it isn't working at all for non-system
code, but that's not the case.
What is happening is best shown on following code:

import numpy.random   # if this line is commented completion in last line works

class C(object):
    def __init__(self, x_):
        self.x=x_

    def getX(self):
        return self.x

    def pr(self):
        print 'ok'

a=C(10)  # nothing changes if I put C() instead, even though it would be wrong
a.  # here is completion in question

Problem is that completion works (a.<tab> suggests getX and pr) if import line is commented. But it there is import numpy.random, completion brakes down.
Note: this import works normally when I run the code.

What are prerequisites for Python completion?
What's happening and what should I do to get completion working for Python.

As I am (relatively) new to Vim, any suggestion is appreciated.

EDIT:
It seems that the problem is in using a.b form in import. If I do from numpy import random, everything is ok. If this is reasonably easy to fix I would like to get a.b from to work too. But now that I know how to go around it that's not so important.

Are there more unusual problem like this one so that I know what's happening in the future?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2024-12-07 03:04:12

pythoncomplete 相当旧且无人维护。

尝试使用 Jedi:https://github.com/davidhalter/jedi-vim
它最初是一个改进的 pythoncomplete,但现在更强大了!

它适用于复杂的代码:completion

并具有附加功能:
在此处输入图像描述

有一个所有可能功能的列表:

  • 内置函数/类支持
  • 复杂的模块/函数/类结构
  • 忽略语法和缩进错误
  • 多次返回/产生
  • 元组分配/数组索引/字典索引
  • 异常/with语句
  • *args/**kwargs
  • 装饰器
  • 描述符-> property / staticmethod / classmethod
  • 闭包
  • 生成器(yield 语句)/迭代器
  • 支持一些魔术方法:__call____iter____next__
    __get____getitem____init__
  • 支持 list.append、set.add、list.extend 等
  • (嵌套)列表推导/三元表达式
  • 相对导入
  • getattr() / __getattr__ / __getattribute__
  • 函数注释(py3k 功能,现在被忽略,但正在解析。
    我不知道如何处理它们。)
  • 类装饰器(py3k 功能,也被忽略,直到我找到一个用途
    情况下,这不适用于 Jedi)
  • 简单/通常的 sys.path 修改
  • isinstance 检查 if/while/assert

pythoncomplete is rather old and unmaintained.

Try to use Jedi: https://github.com/davidhalter/jedi-vim
It was originally an improved pythoncomplete, but is now much much more powerful!

It works for complex code:completion

And has additional features:
enter image description here

There is a list of all possible features:

  • builtin functions/classes support
  • complex module / function / class structures
  • ignores syntax and indentation errors
  • multiple returns / yields
  • tuple assignments / array indexing / dictionary indexing
  • exceptions / with-statement
  • *args / **kwargs
  • decorators
  • descriptors -> property / staticmethod / classmethod
  • closures
  • generators (yield statement) / iterators
  • support for some magic methods: __call__, __iter__, __next__,
    __get__, __getitem__, __init__
  • support for list.append, set.add, list.extend, etc.
  • (nested) list comprehensions / ternary expressions
  • relative imports
  • getattr() / __getattr__ / __getattribute__
  • function annotations (py3k feature, are ignored right now, but being parsed.
    I don't know what to do with them.)
  • class decorators (py3k feature, are being ignored too, until I find a use
    case, that doesn't work with Jedi)
  • simple/usual sys.path modifications
  • isinstance checks for if/while/assert
我偏爱纯白色 2024-12-07 03:04:12

Python 作为一种极其动态的语言,并不适合自己完成。那里没有任何真正好的完成度。在我看来,没有它生活比与它的所有问题作斗争更容易。也就是说,python-mode 确实很棒,就像 Neoascetic 所说的那样。

Python, being an incredibly dynamic language, doesn't lend itself to completion. There isn't any really good completion out there. It's easier to just live without it than to fight with all its problems, IMO. That said, python-mode really is fantastic, like neoascetic said.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文