forked from VimPlug/jedi
Start implementing the bulk of the context/value separation
This commit is contained in:
@@ -192,6 +192,9 @@ class Script(object):
|
||||
self._inference_state.module_cache.add(names, ValueSet([module]))
|
||||
return module
|
||||
|
||||
def _get_module_context(self):
|
||||
return self._get_module().as_context()
|
||||
|
||||
def __repr__(self):
|
||||
return '<%s: %s %r>' % (
|
||||
self.__class__.__name__,
|
||||
@@ -209,7 +212,7 @@ class Script(object):
|
||||
"""
|
||||
with debug.increase_indent_cm('completions'):
|
||||
completion = Completion(
|
||||
self._inference_state, self._get_module(), self._code_lines,
|
||||
self._inference_state, self._get_module_context(), self._code_lines,
|
||||
self._pos, self.call_signatures
|
||||
)
|
||||
return completion.completions()
|
||||
@@ -239,9 +242,9 @@ class Script(object):
|
||||
if leaf is None:
|
||||
return []
|
||||
|
||||
value = self._inference_state.create_value(self._get_module(), leaf)
|
||||
context = self._inference_state.create_context(self._get_module_context(), leaf)
|
||||
|
||||
values = helpers.infer_goto_definition(self._inference_state, value, leaf)
|
||||
values = helpers.infer_goto_definition(self._inference_state, context, leaf)
|
||||
values = convert_values(
|
||||
values,
|
||||
only_stubs=only_stubs,
|
||||
@@ -299,8 +302,8 @@ class Script(object):
|
||||
# Without a name we really just want to jump to the result e.g.
|
||||
# executed by `foo()`, if we the cursor is after `)`.
|
||||
return self.goto_definitions(only_stubs=only_stubs, prefer_stubs=prefer_stubs)
|
||||
value = self._inference_state.create_value(self._get_module(), tree_name)
|
||||
names = list(self._inference_state.goto(value, tree_name))
|
||||
context = self._inference_state.create_context(self._get_module_context(), tree_name)
|
||||
names = list(self._inference_state.goto(context, tree_name))
|
||||
|
||||
if follow_imports:
|
||||
names = filter_follow_imports(names, lambda name: name.is_import())
|
||||
@@ -340,7 +343,7 @@ class Script(object):
|
||||
# Must be syntax
|
||||
return []
|
||||
|
||||
names = usages.usages(self._get_module(), tree_name)
|
||||
names = usages.usages(self._get_module_context(), tree_name)
|
||||
|
||||
definitions = [classes.Definition(self._inference_state, n) for n in names]
|
||||
if not include_builtins:
|
||||
@@ -368,8 +371,8 @@ class Script(object):
|
||||
if call_details is None:
|
||||
return []
|
||||
|
||||
value = self._inference_state.create_value(
|
||||
self._get_module(),
|
||||
value = self._inference_state.create_context(
|
||||
self._get_module_context(),
|
||||
call_details.bracket_leaf
|
||||
)
|
||||
definitions = helpers.cache_call_signatures(
|
||||
@@ -389,10 +392,10 @@ class Script(object):
|
||||
def _analysis(self):
|
||||
self._inference_state.is_analysis = True
|
||||
self._inference_state.analysis_modules = [self._module_node]
|
||||
module = self._get_module()
|
||||
module = self._get_module_context()
|
||||
try:
|
||||
for node in get_executable_nodes(self._module_node):
|
||||
value = module.create_value(node)
|
||||
value = module.create_context(node)
|
||||
if node.type in ('funcdef', 'classdef'):
|
||||
# Resolve the decorators.
|
||||
tree_name_to_values(self._inference_state, value, node.children[1])
|
||||
@@ -505,13 +508,13 @@ def names(source=None, path=None, encoding='utf-8', all_scopes=False,
|
||||
else:
|
||||
cls = TreeNameDefinition
|
||||
return cls(
|
||||
module_value.create_value(name),
|
||||
module_context.create_context(name),
|
||||
name
|
||||
)
|
||||
|
||||
# Set line/column to a random position, because they don't matter.
|
||||
script = Script(source, line=1, column=0, path=path, encoding=encoding, environment=environment)
|
||||
module_value = script._get_module()
|
||||
module_context = script._get_module_context()
|
||||
defs = [
|
||||
classes.Definition(
|
||||
script._inference_state,
|
||||
|
||||
@@ -52,11 +52,11 @@ def filter_names(inference_state, completion_names, stack, like_name):
|
||||
yield new
|
||||
|
||||
|
||||
def get_user_scope(module_value, position):
|
||||
def get_user_context(module_context, position):
|
||||
"""
|
||||
Returns the scope in which the user resides. This includes flows.
|
||||
"""
|
||||
user_stmt = get_statement_of_position(module_value.tree_node, position)
|
||||
user_stmt = get_statement_of_position(module_context.tree_node, position)
|
||||
if user_stmt is None:
|
||||
def scan(scope):
|
||||
for s in scope.children:
|
||||
@@ -68,12 +68,12 @@ def get_user_scope(module_value, position):
|
||||
return scan(s)
|
||||
return None
|
||||
|
||||
scanned_node = scan(module_value.tree_node)
|
||||
scanned_node = scan(module_context.tree_node)
|
||||
if scanned_node:
|
||||
return module_value.create_value(scanned_node, node_is_value=True)
|
||||
return module_value
|
||||
return module_context.create_context(scanned_node, node_is_value=True)
|
||||
return module_context
|
||||
else:
|
||||
return module_value.create_value(user_stmt)
|
||||
return module_context.create_context(user_stmt)
|
||||
|
||||
|
||||
def get_flow_scope_node(module_node, position):
|
||||
@@ -85,10 +85,11 @@ def get_flow_scope_node(module_node, position):
|
||||
|
||||
|
||||
class Completion:
|
||||
def __init__(self, inference_state, module, code_lines, position, call_signatures_callback):
|
||||
def __init__(self, inference_state, module_context, code_lines, position,
|
||||
call_signatures_callback):
|
||||
self._inference_state = inference_state
|
||||
self._module_value = module
|
||||
self._module_node = module.tree_node
|
||||
self._module_context = module_context
|
||||
self._module_node = module_context.tree_node
|
||||
self._code_lines = code_lines
|
||||
|
||||
# The first step of completions is to get the name
|
||||
@@ -104,7 +105,7 @@ class Completion:
|
||||
string, start_leaf = _extract_string_while_in_string(leaf, self._position)
|
||||
if string is not None:
|
||||
completions = list(file_name_completions(
|
||||
self._inference_state, self._module_value, start_leaf, string,
|
||||
self._inference_state, self._module_context, start_leaf, string,
|
||||
self._like_name, self._call_signatures_callback,
|
||||
self._code_lines, self._original_position
|
||||
))
|
||||
@@ -237,7 +238,7 @@ class Completion:
|
||||
yield keywords.KeywordName(self._inference_state, k)
|
||||
|
||||
def _global_completions(self):
|
||||
value = get_user_scope(self._module_value, self._position)
|
||||
value = get_user_context(self._module_context, self._position)
|
||||
debug.dbg('global completion scope: %s', value)
|
||||
flow_scope_node = get_flow_scope_node(self._module_node, self._position)
|
||||
filters = get_global_filters(
|
||||
@@ -252,9 +253,9 @@ class Completion:
|
||||
return completion_names
|
||||
|
||||
def _trailer_completions(self, previous_leaf):
|
||||
user_value = get_user_scope(self._module_value, self._position)
|
||||
inferred_value = self._inference_state.create_value(
|
||||
self._module_value, previous_leaf
|
||||
user_value = get_user_context(self._module_context, self._position)
|
||||
inferred_value = self._inference_state.create_context(
|
||||
self._module_context, previous_leaf
|
||||
)
|
||||
values = infer_call_of_leaf(inferred_value, previous_leaf)
|
||||
completion_names = []
|
||||
@@ -276,7 +277,7 @@ class Completion:
|
||||
|
||||
def _get_importer_names(self, names, level=0, only_modules=True):
|
||||
names = [n.value for n in names]
|
||||
i = imports.Importer(self._inference_state, names, self._module_value, level)
|
||||
i = imports.Importer(self._inference_state, names, self._module_context, level)
|
||||
return i.completion_names(self._inference_state, only_modules=only_modules)
|
||||
|
||||
def _get_class_value_completions(self, is_function=True):
|
||||
@@ -287,7 +288,7 @@ class Completion:
|
||||
cls = tree.search_ancestor(leaf, 'classdef')
|
||||
if isinstance(cls, (tree.Class, tree.Function)):
|
||||
# Complete the methods that are defined in the super classes.
|
||||
random_value = self._module_value.create_value(
|
||||
random_value = self._module_context.create_context(
|
||||
cls,
|
||||
node_is_value=True
|
||||
)
|
||||
|
||||
@@ -7,12 +7,12 @@ from jedi.inference.helpers import get_str_or_none
|
||||
from jedi.parser_utils import get_string_quote
|
||||
|
||||
|
||||
def file_name_completions(inference_state, module_value, start_leaf, string,
|
||||
def file_name_completions(inference_state, module_context, start_leaf, string,
|
||||
like_name, call_signatures_callback, code_lines, position):
|
||||
# First we want to find out what can actually be changed as a name.
|
||||
like_name_length = len(os.path.basename(string) + like_name)
|
||||
|
||||
addition = _get_string_additions(module_value, start_leaf)
|
||||
addition = _get_string_additions(module_context, start_leaf)
|
||||
if addition is None:
|
||||
return
|
||||
string = addition + string
|
||||
@@ -25,7 +25,7 @@ def file_name_completions(inference_state, module_value, start_leaf, string,
|
||||
sigs = call_signatures_callback()
|
||||
is_in_os_path_join = sigs and all(s.full_name == 'os.path.join' for s in sigs)
|
||||
if is_in_os_path_join:
|
||||
to_be_added = _add_os_path_join(module_value, start_leaf, sigs[0].bracket_start)
|
||||
to_be_added = _add_os_path_join(module_context, start_leaf, sigs[0].bracket_start)
|
||||
if to_be_added is None:
|
||||
is_in_os_path_join = False
|
||||
else:
|
||||
@@ -60,7 +60,7 @@ def file_name_completions(inference_state, module_value, start_leaf, string,
|
||||
)
|
||||
|
||||
|
||||
def _get_string_additions(module_value, start_leaf):
|
||||
def _get_string_additions(module_context, start_leaf):
|
||||
def iterate_nodes():
|
||||
node = addition.parent
|
||||
was_addition = True
|
||||
@@ -77,7 +77,7 @@ def _get_string_additions(module_value, start_leaf):
|
||||
addition = start_leaf.get_previous_leaf()
|
||||
if addition != '+':
|
||||
return ''
|
||||
value = module_value.create_value(start_leaf)
|
||||
value = module_context.create_context(start_leaf)
|
||||
return _add_strings(value, reversed(list(iterate_nodes())))
|
||||
|
||||
|
||||
@@ -104,14 +104,14 @@ class FileName(AbstractArbitraryName):
|
||||
is_value_name = False
|
||||
|
||||
|
||||
def _add_os_path_join(module_value, start_leaf, bracket_start):
|
||||
def _add_os_path_join(module_context, start_leaf, bracket_start):
|
||||
def check(maybe_bracket, nodes):
|
||||
if maybe_bracket.start_pos != bracket_start:
|
||||
return None
|
||||
|
||||
if not nodes:
|
||||
return ''
|
||||
value = module_value.create_value(nodes[0])
|
||||
value = module_context.create_context(nodes[0])
|
||||
return _add_strings(value, nodes, add_slash=True) or ''
|
||||
|
||||
if start_leaf.type == 'error_leaf':
|
||||
|
||||
Reference in New Issue
Block a user