From 5ac9c1e6ec7b88a789f2e4add913643368ad0a38 Mon Sep 17 00:00:00 2001 From: Niels de Bruin Date: Thu, 23 Jan 2025 15:57:57 +0100 Subject: [PATCH 1/2] Fix while loop spaces --- .../rewrite/python/format/spaces_visitor.py | 5 +++ .../all/format/spaces/loops_space_test.py | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/rewrite/rewrite/python/format/spaces_visitor.py b/rewrite/rewrite/python/format/spaces_visitor.py index e85a4521..2f43026a 100644 --- a/rewrite/rewrite/python/format/spaces_visitor.py +++ b/rewrite/rewrite/python/format/spaces_visitor.py @@ -1,6 +1,7 @@ from typing import Optional, cast, List, TypeVar import rewrite.java as j +from java import WhileLoop 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, \ @@ -300,6 +301,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)) diff --git a/rewrite/tests/python/all/format/spaces/loops_space_test.py b/rewrite/tests/python/all/format/spaces/loops_space_test.py index dfc3ddf8..7ca6e657 100644 --- a/rewrite/tests/python/all/format/spaces/loops_space_test.py +++ b/rewrite/tests/python/all/format/spaces/loops_space_test.py @@ -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))) + ) From d2bbf8335cfaae492151f0eddb6089b62d081733 Mon Sep 17 00:00:00 2001 From: Niels de Bruin Date: Thu, 23 Jan 2025 16:35:38 +0100 Subject: [PATCH 2/2] Fix import --- rewrite/rewrite/python/format/spaces_visitor.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rewrite/rewrite/python/format/spaces_visitor.py b/rewrite/rewrite/python/format/spaces_visitor.py index 2f43026a..49956cff 100644 --- a/rewrite/rewrite/python/format/spaces_visitor.py +++ b/rewrite/rewrite/python/format/spaces_visitor.py @@ -1,11 +1,10 @@ from typing import Optional, cast, List, TypeVar import rewrite.java as j -from java import WhileLoop 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