1
0
forked from VimPlug/jedi

Goto should not include imports that cannot be followed.

This commit is contained in:
Dave Halter
2015-04-23 02:37:22 +02:00
parent 691e5a8969
commit d04241b482
5 changed files with 29 additions and 12 deletions

View File

@@ -429,8 +429,11 @@ class Script(object):
user_scope = self._parser.user_scope() user_scope = self._parser.user_scope()
definitions = set([user_scope.name]) definitions = set([user_scope.name])
elif isinstance(user_stmt, pr.Import): elif isinstance(user_stmt, pr.Import):
s, name_part = helpers.get_on_import_stmt(self._evaluator, s, name = helpers.get_on_import_stmt(self._evaluator,
self._user_context, user_stmt) self._user_context, user_stmt)
definitions = self._evaluator.goto(name)
"""
try: try:
definitions = [s.follow(is_goto=True)[0]] definitions = [s.follow(is_goto=True)[0]]
except IndexError: except IndexError:
@@ -442,6 +445,7 @@ class Script(object):
np = import_name[0] np = import_name[0]
if not user_stmt.is_star_import() and unicode(name_part) == unicode(np): if not user_stmt.is_star_import() and unicode(name_part) == unicode(np):
definitions.append(np) definitions.append(np)
"""
else: else:
# The Evaluator.goto function checks for definitions, but since we # The Evaluator.goto function checks for definitions, but since we
# use a reverse tokenizer, we have new name_part objects, so we # use a reverse tokenizer, we have new name_part objects, so we
@@ -472,6 +476,12 @@ class Script(object):
try: try:
user_stmt = self._parser.user_stmt() user_stmt = self._parser.user_stmt()
definitions = self._goto(add_import_name=True) definitions = self._goto(add_import_name=True)
if not definitions and isinstance(user_stmt, pr.Import):
# For not defined imports (goto doesn't find something, we take
# the name as a definition. This is enough, because every name
# points to it.
definitions = [user_stmt.name_for_position(self._pos)]
if not definitions: if not definitions:
# Without a definition for a name we cannot find references. # Without a definition for a name we cannot find references.
return [] return []

View File

@@ -305,7 +305,8 @@ class Evaluator(object):
s = imports.ImportWrapper(self, name) s = imports.ImportWrapper(self, name)
for n in s.follow(is_goto=True): for n in s.follow(is_goto=True):
yield n yield n
yield name else:
yield name
stmt = name.get_definition() stmt = name.get_definition()
par = name.parent par = name.parent
@@ -340,7 +341,8 @@ class Evaluator(object):
elif isinstance(par, (pr.Param, pr.Function, pr.Class)) and par.name is name: elif isinstance(par, (pr.Param, pr.Function, pr.Class)) and par.name is name:
return [name] return [name]
elif isinstance(stmt, pr.Import): elif isinstance(stmt, pr.Import):
return imports.ImportWrapper(self, name).follow(is_goto=True) modules = imports.ImportWrapper(self, name).follow(is_goto=True)
return list(resolve_implicit_imports(modules))
elif par.type == 'dotted_name': # Is a decorator. elif par.type == 'dotted_name': # Is a decorator.
index = par.children.index(name) index = par.children.index(name)
if index > 0: if index > 0:

View File

@@ -104,20 +104,23 @@ class ImportWrapper(pr.Base):
if from_import_name is not None: if from_import_name is not None:
types = list(chain.from_iterable( types = list(chain.from_iterable(
self._evaluator.find_types(s, from_import_name, is_goto) self._evaluator.find_types(s, from_import_name, is_goto=is_goto)
for s in types)) for s in types))
if not types: if not types:
importer = get_importer(self._evaluator, importer = get_importer(self._evaluator,
tuple(import_path + [from_import_name]), tuple(import_path + [from_import_name]),
module, self._import.level) module, self._import.level)
types, _ = importer.follow_file_system() types, _ = importer.follow_file_system()
# goto only accepts `Name`
if is_goto:
types = [s.name for s in types]
else:
# goto only accepts `Name`
if is_goto:
types = [s.name for s in types]
# goto only accepts `Name`
if is_goto and not rest:
types = [s.name for s in types]
""" """
# follow the rest of the import (not FS -> classes, functions) # follow the rest of the import (not FS -> classes, functions)
if rest: if rest:
@@ -298,7 +301,7 @@ class _Importer(object):
@memoize_default(NO_DEFAULT) @memoize_default(NO_DEFAULT)
def follow_file_system(self): def follow_file_system(self):
if not self.import_path: if not self.import_path:
return None, [] return [], []
modules = self._do_import(self.import_path, self.sys_path_with_modifications()) modules = self._do_import(self.import_path, self.sys_path_with_modifications())
return modules, [] return modules, []
@@ -413,6 +416,8 @@ class _Importer(object):
else: else:
debug.dbg('search_module %s in paths %s', module_name, paths) debug.dbg('search_module %s in paths %s', module_name, paths)
for path in paths: for path in paths:
# At the moment we are only using one path. So this is
# not important to be correct.
module_file, module_path, is_pkg = \ module_file, module_path, is_pkg = \
find_module(import_parts[-1], [path]) find_module(import_parts[-1], [path])
else: else:

View File

@@ -775,7 +775,7 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, pr.Module, Wrapper)):
return name return name
def py__file__(self): def py__file__(self):
return self._module.path return os.path.abspath(self._module.path)
def py__package__(self): def py__package__(self):
if self._get_init_directory() is None: if self._get_init_directory() is None:

View File

@@ -179,7 +179,7 @@ def _get_paths_from_buildout_script(evaluator, buildout_script):
cache.save_parser(buildout_script, p) cache.save_parser(buildout_script, p)
return p.module return p.module
cached = cache.load_parser(buildout_script, None) cached = cache.load_parser(buildout_script)
module = cached and cached.module or load(buildout_script) module = cached and cached.module or load(buildout_script)
if not module: if not module:
return return