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

Commit 9656e5f

Browse files
committed
Add support for assignment operator
1 parent b910c1c commit 9656e5f

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

rewrite/rewrite/python/format/spaces_visitor.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Optional, cast, TypeVar
22

33
from rewrite import Tree
4-
from rewrite.java import J, Assignment, JLeftPadded
4+
from rewrite.java import J, Assignment, JLeftPadded, AssignmentOperation
55
from rewrite.java import MethodInvocation, MethodDeclaration, Empty, ArrayAccess, Space
66
from rewrite.python import PythonVisitor, SpacesStyle
77
from rewrite.visitor import P
@@ -85,6 +85,28 @@ def visit_array_access(self, array_access: ArrayAccess, p: P) -> J:
8585
)
8686
return a
8787

88+
def visit_assignment(self, assignment: Assignment, p: P) -> J:
89+
"""
90+
Handle assignment operator e.g. a = 1 <-> a=1
91+
"""
92+
a: Assignment = cast(Assignment, super().visit_assignment(assignment, p))
93+
a = a.padding.with_assignment(
94+
self.space_before_jleftpadded(a.padding.assignment, self._style.around_operators.assignment))
95+
a = a.padding.with_assignment(
96+
a.padding.assignment.with_element(
97+
self.space_before(a.padding.assignment.element, self._style.around_operators.assignment)))
98+
return a
99+
100+
def visit_assignment_operation(self, assignment_operation: AssignmentOperation, p: P) -> J:
101+
"""
102+
Handle assignment operation e.g. a += 1 <-> a+=1
103+
"""
104+
a: AssignmentOperation = cast(AssignmentOperation, super().visit_assignment_operation(assignment_operation, p))
105+
operator: JLeftPadded = a.padding.operator
106+
a = a.padding.with_operator(
107+
operator.with_before(update_space(operator.before, self._style.around_operators.assignment)))
108+
return a.with_assignment(self.space_before(a.assignment, self._style.around_operators.assignment))
109+
88110
def space_before(self, j: J2, space_before: bool) -> J2:
89111
space: Space = cast(Space, j.prefix)
90112
if space.comments or '\\' in space.whitespace:
@@ -120,16 +142,6 @@ def space_after(self, j: J2, space_after: bool) -> J2:
120142
return j.with_after(space.with_whitespace(""))
121143
return j
122144

123-
def visit_assignment(self, assignment: Assignment, p: P) -> J:
124-
a: Assignment = cast(Assignment, super().visit_assignment(assignment, p))
125-
a = a.padding.with_assignment(
126-
self.space_before_jleftpadded(a.padding.assignment, self._style.around_operators.assignment))
127-
a = a.padding.with_assignment(
128-
a.padding.assignment.with_element(
129-
self.space_before(a.padding.assignment.element, self._style.around_operators.assignment)))
130-
return a
131-
132-
133145
def update_space(s: Space, have_space: bool) -> Space:
134146
if s.comments:
135147
return s

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def test_spaces_after_comma_method_call():
159159
.with_recipe(from_visitor(SpacesVisitor(style)))
160160
)
161161

162+
162163
def test_spaces_around_assignment():
163164
style = IntelliJ.spaces()
164165
style = style.with_other(
@@ -185,3 +186,27 @@ def foo(x):
185186
spec=RecipeSpec()
186187
.with_recipe(from_visitor(SpacesVisitor(style)))
187188
)
189+
190+
191+
def test_spaces_around_assignment():
192+
style = IntelliJ.spaces()
193+
style = style.with_other(
194+
style.other.with_after_comma(True)
195+
)
196+
rewrite_run(
197+
# language=python
198+
python(
199+
"""
200+
a+=1
201+
a-= 1
202+
a +=1
203+
""",
204+
"""
205+
a += 1
206+
a -= 1
207+
a += 1
208+
"""
209+
),
210+
spec=RecipeSpec()
211+
.with_recipe(from_visitor(SpacesVisitor(style)))
212+
)

0 commit comments

Comments
 (0)