def _backward_result(self, result, alternatives, build, individually=False): if build and build[0]: m = build[2] atok = build[1] if individually: result = [ m.backward(get_source_region(atok, x)) if x else None for x in result ] else: result = m.backward(get_source_region( atok, result)) if result else None if alternatives: alternatives = [ m.backward(get_source_region(atok, x)) for x in alternatives ] else: alternatives = [] return result, alternatives else: return None, None
def _backward_result(self, result, alternatives, build): if build and build[0]: m = build[2] atok = build[1] result = m.backward(get_source_region(atok, result)) if result else None #self._get_selection(view_information,extra) alternatives = [ m.backward(get_source_region(atok, x)) for x in alternatives ] return result, alternatives else: return None, None
def select_region_from_node_up(root, atok, node, target, attribute, outside_level, index=None): return get_source_region( select_from_node(root, atok, node, target, attribute, outside_level, index))
def get_statement(self, origin, atok): # print("\norigin\n",ast.dump(origin)) self.global_constrained_space = None candidate_statement = search_upwards(origin, ast.stmt) big = (ast.If, ast.While, ast.For, ast.FunctionDef, ast.With, ast.ClassDef, ast.Try, ast.ExceptHandler) if match_node(candidate_statement, big): candidate_statement = search_upwards_for_parent(origin, ast.stmt) candidate_statement = candidate_statement if candidate_statement else search_upwards( origin, ast.stmt) if match_node(candidate_statement, big): region = get_source_region( atok, get_weak_header(candidate_statement, atok)) if region: self.global_constrained_space = region return candidate_statement
def node_from_range_new(root, atok, r, special=False, lenient=False): # notes like ast.Store result in (0,0) with atok.get_text_range() # which causes problems if the cursor is right at the beginning of the file inside = lambda x, y: (y[0] <= x[0] < y[1] and y[0] < x[1] <= y[1] and not y[0] == y[1] == 0) if lenient: inside = lambda x, y: (y[0] <= x[0] <= y[1] and y[0] <= x[1] <= y[1] and not y[0] == y[1] == 0) generic_fix(root, atok) for child in ast.iter_child_nodes(root): # print(" just to check something out",child,atok.get_text_range(child)) # print(" and the child fields are ",child._fields) if inside(r, atok.get_text_range(child)): # print(" success with",child,"special = ",special) return node_from_range_new(child, atok, r, special, lenient) if special: # print("Special:\n",ast.dump(root)) if match_node(root, (ast.Tuple, ast.List, ast.Set, ast.Dict, ast.DictComp)): # print("Inside Here After Special") temporary = get_sub_index(root, None) l = [ x for x in temporary for y in [get_source_region(atok, x)] if inside((r[0], r[0]), y) or inside(( r[1], r[1]), y) or inside(y, r) ] # print("temporary:\n",temporary) # print("L:\n",l) if l and l != temporary: return l if match_node(root, (ast.FunctionDef, ast.ClassDef)): converter = atok._line_numbers sr, sc = converter.offset_to_line(r[0]) er, ec = converter.offset_to_line(r[1]) for decorator in root.decorator_list: if sr != decorator.first_token.start[0]: continue if er != decorator.last_token.start[0]: if er != decorator.last_token.start[0] + 1 or ec != 0: continue return decorator return root
def case_one(self, v, q): """ <adjective> argument <argument_index> """ selection = v["selection"] build = self.general_build if self.general_build else line_partial( selection[0]) print(build) if not build: return None, None root, atok, m, r = build selection = m.forward(selection) # after build we set up some variables result = None alternatives = None origin = nearest_node_from_offset(root, atok, selection[0]) calling_node = search_upwards(origin, ast.Call) statement_node, callers = search_upwards_log(origin, ast.stmt, log_targets=ast.Call) information = lambda x: info.get_argument_from_call( x, q["argument_index"] - 1) candidates = sorted_by_source_region( atok, find_matching(statement_node, info.identity(information))) every_caller = find_all_nodes(statement_node, (ast.Call)) ################################################################ # if no adjective is given ################################################################ if q["adjective"] == "None": if calling_node: result = information(calling_node) candidates = [ information(x) for x in tiebreak_on_lca(statement_node, origin, candidates) if x != calling_node ] result, alternatives = obtain_result(result, candidates) ################################################################ # adjective is even ################################################################ if q["adjective"] != "None": additional_parameters = {} if selection[0] != selection[1]: small_root = node_from_range(root, atok, selection) additional_parameters["small_root"] = small_root print("dumping the small root\n", ast.dump(small_root)) additional_parameters["special"] = [calling_node] if calling_node: print(ast.dump(calling_node)) additional_parameters["penalized"] = [calling_node] result, alternatives = adjective_strategy( atok=atok, root=root, adjective_word=q["adjective"], level_nodes=every_caller, information_nodes=candidates, **additional_parameters) result = information(result) if result else m.forward( v["selection"]) alternatives = [information(x) for x in alternatives] if alternatives else [] # translate those nodes back to offsets and forward them through the modification under print("\n\nnow finally printing result and alternatives\n\n") print(result) print( ast.dump(result) if isinstance(result, ast.AST ) else " not known node") print(alternatives) result = m.backward(get_source_region(atok, result)) alternatives = [ m.backward(get_source_region(atok, x)) for x in alternatives ] return result, alternatives
def process_line(self, q, root, atok, origin=None, select_node=None, tiebreaker=lambda x: x, line=None, transformation=None, inverse_transformation=None, priority={}, constrained_space=(), second_tiebreaker=None, invert_then_tiebreak=True): result = None alternatives = None additional_parameters = {"priority": priority} if constrained_space: additional_parameters["constrained_space"] = constrained_space information = self.get_information(q) information_nodes = sorted_by_source_region( atok, find_matching(root, information)) if origin: calling_node = search_upwards(origin, ast.Call) statement_node, callers = search_upwards_log(origin, ast.stmt, log_targets=ast.Call) every_caller = find_all_nodes(root, (ast.Call)) additional = find_all_nodes(root, (ast.Tuple, ast.List, ast.ListComp)) if additional: additional_parameters["additional_level_nodes"] = additional additional_parameters["try_alternative"] = True ################################################################ # go to handle the special Casey's ################################################################ if transformation: temporary = {transformation(x) for x in information_nodes} information_nodes = sorted_by_source_region( atok, [x for x in temporary if x]) ################################################################ # if no adjective is given ################################################################ if "nth" not in q: if origin and calling_node: result = calling_node if calling_node in information_nodes else None information_nodes = [ x for x in tiebreaker(information_nodes) if x is not calling_node ] else: result = None information_nodes = [x for x in tiebreaker(information_nodes)] result, alternatives = obtain_result(result, information_nodes) ################################################################ # adjective is given ################################################################ if "nth" in q: additional_parameters["small_root"] = select_node if origin and calling_node: additional_parameters["penalized"] = [ calling_node ] if calling_node else [] additional_parameters["special"] = [calling_node] result, alternatives = adjective_strategy( atok=atok, root=root, adjective_word=q["nth"], level_nodes=every_caller, information_nodes=information_nodes, **additional_parameters) ############################################################## # reverse transformation if any ############################################################## if transformation: helpful = [result] if result else [] if alternatives: helpful.extend(alternatives) if invert_then_tiebreak: temporary = make_flat( [tiebreaker(inverse_transformation(x)) for x in helpful]) else: temporary = tiebreaker( make_flat([inverse_transformation(x) for x in helpful])) result, alternatives = obtain_result(None, temporary) ################################################################ # Extract information ################################################################ if line and result and alternatives: temporary = [result] + alternatives temporary = [(i, x) for i, x in enumerate(temporary)] temporary = sorted( temporary, key=lambda x: (atok._line_numbers.offset_to_line( get_source_region(atok, x[1])[0])[0] != line, x[0])) temporary = [x[1] for x in temporary] result, alternatives = obtain_result(None, temporary) if result and second_tiebreaker: alternatives = second_tiebreaker(result, alternatives) if self.global_constrained_space: inside = lambda x, y: (y[0] <= x[0] < y[1] and y[0] < x[1] <= y[1]) result = result if inside(get_source_region(atok, result), self.global_constrained_space) else None alternatives = [ x for x in alternatives if inside( get_source_region(atok, x), self.global_constrained_space) ] result, alternatives = obtain_result(result, alternatives) result = information(result) if result else None alternatives = [information(x) for x in alternatives] if alternatives else [] return result, alternatives