def preliminary(self, view_information, query_description, extra={}):
     selection = self._get_selection(view_information, extra)
     build = self.general_build
     if not build or not build[0]:
         return None, None, None, None
     root, atok, m, r = build
     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)
     definition_node = search_upwards(
         origin, ast.FunctionDef)  # ,aybe need to change that in the future
     # in order to find the outermost function.
     if definition_node and definition_node.first_token.startpos > selection[
             1]:
         token = atok.get_token_from_offset(selection[0])
         while token.string.isspace():
             token = atok.prev_token(token)
         s = token.startpos
         origin = nearest_node_from_offset(root, atok, s)
         definition_node = search_upwards(origin, ast.FunctionDef)
     definition_node = (
         definition_node
         if definition_node and query_description["big_roi"] not in [
             "import statement", "import module", "import value",
             "class name", "base class", "decorator"
         ] else root)
     return build, selection, origin, definition_node
Example #2
0
    def case_three(self, view_information, query_description, extra={}):
        ################################################################
        #		<adjective> inside <level_index> argument <argument_index>
        ###############################################################
        selection = self._get_selection(view_information, extra)
        build = self.general_build if self.general_build else line_partial(
            self.code, selection[0])
        if not build or not build[0]:
            return None, None
        root, atok, m, r = build
        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 = self.get_statement(origin, atok)

        ###############################################################
        # transformations
        ################################################################
        temporary = {}
        lca = LCA(statement_node)

        def transformation(node):
            calling_parent = search_upwards_for_parent(node, ast.Call)
            calling_parent = calling_parent.parent if calling_parent else None

            if not calling_parent:
                return None
            field, field_index = lca.get_field_with_respect_to(
                node, calling_parent)
            if query_description[
                    "level_index"] == 0 or correspond_to_index_in_call(
                        calling_parent, query_description["level_index"] - 1,
                        field, field_index):
                if calling_parent not in temporary:
                    temporary[calling_parent] = []
                temporary[calling_parent].append(node)
                return calling_parent
            else:
                return None

        def inverse_transformation(node):
            if node in temporary:
                return temporary[node]
            else:
                return None

        result, alternatives = self.process_line(
            q=query_description,
            root=statement_node,
            atok=atok,
            origin=origin,
            select_node=origin if selection[0] != selection[1] else None,
            tiebreaker=lambda x: tiebreak_on_lca(statement_node, origin, x, lca
                                                 ),
            transformation=transformation,
            inverse_transformation=inverse_transformation,
        )
        return self._backward_result(result, alternatives, build)
Example #3
0
 def case_five(self, view_information, query_description, extra={}):
     ################################################################
     #		<level> [<level_index>] <adjective> (argument <argument_index>|caller [<sub_index>])
     ###############################################################
     selection = self._get_selection(view_information, extra)
     build = self.general_build if self.general_build else line_partial(
         self.code, selection[0])
     if not build or not build[0]:
         return None, None
     root, atok, m, r = build
     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 = self.get_statement(origin, atok)
     priority = {}
     if query_description["level_index"] == 0:
         query_description["level_index"] = -1
     _, calling_parents = search_upwards_log(origin,
                                             targets=ast.stmt,
                                             log_targets=(ast.Call))
     index = query_description["level_index"]
     if index < len(calling_parents):
         priority["child_level"] = 1
         origin = calling_parents[index]
     result, alternatives = self.process_line(
         q=query_description,
         root=statement_node,
         atok=atok,
         origin=origin,
         select_node=origin if selection[0] != selection[1] else None,
         tiebreaker=lambda x: tiebreak_on_lca(statement_node, origin, x),
         priority=priority)
     return self._backward_result(result, alternatives, build)
Example #4
0
    def case_one(self, view_information, query_description, extra={}):
        ################################################################
        #		<adjective> argument <argument_index>
        ###############################################################
        selection = self._get_selection(view_information, extra)
        build = self.general_build if self.general_build else line_partial(
            self.code, selection[0])
        if not build or not build[0]:
            return None, None
        root, atok, m, r = build

        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 = self.get_statement(origin, atok)
        result, alternatives = self.process_line(
            q=query_description,
            root=statement_node,
            atok=atok,
            origin=origin,
            select_node=origin if selection[0] != selection[1] else None,
            tiebreaker=lambda x: tiebreak_on_lca(statement_node, origin, x))
        return self._backward_result(result, alternatives, build)
