Restore the missing spaces in the source (or something similar)
| 497 | |
| 498 | |
| 499 | class BlanksInserter(ast.NodeTransformer): # type: ignore |
| 500 | """ |
| 501 | Restore the missing spaces in the source (or something similar) |
| 502 | """ |
| 503 | |
| 504 | def generic_visit(self, node: ast.AST) -> ast.AST: |
| 505 | if isinstance(getattr(node, "body", None), list): |
| 506 | node.body = self._inject_blanks(node.body) |
| 507 | super().generic_visit(node) |
| 508 | return node |
| 509 | |
| 510 | def _inject_blanks(self, body: list[ast.Node]) -> list[ast.AST]: |
| 511 | if not body: |
| 512 | return body |
| 513 | |
| 514 | new_body = [] |
| 515 | before = body[0] |
| 516 | new_body.append(before) |
| 517 | for i in range(1, len(body)): |
| 518 | after = body[i] |
| 519 | if after.lineno - before.end_lineno - 1 > 0: |
| 520 | # Inserting one blank is enough. |
| 521 | blank = ast.Comment( |
| 522 | value="", |
| 523 | inline=False, |
| 524 | lineno=before.end_lineno + 1, |
| 525 | end_lineno=before.end_lineno + 1, |
| 526 | col_offset=0, |
| 527 | end_col_offset=0, |
| 528 | ) |
| 529 | new_body.append(blank) |
| 530 | new_body.append(after) |
| 531 | before = after |
| 532 | |
| 533 | return new_body |
| 534 | |
| 535 | |
| 536 | def unparse(tree: ast.AST) -> str: |