@@ -31,30 +31,39 @@ defmodule Code.Formatter do
3131  @ right_new_line_before_binary_operators  [ :| ,  :when ] 
3232
3333  # 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 ] 
3535
3636  # Operators with next break fits 
3737  @ next_break_fits_operators  [ :<- ,  :== ,  :!= ,  :=~ ,  :=== ,  :!== ,  :< ,  :> ,  :<= ,  :>= ,  := ,  :"::" ] 
3838
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 
4046  @ required_parens_on_binary_operands  [ 
41-     :|> , 
47+     :||| , 
48+     :&&& , 
4249    :<<< , 
4350    :>>> , 
51+     :|> , 
4452    :<~ , 
4553    :~> , 
4654    :<<~ , 
4755    :~>> , 
4856    :<~> , 
4957    :"<|>" , 
50-     :"^^^" , 
51-     :+++ , 
52-     :--- , 
5358    :in , 
59+     :"^^^" , 
60+     :"//" , 
5461    :++ , 
5562    :-- , 
56-     :.. , 
57-     :<> 
63+     :+++ , 
64+     :--- , 
65+     :<> , 
66+     :.. 
5867  ] 
5968
6069  @ locals_without_parens  [ 
@@ -785,14 +794,13 @@ defmodule Code.Formatter do
785794      op_string  =  Atom . to_string ( op ) 
786795
787796      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  -> 
792800          binary_op_to_algebra ( op ,  op_string ,  meta ,  left ,  right ,  context ,  state ,  nesting ) 
793801
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. 
796804        ( parent_op  in  @ required_parens_on_binary_operands  and  op  not in  @ no_space_binary_operators )  or 
797805          ( op  in  @ required_parens_logical_binary_operands  and 
798806             parent_op  in  @ required_parens_logical_binary_operands )  or  parent_prec  >  prec  or 
0 commit comments