为什么当我按时 SuperTab 会输出 self?

发布于 2025-01-03 04:57:22 字数 2341 浏览 2 评论 0原文

设置

在我的 .vimrc 中,我有以下几行:

" .vimrc
let g:virtualenv_directory="/Users/Kit/Development/virtualenv"

然后在 ~/.vim/ftplugin/python/virtualenv.vim 中,我有这些:

py << EOF
import os.path
import sys
import vim
if 'VIRTUAL_ENV' in os.environ:
    project_base_dir = os.environ['VIRTUAL_ENV']
    sys.path.insert(0, project_base_dir)
    activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))
    print "virtualenv in os.environ!"
EOF
VirtualEnvActivate my-virtualenv-python-2.7

~/ .vim/ftplugin/python/virtualenv.vim 我有这些 SuperTab 设置:

setlocal omnifunc=pythoncomplete#Complete
setlocal completeopt=menuone,longest,preview
let g:SuperTabDefaultCompletionType="<c-x><c-]>"

在我经常工作的工作目录中,我执行了以下 bash 命令来生成一个 TAGS 文件全部我的 .py 文件

find . -name '*.py' -type f -print0 | xargs -0 etags -l python

问题

例如,我有一个 main.py ,其中有一个对象 app ,这样以下脚本就可以工作很好:

import main
new_app = main.app() # works totally fine Python-wise

例如,如果我编写一些新代码并尝试使用 SuperTabomnicompletion:

import main
new_new_app = main.<Tab>

这就是我得到的:

new_new_app = mainself.

如果我多次按 Tab

new_new_app = mainselfselfselfself.

什么对我有用

如果,但是,我这样做以下:

new_new_app = main.a<Tab>

我得到了完整的列表a.. 对象,其中包括不属于模块 main 的对象。

我想要什么

如果我在 .vimrc 中设置以下内容:

let g:SuperTabDefaultCompletionType="context"

然后,我使用标准 Python 库中的模块:

import sys
sys.<Tab> # This will still result in sysselfselfself.
sys.p<Tab> # This will result in the correct list of `sys` members beginning with `p`

但是 "context" 设置不适用于我的自己的模块:

new_new_app = main.a<Tab>
# Will say at the bottom: Omni completion (^O^N^P) Pattern not found

问题

我应该如何设置omnicompletion和SuperTab,以便它对我自己的模块的行为与对标准库模块的行为一样?以及消除selfselfself.烦恼?

The Setup

In my .vimrc I have the following lines:

" .vimrc
let g:virtualenv_directory="/Users/Kit/Development/virtualenv"

Then in ~/.vim/ftplugin/python/virtualenv.vim I have these:

py << EOF
import os.path
import sys
import vim
if 'VIRTUAL_ENV' in os.environ:
    project_base_dir = os.environ['VIRTUAL_ENV']
    sys.path.insert(0, project_base_dir)
    activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))
    print "virtualenv in os.environ!"
EOF
VirtualEnvActivate my-virtualenv-python-2.7

In ~/.vim/ftplugin/python/virtualenv.vim I have these SuperTab settings:

setlocal omnifunc=pythoncomplete#Complete
setlocal completeopt=menuone,longest,preview
let g:SuperTabDefaultCompletionType="<c-x><c-]>"

In my working directory, where I always work from, I executed the following bash command to generate a TAGS file for all my .py files

find . -name '*.py' -type f -print0 | xargs -0 etags -l python

The problem

For example, I have a main.py which has an object app inside it, such that the following script works fine:

import main
new_app = main.app() # works totally fine Python-wise

If, for example, I write some new code and try to use SuperTab omnicompletion:

import main
new_new_app = main.<Tab>

This is what I get:

new_new_app = mainself.

And if I press Tab several times:

new_new_app = mainselfselfselfself.

What works for me

If, however, I do the following:

new_new_app = main.a<Tab>

I get a whole list of a.. objects that include those that don't belong to module main.

What I want

If I set the following in .vimrc:

let g:SuperTabDefaultCompletionType="context"

Then, I use a module from the standard Python library:

import sys
sys.<Tab> # This will still result in sysselfselfself.
sys.p<Tab> # This will result in the correct list of `sys` members beginning with `p`

But the "context" setting won't work on my own modules:

new_new_app = main.a<Tab>
# Will say at the bottom: Omni completion (^O^N^P) Pattern not found

The Question

How should I set up omnicompletion and SuperTab so that it behaves for my own modules as for the standard library modules? As well as eliminate the selfselfself. annoyance?

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

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

发布评论

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

评论(1

灼痛 2025-01-10 04:57:22

正如您所指出的,这是由 snipmate 引起的: https://github.com/garbas/ vim-snipmate/issues/65

我也提出了一个解决方案:
https://github.com/garbas/vim-snipmate/pull/84

它没有被接受,因为 snipmate 不应该是上下文敏感的。

有两种解决方案:

  1. 拿我的 snipmate 叉子:

    https://github.com/davidhalter/vim-snipmate

    这可能不是最好的主意,因为这只是我的叉子,我没有积极维护它。

  2. 分叉 https://github.com/honza/snipmate-snippets 并删除映射对于点(不再可能使用点来完成自我)。

As you point out, this is caused by snipmate: https://github.com/garbas/vim-snipmate/issues/65

I also proposed a solution:
https://github.com/garbas/vim-snipmate/pull/84

It did not get accepted, because snipmate should not be context sensitive.

There are two solutions for this:

  1. Take my snipmate fork:

    https://github.com/davidhalter/vim-snipmate

    Which is probably not the best idea, since it's just my fork and I don't actively maintain it.

  2. Fork https://github.com/honza/snipmate-snippets and remove the mapping for the dot (Using the dot will not be possible anymore, to complete self).

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