def case_three(self, view_information, query_description, extra={}):
        ################################################################
        # <vertical_abstract_only_direction> [<ndir>] <big_roi> [<big_roi_sub_index>]
        ###############################################################
        build, selection, origin, definition_node = self.preliminary(
            view_information, query_description, extra)
        targets, exclusions, information = self.decode(query_description,
                                                       build)
        root, atok, m, r = build
        temporary_information = lambda x: information(x) if match_node(
            x, targets, exclusions) and generic_fix(x, atok) else None

        direction = query_description["vertical_direction"]
        ndir = query_description["ndir"]
        row, column = view_information["rowcol"](m.backward(selection)[0])

        # bug fixing
        test_result = decode_abstract_vertical(root,
                                               atok,
                                               targets,
                                               row + 1,
                                               1,
                                               direction,
                                               True,
                                               temporary_information,
                                               want_alternatives=False)
        l = search_upwards_log(origin, ast.stmt)
        if (test_result in [l[0]] + l[1]
                and row + 1 >= test_result.first_token.start[0]
                and row + 1 <= test_result.last_token.end[0]):

            ndir = ndir + 1

        result, alternatives = decode_abstract_vertical(root,
                                                        atok,
                                                        targets,
                                                        row + 1,
                                                        ndir,
                                                        direction,
                                                        True,
                                                        temporary_information,
                                                        want_alternatives=True)

        if result:
            new_definition_node = search_upwards(result, ast.FunctionDef)
            if definition_node is not new_definition_node and new_definition_node is not None:
                alternatives = tiebreak_on_lca(
                    new_definition_node, result,
                    find_all_nodes(new_definition_node, targets, exclusions))

        result, alternatives = obtain_result(result, alternatives)
        information = getattr(information, "secondary", information)
        result = information(result) if result else None
        alternatives = [information(x)
                        for x in alternatives] if alternatives else []
        return self._backward_result(result, alternatives, build)
	def handle_single(self,view_information,query_description,extra = {}):
		build, selection, origin = self._preliminary(view_information = view_information,query_description = query_description,extra = extra)
		if not  build: 
			return None,None
		root,atok,m,r = build 
		definition_nodes = [search_upwards(origin,ast.FunctionDef)] if query_description["format"]>=2 else find_all_nodes(root,ast.FunctionDef)
		if query_description["format"]>=2:
			if "vertical_direction"  in query_description:
				definition_node = definition_nodes[0]
				temporary_information = lambda x: match_node(x,ast.FunctionDef) 
				direction = query_description["vertical_direction"]
				ndir = query_description["ndir"]
				row = view_information["rowcol"](m.backward(selection)[0])[0] + 1 if definition_node is None else definition_node.first_token.start[0]
				bonus = 1 if definition_node.first_token.startpos > selection[1]  else 0
				t = decode_abstract_vertical(root,atok,(),row, ndir + bonus,direction,True,temporary_information)
				definition_nodes = [t]

		name_nodes = make_flat([get_argument_from_definition(x)  for x in definition_nodes])
		names = list(OrderedDict([(x,0)  for x in name_nodes]).keys())
		if "experimental"  in query_description:
			names = [x + "=" + x  for x in names] 
		if query_description["format"]==1:
			result = None
		else:
			mode = {
				2:"individual",
				3:"range",
			}[query_description["format"]]
			result = ",".join(decode_item_selection(names,query_description,mode,"item_index"))
		return result, names
