Generate a line. If the line is empty, only emit if it makes sense. If the line is too long, split it first and then generate. If any lines were generated, set up a new current_line.
(self, indent: int = 0)
| 119 | self.__post_init__() |
| 120 | |
| 121 | def line(self, indent: int = 0) -> Iterator[Line]: |
| 122 | """Generate a line. |
| 123 | |
| 124 | If the line is empty, only emit if it makes sense. |
| 125 | If the line is too long, split it first and then generate. |
| 126 | |
| 127 | If any lines were generated, set up a new current_line. |
| 128 | """ |
| 129 | if not self.current_line: |
| 130 | self.current_line.depth += indent |
| 131 | return # Line is empty, don't emit. Creating a new one unnecessary. |
| 132 | |
| 133 | if len(self.current_line.leaves) == 1 and is_async_stmt_or_funcdef( |
| 134 | self.current_line.leaves[0] |
| 135 | ): |
| 136 | # Special case for async def/for/with statements. `visit_async_stmt` |
| 137 | # adds an `ASYNC` leaf then visits the child def/for/with statement |
| 138 | # nodes. Line yields from those nodes shouldn't treat the former |
| 139 | # `ASYNC` leaf as a complete line. |
| 140 | return |
| 141 | |
| 142 | complete_line = self.current_line |
| 143 | self.current_line = Line(mode=self.mode, depth=complete_line.depth + indent) |
| 144 | yield complete_line |
| 145 | |
| 146 | def visit_default(self, node: LN) -> Iterator[Line]: |
| 147 | """Default `visit_*()` implementation. Recurses to children of `node`.""" |
no test coverage detected