mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-20 04:21:13 +08:00
fix completions on 'from . import variable'
This commit is contained in:
@@ -104,6 +104,14 @@ class ImportPath(parsing.Base):
|
|||||||
for i in range(self.import_stmt.relative_count - 1):
|
for i in range(self.import_stmt.relative_count - 1):
|
||||||
path = os.path.dirname(path)
|
path = os.path.dirname(path)
|
||||||
names += self.get_module_names([path])
|
names += self.get_module_names([path])
|
||||||
|
|
||||||
|
if self.import_stmt.relative_count:
|
||||||
|
rel_path = self.get_relative_path() + '/__init__.py'
|
||||||
|
try:
|
||||||
|
m = modules.Module(rel_path)
|
||||||
|
names += m.parser.module.get_defined_names()
|
||||||
|
except IOError:
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
if on_import_stmt and isinstance(scope, parsing.Module) \
|
if on_import_stmt and isinstance(scope, parsing.Module) \
|
||||||
and scope.path.endswith('__init__.py'):
|
and scope.path.endswith('__init__.py'):
|
||||||
@@ -183,24 +191,23 @@ class ImportPath(parsing.Base):
|
|||||||
evaluate.follow_statement.pop_stmt()
|
evaluate.follow_statement.pop_stmt()
|
||||||
return scopes
|
return scopes
|
||||||
|
|
||||||
|
def get_relative_path(self):
|
||||||
|
path = self.file_path
|
||||||
|
for i in range(self.import_stmt.relative_count - 1):
|
||||||
|
path = os.path.dirname(path)
|
||||||
|
return path
|
||||||
|
|
||||||
def _follow_file_system(self):
|
def _follow_file_system(self):
|
||||||
"""
|
"""
|
||||||
Find a module with a path (of the module, like usb.backend.libusb10).
|
Find a module with a path (of the module, like usb.backend.libusb10).
|
||||||
"""
|
"""
|
||||||
def get_relative_path():
|
|
||||||
module = self.import_stmt.get_parent_until()
|
|
||||||
path = os.path.abspath(module.path)
|
|
||||||
for i in range(self.import_stmt.relative_count):
|
|
||||||
path = os.path.dirname(path)
|
|
||||||
return path
|
|
||||||
|
|
||||||
def follow_str(ns_path, string):
|
def follow_str(ns_path, string):
|
||||||
debug.dbg('follow_module', ns_path, string)
|
debug.dbg('follow_module', ns_path, string)
|
||||||
path = None
|
path = None
|
||||||
if ns_path:
|
if ns_path:
|
||||||
path = ns_path
|
path = ns_path
|
||||||
elif self.import_stmt.relative_count:
|
elif self.import_stmt.relative_count:
|
||||||
path = get_relative_path()
|
path = self.get_relative_path()
|
||||||
|
|
||||||
global imports_processed
|
global imports_processed
|
||||||
imports_processed += 1
|
imports_processed += 1
|
||||||
@@ -235,9 +242,9 @@ class ImportPath(parsing.Base):
|
|||||||
if self.import_stmt.relative_count \
|
if self.import_stmt.relative_count \
|
||||||
and len(self.import_path) == 1:
|
and len(self.import_path) == 1:
|
||||||
# follow `from . import some_variable`
|
# follow `from . import some_variable`
|
||||||
|
rel_path = self.get_relative_path()
|
||||||
try:
|
try:
|
||||||
current_namespace = follow_str(get_relative_path(),
|
current_namespace = follow_str(rel_path, '__init__')
|
||||||
'__init__')
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
if current_namespace[1]:
|
if current_namespace[1]:
|
||||||
|
|||||||
@@ -23,8 +23,11 @@ class Module(builtin.CachedModule):
|
|||||||
:param path: The module path of the file.
|
:param path: The module path of the file.
|
||||||
:param source: The source code of the file.
|
:param source: The source code of the file.
|
||||||
"""
|
"""
|
||||||
def __init__(self, path, source):
|
def __init__(self, path, source=None):
|
||||||
super(Module, self).__init__(path=path)
|
super(Module, self).__init__(path=path)
|
||||||
|
if source is None:
|
||||||
|
with open(path) as f:
|
||||||
|
source = f.read()
|
||||||
self.source = source_to_unicode(source)
|
self.source = source_to_unicode(source)
|
||||||
self._line_cache = None
|
self._line_cache = None
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user