Cleaning up evaluate.helpers.

This commit is contained in:
Dave Halter
2014-12-16 17:19:14 +01:00
parent 6821ccba91
commit 237f0e526c
2 changed files with 1 additions and 192 deletions

View File

@@ -10,7 +10,6 @@ from jedi._compatibility import next, unicode, use_metaclass
from jedi import settings from jedi import settings
from jedi import common from jedi import common
from jedi.parser import tree as pr from jedi.parser import tree as pr
from jedi.evaluate.helpers import statement_elements_in_statement
from jedi.evaluate.cache import memoize_default, CachedMetaClass from jedi.evaluate.cache import memoize_default, CachedMetaClass
from jedi.evaluate import representation as er from jedi.evaluate import representation as er
from jedi.evaluate import iterable from jedi.evaluate import iterable

View File

@@ -1,9 +1,7 @@
import copy import copy
from itertools import chain from itertools import chain
from jedi._compatibility import unicode
from jedi.parser import tree as pr from jedi.parser import tree as pr
from jedi import debug
def deep_ast_copy(obj, new_elements_default=None, check_first=False): def deep_ast_copy(obj, new_elements_default=None, check_first=False):
@@ -106,7 +104,7 @@ def deep_ast_copy(obj, new_elements_default=None, check_first=False):
for unfinished in unfinished_parents: for unfinished in unfinished_parents:
try: try:
unfinished.parent = new_elements[unfinished.parent] unfinished.parent = new_elements[unfinished.parent]
except KeyError: # TODO this keyerror is useless. except KeyError: # TODO this keyerror is useless.
pass pass
return result return result
@@ -146,143 +144,6 @@ def call_of_name(name, cut_own_trailer=False):
return par return par
def _call_signature_array_for_pos(stmt, pos):
"""
Searches for the array and position of a tuple.
Returns a tuple of (array, index-in-the-array, call).
"""
def search_array(arr, pos, origin_call=None):
accepted_types = pr.Array.TUPLE, pr.Array.NOARRAY
if arr.type == 'dict':
for stmt in arr.values + arr.keys:
tup = _call_signature_array_for_pos(stmt, pos)
if tup[0] is not None:
return tup
else:
for i, stmt in enumerate(arr):
tup = _call_signature_array_for_pos(stmt, pos)
if tup[0] is not None:
return tup
# Since we need the index, we duplicate efforts (with empty
# arrays).
if arr.start_pos < pos <= stmt.end_pos:
if arr.type in accepted_types and origin_call:
return arr, i, origin_call
if len(arr) == 0 and arr.start_pos < pos < arr.end_pos:
if arr.type in accepted_types and origin_call:
return arr, 0, origin_call
return None, 0, None
def search_call(call, pos, origin_call=None):
tup = None, 0, None
while call.next is not None and tup[0] is None:
method = search_array if isinstance(call.next, pr.Array) else search_call
# TODO This is wrong, don't call search_call again, because it will
# automatically be called by call.next.
tup = method(call.next, pos, origin_call or call)
call = call.next
return tup
if stmt.start_pos >= pos >= stmt.end_pos:
return None, 0, None
tup = None, 0, None
# TODO this is still old
for command in [] and stmt.expression_list():
if isinstance(command, pr.Array):
tup = search_array(command, pos)
elif isinstance(command, pr.StatementElement):
tup = search_call(command, pos, command)
if tup[0] is not None:
break
return tup
def scan_node_for_call_signature(node, pos):
"""to something with call_signatures"""
if node.type == 'power' and node.start_pos < pos < node.end_pos:
for i, trailer in enumerate(node.children[1:], 1):
if trailer.type == 'trailer' and trailer.children[0] == '(' \
and trailer.children[0].start_pos < pos \
and pos <= trailer.children[-1].start_pos:
# Delete all the nodes including the current one
node.children[i:] = []
return node, trailer
for child in node.children:
node, trailer = scan_node_for_call_signature(child, pos)
if node is not None:
return node, trailer
return None, None
def search_call_signatures(user_stmt, position):
"""
Returns the function Call that matches the position before.
"""
debug.speed('func_call start')
call, arr, index = None, None, 0
if user_stmt is not None and isinstance(user_stmt, pr.ExprStmt):
# some parts will of the statement will be removed
user_stmt = deep_ast_copy(user_stmt)
return scan_node_for_call_signature(user_stmt, position) + (0,)
#arr, index, call = _call_signature_array_for_pos(user_stmt, position)
# Now remove the part after the call. Including the array from the
# statement.
stmt_el = call
# TODO REMOVE this? or change?
while False and isinstance(stmt_el, pr.StatementElement):
if stmt_el.next == arr:
stmt_el.next = None
break
stmt_el = stmt_el.next
debug.speed('func_call parsed')
return call, arr, index
def scan_statement_for_calls(stmt, search_name, assignment_details=False):
""" Returns the function Calls that match search_name in an Array. """
def scan_array(arr, search_name):
result = []
if arr.type == pr.Array.DICT:
for key_stmt, value_stmt in arr.items():
result += scan_statement_for_calls(key_stmt, search_name)
result += scan_statement_for_calls(value_stmt, search_name)
else:
for stmt in arr:
result += scan_statement_for_calls(stmt, search_name)
return result
check = list(stmt.expression_list())
if assignment_details:
for expression_list, op in stmt.assignment_details:
check += expression_list
result = []
for c in check:
if isinstance(c, pr.Array):
result += scan_array(c, search_name)
elif isinstance(c, pr.Call):
s_new = c
while s_new is not None:
if isinstance(s_new, pr.Array):
result += scan_array(s_new, search_name)
else:
if search_name == unicode(s_new.name):
result.append(c)
s_new = s_new.next
elif isinstance(c, pr.ListComprehension):
for s in c.stmt, c.middle, c.input:
result += scan_statement_for_calls(s, search_name)
return result
def get_module_names(module, all_scopes): def get_module_names(module, all_scopes):
""" """
Returns a dictionary with name parts as keys and their call paths as Returns a dictionary with name parts as keys and their call paths as
@@ -295,52 +156,12 @@ def get_module_names(module, all_scopes):
return chain.from_iterable(dct.values()) return chain.from_iterable(dct.values())
def statement_elements_in_statement(stmt):
"""
Returns a list of statements. Statements can contain statements again in
Arrays.
"""
def search_stmt_el(stmt_el, stmt_els):
stmt_els.append(stmt_el)
while stmt_el is not None:
if isinstance(stmt_el, pr.Array):
for stmt in stmt_el.values + stmt_el.keys:
stmt_els.extend(statement_elements_in_statement(stmt))
stmt_el = stmt_el.next
stmt_els = []
for as_name in stmt.as_names:
# TODO This creates a custom pr.Call, we shouldn't do that.
stmt_els.append(pr.Call(as_name._sub_module, as_name,
as_name.start_pos, as_name.end_pos))
ass_items = chain.from_iterable(items for items, op in stmt.assignment_details)
for item in stmt.expression_list() + list(ass_items):
if isinstance(item, pr.StatementElement):
search_stmt_el(item, stmt_els)
elif isinstance(item, pr.ListComprehension):
for stmt in (item.stmt, item.middle, item.input):
stmt_els.extend(statement_elements_in_statement(stmt))
elif isinstance(item, pr.Lambda):
for stmt in item.params + item.returns:
stmt_els.extend(statement_elements_in_statement(stmt))
return stmt_els
class FakeSubModule(): class FakeSubModule():
line_offset = 0 line_offset = 0
parent = None parent = None
path = None path = None
class FakeArray(pr.Array):
def __init__(self, values, parent=None, arr_type=pr.Array.LIST):
p = (0, 0)
super(FakeArray, self).__init__(FakeSubModule, p, arr_type, parent)
self.values = values
class FakeStatement(pr.ExprStmt): class FakeStatement(pr.ExprStmt):
def __init__(self, values, start_pos=(0, 0), parent=None): def __init__(self, values, start_pos=(0, 0), parent=None):
self._start_pos = start_pos self._start_pos = start_pos
@@ -400,14 +221,3 @@ class LazyName(FakeName):
@parent.setter @parent.setter
def parent(self, value): def parent(self, value):
pass # Do nothing, super classes can try to set the parent. pass # Do nothing, super classes can try to set the parent.
def stmts_to_stmt(statements):
"""
Sometimes we want to have something like a result_set and unite some
statements in one.
"""
if len(statements) == 1:
return statements[0]
array = FakeArray(statements, arr_type=pr.Array.NOARRAY)
return FakeStatement([array])