|
1 | 1 | from typing import Optional, cast, TypeVar |
2 | 2 |
|
3 | 3 | from rewrite import Tree |
4 | | -from rewrite.java import J, Assignment, JLeftPadded |
| 4 | +from rewrite.java import J, Assignment, JLeftPadded, AssignmentOperation |
5 | 5 | from rewrite.java import MethodInvocation, MethodDeclaration, Empty, ArrayAccess, Space |
6 | 6 | from rewrite.python import PythonVisitor, SpacesStyle |
7 | 7 | from rewrite.visitor import P |
@@ -85,6 +85,28 @@ def visit_array_access(self, array_access: ArrayAccess, p: P) -> J: |
85 | 85 | ) |
86 | 86 | return a |
87 | 87 |
|
| 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 | + |
88 | 110 | def space_before(self, j: J2, space_before: bool) -> J2: |
89 | 111 | space: Space = cast(Space, j.prefix) |
90 | 112 | if space.comments or '\\' in space.whitespace: |
@@ -120,16 +142,6 @@ def space_after(self, j: J2, space_after: bool) -> J2: |
120 | 142 | return j.with_after(space.with_whitespace("")) |
121 | 143 | return j |
122 | 144 |
|
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 | | - |
133 | 145 | def update_space(s: Space, have_space: bool) -> Space: |
134 | 146 | if s.comments: |
135 | 147 | return s |
|
0 commit comments