def code(self): state = CodegenState( default_indent=' ' * 4, # TODO: take in account the config default_newline="\n" # TODO: take in account the config ) self._codegen(state) return ''.join(state.tokens)
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)
def get_modified_statement_code(self, node: BaseStatement) -> str: """ Gets the new code for ``node`` as if it were in same location as the old statement being replaced. This means that it inherits details like the old statement's indentation. """ new_codegen_state = CodegenState( default_indent=self._prev_codegen_state.default_indent, default_newline=self._prev_codegen_state.default_newline, indent_tokens=list(self._indent_tokens), ) node._codegen(new_codegen_state) if not self.has_trailing_newline: new_codegen_state.pop_trailing_newline() return "".join(new_codegen_state.tokens)
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)
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)
def cst_node_to_code(node): state = CodegenState(default_indent=4, default_newline='\n') node._codegen(state) return "".join(state.tokens)