@@ -31,30 +31,39 @@ defmodule Code.Formatter do
31
31
@ right_new_line_before_binary_operators [ :| , :when ]
32
32
33
33
# Operators that are logical cannot be mixed without parens
34
- @ required_parens_logical_binary_operands [ :|| , :||| , : or, :&& , :& &&, :and ]
34
+ @ required_parens_logical_binary_operands [ :|| , :or , :&& , :and ]
35
35
36
36
# Operators with next break fits
37
37
@ next_break_fits_operators [ :<- , :== , :!= , :=~ , :=== , :!== , :< , :> , :<= , :>= , := , :"::" ]
38
38
39
- # Operators that always require parens on operands when they are the parent
39
+ # Operators that always require parens even
40
+ # when they are their own parents as they are not semantically associative
41
+ @ required_parens_even_when_parent [ :-- , :--- ]
42
+
43
+ # Operators that always require parens on operands
44
+ # when they are the parent of another operator with a difference precedence
45
+ # Most operators are listed, except comparison, arithmetic, and low precedence
40
46
@ required_parens_on_binary_operands [
41
- :|> ,
47
+ :||| ,
48
+ :&&& ,
42
49
:<<< ,
43
50
:>>> ,
51
+ :|> ,
44
52
:<~ ,
45
53
:~> ,
46
54
:<<~ ,
47
55
:~>> ,
48
56
:<~> ,
49
57
:"<|>" ,
50
- :"^^^" ,
51
- :+++ ,
52
- :--- ,
53
58
:in ,
59
+ :"^^^" ,
60
+ :"//" ,
54
61
:++ ,
55
62
:-- ,
56
- :.. ,
57
- :<>
63
+ :+++ ,
64
+ :--- ,
65
+ :<> ,
66
+ :..
58
67
]
59
68
60
69
@ locals_without_parens [
@@ -785,14 +794,13 @@ defmodule Code.Formatter do
785
794
op_string = Atom . to_string ( op )
786
795
787
796
cond do
788
- # If the operator has the same precedence as the parent and is on
789
- # the correct side, we respect the nesting rule to avoid multiple
790
- # nestings. This only applies for left associativity or same operator.
791
- parent_prec == prec and parent_assoc == side and ( side == :left or op == parent_op ) ->
797
+ # If we have the same operator and it is in the correct side,
798
+ # we don't add parens unless it is explicitly required.
799
+ parent_assoc == side and op == parent_op and op not in @ required_parens_even_when_parent ->
792
800
binary_op_to_algebra ( op , op_string , meta , left , right , context , state , nesting )
793
801
794
- # If the parent requires parens or the precedence is inverted or
795
- # it is in the wrong side, then we *need* parenthesis.
802
+ # If the operator requires parens (most of them do) or we are mixing logical operators
803
+ # or the precedence is inverted or it is in the wrong side, then we *need* parenthesis.
796
804
( parent_op in @ required_parens_on_binary_operands and op not in @ no_space_binary_operators ) or
797
805
( op in @ required_parens_logical_binary_operands and
798
806
parent_op in @ required_parens_logical_binary_operands ) or parent_prec > prec or
0 commit comments