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 state.pop_trailing_newline()
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)