Skip to content

Commit

Permalink
Merge branch 'main' into fix/stringliteral
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdebruin authored Jan 24, 2025
2 parents 1d6fdab + e8c666c commit 426cf50
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 1 deletion.
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
216 changes: 216 additions & 0 deletions rewrite/tests/python/all/format/full_formatter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
from rewrite.python import AutoFormat
from rewrite.test import rewrite_run, python, RecipeSpec


def test_remove_leading_module_blank_lines():
rewrite_run(
# language=python
python(
"""\
from typing import Tuple
from dataclasses import dataclass
import os,sys
@dataclass
class Test:
pass
class FormatTest:
def __init__( self , x:int,y:int ):
self.x=x
self.y =y
self.z=[1, 2,3]
self.d={"a":1,"b" :2}
self.test_obj=Test()
def method_spaces_test(self,a, b,c):
return a+b*c>>2|4
def multiline_method (
self,
very_long_parameter_name: int,
another_long_parameter: str)-> Tuple[int,str]:
return very_long_parameter_name, another_long_parameter
def list_comprehension_test(self):
return [x *2 for x in range(10)
if x>5]
def dict_comprehension_test(self):
keys =['a','b','c']
values=[1, 2,3]
return {k: v**2
for k,v in zip(keys,values)
if v %2==0}
def nested_structures(self):
return {"key":[1,2,
3],"other":{
"nested":4}}
def operators_test(self):
a, b, c,d, e, f = 1, 2, 3, 4, 5, 6
a=1+2
b =3*4
c += 3
d>>=2
e|=3
f **=2
return f
def helper_method_1( self,x:int)->int:
return x **2
def helper_method_2(self,y:int, z:int )->int:
try:
result =y<<2 *z
return result
except ZeroDivisionError as e:
raise e
except(ValueError, TypeError) :
return 0
finally :
self.cleanup( )
def cleanup(self):
pass
def conditional_test(self ):
x,y,z = 10,8,20
a,b,c,d,e,f = 1,2,3,3,4,5
if(x>5 and y <10 or z>=15):
result=self.helper_method_1( x )
return result
elif (a<=b and c ==d or e !=f):
self.helper_method_2(y, z)
self.nested_structures( )
elif(x&y==4 and z|y !=0):
x>>=1
self.method_spaces_test(a,b, c)
while(x**2>=100 and y<<2 <=50):
self.operators_test( )
self.test_obj.empty_method( )
for i in range(10) :
if i*2>5 and i %3==0:
self.list_comprehension_test( )
def top_level_function (x:int,y:int ):
return x+y
class AnotherClass:
def first_method(self): pass
""",
"""\
from typing import Tuple
from dataclasses import dataclass
import os, sys
@dataclass
class Test:
pass
class FormatTest:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
self.z = [1, 2, 3]
self.d = {"a": 1, "b": 2}
self.test_obj = Test()
def method_spaces_test(self, a, b, c):
return a + b * c >> 2 | 4
def multiline_method(
self,
very_long_parameter_name: int,
another_long_parameter: str) -> Tuple[int, str]:
return very_long_parameter_name, another_long_parameter
def list_comprehension_test(self):
return [x * 2 for x in range(10)
if x > 5]
def dict_comprehension_test(self):
keys = ['a', 'b', 'c']
values = [1, 2, 3]
return {k: v ** 2
for k, v in zip(keys, values)
if v % 2 == 0}
def nested_structures(self):
return {"key": [1, 2,
3], "other": {
"nested": 4}}
def operators_test(self):
a, b, c, d, e, f = 1, 2, 3, 4, 5, 6
a = 1 + 2
b = 3 * 4
c += 3
d >>= 2
e |= 3
f **= 2
return f
def helper_method_1(self, x: int) -> int:
return x ** 2
def helper_method_2(self, y: int, z: int) -> int:
try:
result = y << 2 * z
return result
except ZeroDivisionError as e:
raise e
except(ValueError, TypeError):
return 0
finally:
self.cleanup()
def cleanup(self):
pass
def conditional_test(self):
x, y, z = 10, 8, 20
a, b, c, d, e, f = 1, 2, 3, 3, 4, 5
if (x > 5 and y < 10 or z >= 15):
result = self.helper_method_1(x)
return result
elif (a <= b and c == d or e != f):
self.helper_method_2(y, z)
self.nested_structures()
elif (x & y == 4 and z | y != 0):
x >>= 1
self.method_spaces_test(a, b, c)
while (x ** 2 >= 100 and y << 2 <= 50):
self.operators_test()
self.test_obj.empty_method()
for i in range(10):
if i * 2 > 5 and i % 3 == 0:
self.list_comprehension_test()
def top_level_function(x: int, y: int):
return x + y
class AnotherClass:
def first_method(self): pass
"""
),
spec=RecipeSpec()
.with_recipe(AutoFormat())
)
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)))
)

0 comments on commit 426cf50

Please sign in to comment.