Ejemplo n.º 3
0
	def case_two(self,view_information,query_description, extra = {}):
		################################################################	
		#		<vertical_direction> <ndir> <adjective> argument <argument_index>
		###############################################################	
		selection = self._get_selection(view_information,extra)
		vertical_direction = query_description["vertical_direction"]
		ndir = query_description["ndir"]

		if vertical_direction in ["up","down"]:
			row, column = view_information["rowcol"](m.backward(selection)[0])
			nr = max(0,row + ndir if vertical_direction=="down" else row - ndir)
			t = view_information["text_point"](nr,0)
			selection = (t,t)

		build = self.general_build if self.general_build else line_partial(selection[0])
		if not build  or not build[0] :
			return None,None
		root,atok,m,r  = build 
		selection = m.forward(selection)

		# this is the wrong but for some reason it is working:)
		if vertical_direction in ["above","below"]:
			row, column = view_information["rowcol"](m.backward(selection)[0]) 
			nr = decode_abstract_vertical(root,atok,ast.Call,row+1, ndir,vertical_direction)-1
			t = view_information["text_point"](nr,0)
			selection = (t,t)
			selection = m.forward(selection)		

		origin = nearest_node_from_offset(root,atok, selection[0]) if selection[0]==selection[1] else node_from_range(root,atok, selection)
		statement_node = search_upwards(origin,ast.stmt)


		# 
		alternative_logical_lines = find_all_nodes(statement_node.parent,selector = lambda x:match_node(x,ast.stmt) and x.first_token.start[0]==origin.first_token.start[0]
			,visit_all_levels=False)
		sharing_physical = alternative_logical_lines not in [[] ,[statement_node]]

		priority = {"root_lexical_order":1} if ( statement_node.first_token.start[0] != origin.first_token.start[0]
									or sharing_physical) else {}
		result, alternatives = self.process_line(
			q = query_description,
			root = statement_node if not sharing_physical else statement_node.parent,
			atok = atok,
			origin = None,
			select_node = None,
			tiebreaker = lambda x: tiebreak_on_lca(statement_node if not sharing_physical else statement_node.parent,origin,x),
			line = nr+1,
			priority = priority,
			constrained_space = m.forward((view_information["text_point"](nr ,0),view_information["text_point"](nr + 1,0))),
			second_tiebreaker = lambda x,y : tiebreak_on_visual(row + 1,x,y)
		)
		if sharing_physical:
			if alternatives:
				alternatives = [x  for x in alternatives if  x.first_token.start[0]==origin.first_token.start[0]]
			if result and result.first_token.start[0]!=origin.first_token.start[0]:
				result, alternatives = obtain_result(result, alternatives)
		return self._backward_result(result, alternatives,build)
    def case_four(self, view_information, query_description, extra={}):
        ################################################################
        # [smart] <vertical_abstract_only_direction> [<ndir>] <block> [<adjective>] <big_roi> [<big_roi_sub_index>]
        ###############################################################
        build, selection, origin, definition_node = self.preliminary(
            view_information, query_description, extra)
        targets, exclusions, information = self.decode(query_description,
                                                       build)
        temporary_information = lambda x: match_node(x, ast.FunctionDef)
        root, atok, m, r = build

        direction = query_description["vertical_direction"]
        ndir = query_description["ndir"]
        row = view_information["rowcol"](
            m.backward(selection)[0]
        )[0] + 1 if definition_node is root else definition_node.first_token.start[
            0]
        bonus = 1 if definition_node.first_token.startpos > selection[1] else 0
        t = decode_abstract_vertical(root, atok, targets, row, ndir + bonus,
                                     direction, True, temporary_information)
        if "nth" not in query_description:
            information = getattr(information, "secondary", information)
            selector = lambda x: match_node(x, targets, exclusions
                                            ) and generic_fix(x, build[1])
            candidates = tiebreak_on_lca(root, definition_node,
                                         find_all_nodes(t, selector=selector))
            candidates = [information(x) for x in candidates if information(x)]
            result, alternatives = obtain_result(None, candidates)
            return self._backward_result(result, alternatives, build)
        else:
            additional_parameters = {}
            result, alternatives = adjective_strategy(
                atok=atok,
                root=t,
                adjective_word=query_description["nth"],
                level_nodes=find_all_nodes(
                    t, (ast.If, ast.While, ast.For, ast.Try, ast.With,
                        ast.FunctionDef)),
                information_nodes=find_matching(
                    t, lambda x: information(x)
                    if match_node(x, targets, exclusions) else None),
                **additional_parameters)
            information = getattr(information, "secondary", information)
            result = information(result) if result else None
            alternatives = [information(x)
                            for x in alternatives] if alternatives else []
            return self._backward_result(result, alternatives, build)