Esempio n. 1
0
 def test_codegen_decrease_indent(self) -> None:
     state = BasicCodegenState(" " * 4, "\n", BasicPositionProvider())
     state.increase_indent(state.default_indent)
     state.increase_indent(state.default_indent)
     state.increase_indent(state.default_indent)
     state.decrease_indent()
     state.add_indent_tokens()
     self.assertEqual(position(state), (1, 8))
Esempio n. 2
0
    def code_for_node(self,
                      node: CSTNode,
                      provider: Optional["PositionProvider"] = None) -> str:
        """
        Generates the code for the given node in the context of this module. This is a
        method of Module, not CSTNode, because we need to know the module's default
        indentation and newline formats.

        By default, this also generates syntactic line and column metadata for each
        node. Passing :class:`~libcst.BasicPositionProvider` will generate basic
        line and column metadata instead. See :ref:`Metadata<libcst-metadata>`
        for more information.
        """

        from libcst.metadata.position_provider import SyntacticPositionProvider

        if provider is None:
            state = CodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )
        elif isinstance(provider, SyntacticPositionProvider):
            state = SyntacticCodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )
        else:
            state = BasicCodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )

        node._codegen(state)

        return "".join(state.tokens)
Esempio n. 3
0
    def test_position(self) -> None:
        # create a dummy node
        node = cst.Pass()

        # simulate codegen behavior for the dummy node
        # generates the code " pass "
        state = BasicCodegenState(" " * 4, "\n", BasicPositionProvider())
        start = CodePosition(state.line, state.column)
        state.add_token(" ")
        with state.record_syntactic_position(node):
            state.add_token("pass")
        state.add_token(" ")
        end = CodePosition(state.line, state.column)
        state.record_position(node, CodeRange(start, end))

        # check whitespace is correctly recorded
        self.assertEqual(state.provider._computed[node],
                         CodeRange.create((1, 0), (1, 6)))
Esempio n. 4
0
 def test_codegen_add_newline(self) -> None:
     state = BasicCodegenState(" " * 4, "\n", BasicPositionProvider())
     state.add_token("\n")
     self.assertEqual(position(state), (2, 0))
Esempio n. 5
0
 def test_codegen_add_tokens(self) -> None:
     state = BasicCodegenState(" " * 4, "\n", BasicPositionProvider())
     state.add_token("1234\n1234")
     self.assertEqual(position(state), (2, 4))
Esempio n. 6
0
 def test_codegen_initial_position(self) -> None:
     state = BasicCodegenState(" " * 4, "\n", BasicPositionProvider())
     self.assertEqual(position(state), (1, 0))