Example #5
0
    def case_four(self, view_information, query_description, extra={}):
        ################################################################
        #		<level> [<level_index>] <adjective> (argument <argument_index>|caller [<sub_index>])
        ###############################################################
        selection = self._get_selection(view_information, extra)
        build = self.general_build if self.general_build else line_partial(
            self.code, selection[0])
        if not build or not build[0]:
            return None, None
        root, atok, m, r = build
        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 = self.get_statement(origin, atok)

        ###############################################################
        # transformationszooming
        ################################################################
        every_caller = find_all_nodes(root, (ast.Call))
        level = LevelVisitor(root, every_caller, atok)
        lca = LCA(statement_node)

        def transformation(node):
            calling_parent, field, field_index = level[node]
            if not calling_parent or calling_parent is level.root:
                return None
            if query_description[
                    "level_index"] == 0 or correspond_to_index_in_call(
                        calling_parent, query_description["level_index"] - 1,
                        field, field_index):
                adj = translate_adjective[query_description["nth"]] - 1
                n = level(node, 3, adj)
                return node if n is node else None
            else:
                return None

        def inverse_transformation(node):
            return [node]

        q = deepcopy(query_description)
        del q["nth"]
        result, alternatives = self.process_line(
            q=q,
            root=statement_node,
            atok=atok,
            origin=origin,
            select_node=origin if selection[0] != selection[1] else None,
            tiebreaker=lambda x: tiebreak_on_lca(statement_node, origin, x, lca
                                                 ),
            transformation=transformation,
            inverse_transformation=inverse_transformation,
            invert_then_tiebreak=False)
        return self._backward_result(result, alternatives, build)
Example #6
0
    def case_two(self, view_information, query_description, extra={}):
        ################################################################
        #		<vertical_direction> <ndir> <adjective> argument <argument_index>
        ###############################################################
        selection = self._get_selection(view_information, extra)
        if "vertical_direction" in query_description:
            vertical_direction = query_description["vertical_direction"]
            ndir = query_description["ndir"]
        else:
            vertical_direction = "upwards"
            ndir = 0

        build = self.general_build if self.general_build else line_partial(
            self.code, 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 ["upwards", "downwards"]:
            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 = self.get_statement(origin, atok)

        #
        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 or ndir) 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)
Example #7
0
	def _preliminary(self,view_information,query_description, extra = {}):
		selection = self._get_selection(view_information,extra)
		build = self.general_build 
		if not build  or not build[0] :
			return None,None,None
		root,atok,m,r  = build 
		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)
		return build, selection, origin
    def handle_single(self, view_information, query_description, extra={}):
        # print(" inside here selection where he parked ")
        selection = self._get_selection(view_information, extra)
        build = self.general_build if self.general_build else line_partial(
            self.code, selection[0])
        if not build or not build[0]:
            return None, None
        root, atok, m, r = build
        selection = m.forward(selection)
        origin = nearest_node_from_offset(
            root, atok, selection[0],
            special=True) if selection[0] == selection[1] else node_from_range(
                root, atok, selection, special=True)
        if selection[0] == selection[1]:
            # return None,None
            pass
        second_origin = origin
        if "nth" in query_description:
            # print(" hello world  ")
            # print(translate_adjective[query_description["nth"]])
            second_origin = get_sub_index(
                origin, translate_adjective[query_description["nth"]] - 1)

        if query_description["format"] == 1:
            if "nth2" in query_description:
                second_origin = get_sub_index(
                    second_origin,
                    translate_adjective[query_description["nth2"]] - 1)
            result = get_sub_index(second_origin,
                                   query_description["sub_index"] - 1)
            alternatives = []
        elif query_description["format"] == 2:
            if "nth2" in query_description:
                second_origin = get_sub_index(
                    second_origin,
                    translate_adjective[query_description["nth2"]] - 1)
            result = [
                get_sub_index(second_origin,
                              query_description["sub_index"] - 1),
                get_sub_index(second_origin,
                              query_description.get("sub_index2", 0) - 1)
            ]
            alternatives = []
        elif query_description["format"] == 3 or query_description[
                "format"] == 4:
            intermediate = get_sub_index(second_origin, None)
            if "nth2" in query_description:
                intermediate = [
                    get_sub_index(
                        x, translate_adjective[query_description["nth2"]] - 1)
                    for x in intermediate
                ]
                intermediate = [x for x in intermediate if x]
            if "sub_index" in query_description:
                candidates = [
                    get_sub_index(x, query_description["sub_index"] - 1)
                    for x in intermediate
                ]
                candidates = [x for x in candidates if x]
            else:
                candidates = intermediate
            if query_description["format"] == 3:
                result, alternatives = obtain_result(None, candidates)
            elif query_description["format"] == 4:
                result = candidates if candidates else None
                alternatives = []
        return self._backward_result(
            result,
            alternatives,
            build,
            individually=query_description["format"] == 4)