1
0
forked from VimPlug/jedi

All modules now have a code_lines attribute, see #1062

This commit is contained in:
Dave Halter
2018-03-16 10:20:14 +01:00
parent 24e1f7e6f0
commit 90a226f898
11 changed files with 102 additions and 33 deletions
+22 -12
View File
@@ -5,6 +5,8 @@ Used only for REPL Completion.
import inspect
import os
from jedi.parser_utils import get_cached_code_lines
from jedi import settings
from jedi.evaluate import compiled
from jedi.cache import underscore_memoization
@@ -140,10 +142,10 @@ def _find_syntax_node_name(evaluator, access_handle):
path = inspect.getsourcefile(python_object)
except TypeError:
# The type might not be known (e.g. class_with_dict.__weakref__)
return None, None, None
return None
if path is None or not os.path.exists(path):
# The path might not exist or be e.g. <stdin>.
return None, None, None
return None
module_node = _load_module(evaluator, path)
@@ -151,22 +153,23 @@ def _find_syntax_node_name(evaluator, access_handle):
# We don't need to check names for modules, because there's not really
# a way to write a module in a module in Python (and also __name__ can
# be something like ``email.utils``).
return module_node, module_node, path
code_lines = get_cached_code_lines(evaluator.grammar, path)
return module_node, module_node, path, code_lines
try:
name_str = python_object.__name__
except AttributeError:
# Stuff like python_function.__code__.
return None, None, None
return None
if name_str == '<lambda>':
return None, None, None # It's too hard to find lambdas.
return None # It's too hard to find lambdas.
# Doesn't always work (e.g. os.stat_result)
try:
names = module_node.get_used_names()[name_str]
except KeyError:
return None, None, None
return None
names = [n for n in names if n.is_definition()]
try:
@@ -183,29 +186,36 @@ def _find_syntax_node_name(evaluator, access_handle):
# There's a chance that the object is not available anymore, because
# the code has changed in the background.
if line_names:
return module_node, line_names[-1].parent, path
names = line_names
code_lines = get_cached_code_lines(evaluator.grammar, path)
# It's really hard to actually get the right definition, here as a last
# resort we just return the last one. This chance might lead to odd
# completions at some points but will lead to mostly correct type
# inference, because people tend to define a public name in a module only
# once.
return module_node, names[-1].parent, path
return module_node, names[-1].parent, path, code_lines
@compiled_objects_cache('mixed_cache')
def _create(evaluator, access_handle, parent_context, *args):
module_node, tree_node, path = _find_syntax_node_name(evaluator, access_handle)
compiled_object = create_cached_compiled_object(
evaluator, access_handle, parent_context=parent_context.compiled_object)
if tree_node is None:
result = _find_syntax_node_name(evaluator, access_handle)
if result is None:
return compiled_object
module_node, tree_node, path, code_lines = result
if parent_context.tree_node.get_root_node() == module_node:
module_context = parent_context.get_root_context()
else:
module_context = ModuleContext(evaluator, module_node, path=path)
module_context = ModuleContext(
evaluator, module_node,
path=path,
code_lines=code_lines,
)
# TODO this __name__ is probably wrong.
name = compiled_object.get_root_context().py__name__()
imports.add_module(evaluator, name, module_context)