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

Commit 0c93349

Browse files
committed
Support blank lines before first method
In IDEA this setting actually only applies if the method is the first statement within the class, so this behavior has been duplicated.
1 parent ebf1941 commit 0c93349

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

rewrite/rewrite/python/format/blank_lines.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def visit_statement(self, statement: Statement, p: P) -> J:
5252
min_lines = max(min_lines, self._style.minimum.around_method)
5353
elif not is_first and isinstance(statement, ClassDeclaration):
5454
min_lines = max(min_lines, self._style.minimum.around_class)
55+
elif is_first and isinstance(statement, MethodDeclaration):
56+
min_lines = max(min_lines, self._style.minimum.before_first_method)
5557
if prev_import:
5658
min_lines = max(min_lines, self._style.minimum.after_local_imports)
5759
statement = minimum_lines_for_tree(statement, min_lines)

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,44 @@ class Foo:
146146
from_visitor(BlankLinesVisitor(style))
147147
)
148148
)
149+
150+
151+
def test_before_first_method():
152+
style = IntelliJ.blank_lines()
153+
style = style.with_minimum(
154+
style.minimum.with_before_first_method(2)
155+
)
156+
rewrite_run(
157+
# language=python
158+
python(
159+
"""\
160+
class Foo:
161+
def __init__(self):
162+
pass
163+
164+
165+
class Bar:
166+
x = 1
167+
def __init__(self):
168+
pass
169+
""",
170+
"""\
171+
class Foo:
172+
173+
174+
def __init__(self):
175+
pass
176+
177+
178+
class Bar:
179+
x = 1
180+
181+
def __init__(self):
182+
pass
183+
"""
184+
),
185+
spec=RecipeSpec()
186+
.with_recipes(
187+
from_visitor(BlankLinesVisitor(style))
188+
)
189+
)

0 commit comments

Comments
 (0)