当前的 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