1

当前的 python#complete 不支持任何带有以下 import 语句的 python 脚本:

from . import module
from .modulea import abc

它将在 vim 中显示“来自:语法错误...”。

任何人有任何线索来解决它?

我今天花了一些时间通过 pythoncomplete 脚本自己解决这个问题。我能够通过 _parsedotname 函数的一些技巧来解决它。由于转换“。”的问题,我不确定我的黑客行为有多便携。进入绝对路径,但它适用于我的机器。以下是我的更改(是的,您会看到很多打印语句,我用它来理解代码流......)

def _parsedotname(self,pre=None):
    #returns (dottedname, nexttoken)
    name = []
    absolute_relative_path = False
    if pre is None:
        tokentype, token, indent = self.next()
        #print tokentype, token, indent
        if tokentype == 51 and token == '.':
            import os
            import sys
            #print os.path.abspath(os.curdir)
            fullpath = os.path.abspath(os.curdir)
            paths = fullpath.split(os.path.sep) 
            n_ = -1
            #print fullpath
            pyexeindex = sys.path.index(os.path.dirname(sys.executable))
            #print sys.path[pyexeindex:]
            while fullpath not in sys.path[pyexeindex:]:
                fullpath = os.path.sep.join(paths[:n_])
                #print fullpath
                n_ -= 1
            if fullpath == '':
                return ('', token)
            absolute_relative_path = True
            name = '.'.join(paths[n_+1:])
            #print name
        elif tokentype != NAME and token != '*':
            #print 'should not here'
            return ('', token)
    else: token = pre
    if '.' in name:
        name = name.split('.')
    else:
        name.append(token)

    while True:
        if not absolute_relative_path:
            tokentype, token, indent = self.next()
            if token != '.': break
        tokentype, token, indent = self.next()
        if not absolute_relative_path:
            if tokentype != NAME: break
        else:
            absolute_relative_path = False
            if tokentype == NAME and token == 'import':
                return (".".join(name), token)
        name.append(token)
    return (".".join(name), token)

现在,它对两者都有效:

from . import module
from .moduleA import moduleB
4

1 回答 1

1

我想你正在使用 vim internal pythoncomplete

正如我在这里写的:Python docstring with vim pythoncomplete is not display newlines for my own class functions

pythoncomplete是一个非常简单的工具,它通过执行语句来完成大部分功能import(顺便说一句,这很危险)。解决它可能不是最好的主意,因为我目前正在尝试这样做(编写一个好的python 自动完成)。

但我不认为我的版本会在一两个月内准备好做你想做的事,但它已经很远了,我准备好了再告诉你。

于 2012-05-07T19:10:31.167 回答