Exemple #1
0
 def _codegen_impl(self, state: CodegenState) -> None:
     if self.indent:
         state.add_indent_tokens()
     self.whitespace._codegen(state)
     if self.comment is not None:
         self.comment._codegen(state)
     self.newline._codegen(state)
Exemple #2
0
 def _codegen_impl(self, state: CodegenState) -> None:
     self.whitespace_before._codegen(state)
     with state.record_syntactic_position(self):
         state.add_token(self._get_tokens()[0])
         self.whitespace_between._codegen(state)
         state.add_token(self._get_tokens()[1])
     self.whitespace_after._codegen(state)
Exemple #3
0
 def _codegen_impl(self, state: CodegenState) -> None:
     self.first_line._codegen(state)
     for line in self.empty_lines:
         line._codegen(state)
     if self.indent:
         state.add_indent_tokens()
     self.last_line._codegen(state)
Exemple #4
0
 def _codegen_impl(self, state: CodegenState) -> None:
     state.increase_indent(self.value)
     with state.record_syntactic_position(self,
                                          start_node=self.child,
                                          end_node=self.child):
         self.child._codegen(state)
     state.decrease_indent()
Exemple #5
0
 def _codegen_impl(self, state: CodegenState) -> None:
     for h in self.header:
         h._codegen(state)
     for stmt in self.body:
         stmt._codegen(state)
     for f in self.footer:
         f._codegen(state)
     if self.has_trailing_newline:
         if len(state.tokens) == 0:
             # There was nothing in the header, footer, or body. Just add a newline
             # to preserve the trailing newline.
             state.add_token(state.default_newline)
     else:  # has_trailing_newline is false
         if len(state.tokens) > 0:
             # EmptyLine and all statements generate newlines, so we can be sure that
             # the last token (if we're not an empty file) is a newline.
             state.tokens.pop()
Exemple #6
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)
Exemple #7
0
 def _codegen_impl(self, state: CodegenState) -> None:
     self.whitespace_before._codegen(state)
     state.add_token(self.value)
     self.whitespace_after._codegen(state)
Exemple #8
0
 def _codegen_impl(self, state: CodegenState) -> None:
     state.add_token("*")
Exemple #9
0
 def _codegen_impl(self, state: CodegenState) -> None:
     state.add_token(self._get_token())
     self.whitespace_after._codegen(state)
Exemple #10
0
 def _codegen_impl(self, state: CodegenState) -> None:
     state.add_token(
         state.default_newline if self.value is None else self.value)
Exemple #11
0
 def _codegen(self, state: CodegenState, **kwargs: Any) -> None:
     start = CodePosition(state.line, state.column)
     self._codegen_impl(state, **kwargs)
     end = CodePosition(state.line, state.column)
     state.record_position(self, CodeRange(start, end))