Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit b753d7c

Browse files
committed
Start work on BlankLinesVisitor
1 parent 8a27f5e commit b753d7c

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

rewrite/rewrite/python/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
# Formatter
6868
'AutoFormat',
69+
'BlankLinesVisitor',
6970
'NormalizeFormatVisitor',
7071
'SpacesVisitor',
7172
]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
22

33
from .auto_format import *
4+
from .blank_lines import *
45
from .normalize_format import *
56

67
__all__ = [
78
'AutoFormat',
9+
'BlankLinesVisitor',
810
'NormalizeFormatVisitor',
911
'SpacesVisitor',
1012
]

rewrite/rewrite/python/format/auto_format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional, cast, TypeVar
22

33
from .normalize_format import NormalizeFormatVisitor
4+
from .. import BlankLinesVisitor, BlankLinesStyle
45
from ..style import SpacesStyle, IntelliJ
56
from ..visitor import PythonVisitor
67
from ... import Recipe, Tree, Cursor
@@ -22,6 +23,7 @@ def visit(self, tree: Optional[Tree], p: P, parent: Optional[Cursor] = None) ->
2223
cu = tree if isinstance(tree, JavaSourceFile) else self._cursor.first_enclosing_or_throw(JavaSourceFile)
2324

2425
tree = NormalizeFormatVisitor(self._stop_after).visit(tree, p, self._cursor.fork())
26+
tree = BlankLinesVisitor(cu.get_style(BlankLinesStyle) or IntelliJ.blank_lines(), self._stop_after).visit(tree, p, self._cursor.fork())
2527
tree = SpacesVisitor(cu.get_style(SpacesStyle) or IntelliJ.spaces(), self._stop_after).visit(tree, p, self._cursor.fork())
2628
return tree
2729

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from __future__ import annotations
2+
3+
from typing import Optional, TypeVar
4+
5+
from rewrite import Tree, P, Cursor
6+
from rewrite.java import J
7+
from rewrite.python import PythonVisitor, BlankLinesStyle
8+
from rewrite.visitor import T
9+
10+
J2 = TypeVar('J2', bound=J)
11+
12+
13+
class BlankLinesVisitor(PythonVisitor):
14+
def __init__(self, style: BlankLinesStyle, stop_after: Tree = None):
15+
self._style = style
16+
self._stop_after = stop_after
17+
self._stop = False
18+
19+
def post_visit(self, tree: T, p: P) -> Optional[T]:
20+
if self._stop_after and tree == self._stop_after:
21+
self._stop = True
22+
return tree
23+
24+
def visit(self, tree: Optional[Tree], p: P, parent: Optional[Cursor] = None) -> Optional[T]:
25+
return tree if self._stop else super().visit(tree, p, parent)

0 commit comments

Comments
 (0)