Example #1
0
    def code_for_node(self, node: CSTNode) -> 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.
        """

        state = CodegenState(default_indent=self.default_indent,
                             default_newline=self.default_newline)
        node._codegen(state)
        return "".join(state.tokens)
Example #2
0
    def code_for_node(
            self,
            node: CSTNode,
            provider: Optional["_PositionProviderUnion"] = 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.

        This can also be used with a :class:`~libcst.metadata.PositionProvider` or a
        :class:`~libcst.metadata.WhitespaceInclusivePositionProvider` to compute
        position information, but that's an implementation detail, and you should use
        :meth:`MetadataWrapper.resolve() <libcst.metadata.MetadataWrapper.resolve>`
        instead.

        See :ref:`Metadata<libcst-metadata>` for more information.
        """

        from libcst.metadata.position_provider import PositionProvider

        if provider is None:
            state = CodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )
        elif isinstance(provider, PositionProvider):
            state = PositionProvidingCodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )
        else:
            state = WhitespaceInclusivePositionProvidingCodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )

        node._codegen(state)

        return "".join(state.tokens)
Example #3
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)