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

Add fix for operators #129

Merged
merged 10 commits into from
Jan 28, 2025
30 changes: 27 additions & 3 deletions rewrite/rewrite/python/format/spaces_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
MethodDeclaration, Empty, ArrayAccess, Space, If, Block, ClassDeclaration, VariableDeclarations, JRightPadded, \
Import, ParameterizedType, Parentheses
from rewrite.python import PythonVisitor, SpacesStyle, Binary, ChainedAssignment, Slice, CollectionLiteral, \
ForLoop, DictLiteral, KeyValue, TypeHint, MultiImport, ExpressionTypeTree, ComprehensionExpression
ForLoop, DictLiteral, KeyValue, TypeHint, MultiImport, ExpressionTypeTree, ComprehensionExpression, NamedArgument
from rewrite.visitor import P


Expand Down Expand Up @@ -99,6 +99,25 @@ def _process_argument(index, arg, args_size):
)
)

def visit_named_argument(self, named_argument: NamedArgument, p: P) -> J:
a = cast(NamedArgument, super().visit_named_argument(named_argument, p))
if a.padding.value is not None:
a = a.padding.with_value(
space_before_left_padded(a.padding.value, self._style.around_operators.eq_in_keyword_argument))
return a.padding.with_value(
space_before_left_padded_element(a.padding.value, self._style.around_operators.eq_in_keyword_argument))
return a

def visit_variable(self, named_variable: VariableDeclarations.NamedVariable, p: P) -> J:
v = cast(VariableDeclarations.NamedVariable, super().visit_variable(named_variable, p))
if v.padding.initializer is not None and v.padding.initializer.element is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we here probably need to limit this to method parameter declarations. Currently this would also apply to other variables (e.g. class fields or local variables). Or does the IntelliJ setting also apply to other types of variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this code is only relevant for the eq_in_named_parameter and, more specifically, when it's set to False, which is not the default. I am not quite sure what the best approach here would be, so for now, I removed the snippet from this PR as the rest of the operators are independent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trick would be to use the cursor to check if the parent tree is a method declaration.

use_space = self._style.around_operators.eq_in_named_parameter or v.variable_type is not None
v = v.padding.with_initializer(
space_before_left_padded(v.padding.initializer, use_space))
v = v.padding.with_initializer(
space_before_left_padded_element(v.padding.initializer, use_space))
return v

def visit_block(self, block: Block, p: P) -> J:
b = cast(Block, super().visit_block(block, p))
b = space_before(b, self._style.other.before_colon)
Expand Down Expand Up @@ -226,8 +245,10 @@ def visit_binary(self, binary: j.Binary, p: P) -> J:
b = self._apply_binary_space_around(b, self._style.around_operators.additive)
elif op in [j.Binary.Type.Multiplication, j.Binary.Type.Division, j.Binary.Type.Modulo]:
b = self._apply_binary_space_around(b, self._style.around_operators.multiplicative)
elif op in [j.Binary.Type.Equal, j.Binary.Type.NotEqual]:
b = self._apply_binary_space_around(b, self._style.around_operators.equality)
elif op in [j.Binary.Type.LessThan, j.Binary.Type.GreaterThan, j.Binary.Type.LessThanOrEqual,
j.Binary.Type.GreaterThanOrEqual, j.Binary.Type.Equal, j.Binary.Type.NotEqual]:
j.Binary.Type.GreaterThanOrEqual]:
b = self._apply_binary_space_around(b, self._style.around_operators.relational)
elif op in [j.Binary.Type.BitAnd, j.Binary.Type.BitOr, j.Binary.Type.BitXor]:
b = self._apply_binary_space_around(b, self._style.around_operators.bitwise)
Expand All @@ -246,10 +267,13 @@ def visit_python_binary(self, binary: Binary, p: P) -> J:
if op == Binary.Type.In or op == Binary.Type.Is or op == Binary.Type.IsNot or op == Binary.Type.NotIn:
# TODO: Not sure what style options to use for these operators
b = self._apply_binary_space_around(b, True)
elif op == Binary.Type.FloorDivision or op == Binary.Type.MatrixMultiplication or op == Binary.Type.Power:
elif op in [Binary.Type.FloorDivision, Binary.Type.MatrixMultiplication]:
b = self._apply_binary_space_around(b, self._style.around_operators.multiplicative)
elif op == Binary.Type.StringConcatenation:
b = self._apply_binary_space_around(b, self._style.around_operators.additive)
elif op == Binary.Type.Power:
b = self._apply_binary_space_around(b, self._style.around_operators.power)

return b

def visit_if(self, if_stm: If, p: P) -> J:
Expand Down
Loading
Loading