Skip to content

source_giver

SourceGiver

Source code in lineapy/transformer/source_giver.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class SourceGiver:
    def transform(self, nodes: ast.Module) -> None:
        """
        This call should only happen once asttoken has run its magic
        and embellished the ast with tokens and line numbers.
        At that point, all this function will do is use those tokens to
        figure out end_lineno and end_col_offset for every node in the tree
        """
        node: ast.AST
        # TODO check if the ast type is a Module instead of simply relying on mypy
        for node in ast.walk(nodes):
            if not hasattr(node, "lineno"):
                continue

            if hasattr(node, "last_token"):
                node.end_lineno = node.last_token.end[0]  # type: ignore
                node.end_col_offset = node.last_token.end[1]  # type: ignore
                # if isinstance(node, ast.ListComp):
                node.col_offset = node.first_token.start[1]  # type: ignore

transform(nodes)

This call should only happen once asttoken has run its magic and embellished the ast with tokens and line numbers. At that point, all this function will do is use those tokens to figure out end_lineno and end_col_offset for every node in the tree

Source code in lineapy/transformer/source_giver.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def transform(self, nodes: ast.Module) -> None:
    """
    This call should only happen once asttoken has run its magic
    and embellished the ast with tokens and line numbers.
    At that point, all this function will do is use those tokens to
    figure out end_lineno and end_col_offset for every node in the tree
    """
    node: ast.AST
    # TODO check if the ast type is a Module instead of simply relying on mypy
    for node in ast.walk(nodes):
        if not hasattr(node, "lineno"):
            continue

        if hasattr(node, "last_token"):
            node.end_lineno = node.last_token.end[0]  # type: ignore
            node.end_col_offset = node.last_token.end[1]  # type: ignore
            # if isinstance(node, ast.ListComp):
            node.col_offset = node.first_token.start[1]  # type: ignore

Was this helpful?

Help us improve docs with your feedback!