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

Commit bd235c9

Browse files
committed
Add blank line formatting for class members
1 parent 4673bde commit bd235c9

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

rewrite/rewrite/python/format/blank_lines.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional, TypeVar, cast
44

55
from rewrite import Tree, P, Cursor
6-
from rewrite.java import J, Space, Statement, JRightPadded
6+
from rewrite.java import J, Space, Statement, JRightPadded, Block, ClassDeclaration, MethodDeclaration
77
from rewrite.python import PythonVisitor, BlankLinesStyle, CompilationUnit
88
from rewrite.visitor import T
99

@@ -30,6 +30,15 @@ def visit_statement(self, statement: Statement, p: P) -> J:
3030
statement = minimum_lines_for_tree(statement, self._style.minimum.around_top_level_classes_functions)
3131
elif top_level:
3232
statement = statement.with_prefix(statement.prefix.with_whitespace(''))
33+
else:
34+
in_block = isinstance(parent_cursor.value, Block)
35+
in_class = in_block and isinstance(parent_cursor.parent_tree_cursor().value, ClassDeclaration)
36+
if in_class:
37+
is_first = cast(Block, parent_cursor.value).statements[0] is statement
38+
if not is_first and isinstance(statement, MethodDeclaration):
39+
statement = minimum_lines_for_tree(statement, self._style.minimum._around_method)
40+
elif not is_first and isinstance(statement, ClassDeclaration):
41+
statement = minimum_lines_for_tree(statement, self._style.minimum._around_class)
3342
return statement
3443

3544
def post_visit(self, tree: T, p: P) -> Optional[T]:

rewrite/tests/python/all/format/blank_lines_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,35 @@ def f():
5151
from_visitor(BlankLinesVisitor(IntelliJ.blank_lines()))
5252
)
5353
)
54+
55+
56+
def test_blank_lines_between_class_methods():
57+
rewrite_run(
58+
# language=python
59+
python(
60+
"""\
61+
class Foo:
62+
def foo(self):
63+
pass
64+
def bar(self):
65+
pass
66+
class Nested:
67+
pass
68+
""",
69+
"""\
70+
class Foo:
71+
def foo(self):
72+
pass
73+
74+
def bar(self):
75+
pass
76+
77+
class Nested:
78+
pass
79+
"""
80+
),
81+
spec=RecipeSpec()
82+
.with_recipes(
83+
from_visitor(BlankLinesVisitor(IntelliJ.blank_lines()))
84+
)
85+
)

0 commit comments

Comments
 (0)