Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

While loop condition spaces #131

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion rewrite/rewrite/python/format/spaces_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rewrite import Tree, list_map
from rewrite.java import J, Assignment, JLeftPadded, AssignmentOperation, MemberReference, MethodInvocation, \
MethodDeclaration, Empty, ArrayAccess, Space, If, Block, ClassDeclaration, VariableDeclarations, JRightPadded, \
Import, ParameterizedType, Parentheses
Import, ParameterizedType, Parentheses, WhileLoop
from rewrite.python import PythonVisitor, SpacesStyle, Binary, ChainedAssignment, Slice, CollectionLiteral, \
ForLoop, DictLiteral, KeyValue, TypeHint, MultiImport, ExpressionTypeTree, ComprehensionExpression
from rewrite.visitor import P
Expand Down Expand Up @@ -300,6 +300,10 @@ def visit_python_for_loop(self, for_loop: ForLoop, p: P) -> J:
fl = fl.padding.with_iterable(space_before_right_padded_element(fl.padding.iterable, True))
return fl

def visit_while_loop(self, while_loop: WhileLoop, p: P) -> J:
w = cast(WhileLoop, super().visit_while_loop(while_loop, p))
return w.with_condition(space_before(w.condition, True))

def visit_parameterized_type(self, parameterized_type: ParameterizedType, p: P) -> J:
pt = cast(ParameterizedType, super().visit_parameterized_type(parameterized_type, p))

Expand Down
38 changes: 38 additions & 0 deletions rewrite/tests/python/all/format/spaces/loops_space_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,41 @@ def test_spaces_within_array_access_brackets():
spec=RecipeSpec()
.with_recipe(from_visitor(SpacesVisitor(style)))
)


def test_spaces_while_loop():
style = IntelliJ.spaces()
rewrite_run(
# language=python
python(
"""\
while x < 10 :
print("Hello")
""",
"""\
while x < 10:
print("Hello")
"""
),
spec=RecipeSpec()
.with_recipe(from_visitor(SpacesVisitor(style)))
)


def test_spaces_while_loop_with_parenthesis():
style = IntelliJ.spaces()
rewrite_run(
# language=python
python(
"""\
while(x < 10) :
print("Hello")
""",
"""\
while (x < 10):
print("Hello")
"""
),
spec=RecipeSpec()
.with_recipe(from_visitor(SpacesVisitor(style)))
)
Loading