@@ -1551,6 +1551,11 @@ def format(q)
1551
1551
q . format ( statements )
1552
1552
end
1553
1553
end
1554
+
1555
+ if consequent
1556
+ q . breakable_force
1557
+ q . format ( consequent )
1558
+ end
1554
1559
end
1555
1560
elsif !if_keyword_loc
1556
1561
q . group do
@@ -1560,57 +1565,58 @@ def format(q)
1560
1565
q . text ( " : " )
1561
1566
q . format ( consequent . statements . body . first )
1562
1567
end
1563
- elsif statements . nil? || predicate . contains_write?
1568
+ elsif statements . nil?
1564
1569
q . group do
1565
- q . loc ( if_keyword_loc )
1566
- q . text ( " " )
1567
- q . nest ( 3 ) { q . format ( predicate ) }
1568
-
1569
- if statements
1570
- q . indent do
1571
- q . breakable_force
1572
- q . format ( statements )
1573
- end
1574
- end
1575
-
1576
- if consequent
1577
- q . breakable_force
1578
- q . format ( consequent )
1570
+ format_break ( q )
1571
+ q . break_parent
1572
+ end
1573
+ elsif predicate . contains_write? || statements . contains_write?
1574
+ if end_keyword_loc
1575
+ q . group do
1576
+ format_break ( q )
1577
+ q . break_parent
1579
1578
end
1580
-
1581
- q . breakable_force
1582
- q . text ( "end" )
1579
+ else
1580
+ q . group { format_flat ( q ) }
1583
1581
end
1584
1582
else
1585
1583
q . group do
1586
1584
q
1587
- . if_break do
1588
- q . group do
1589
- q . loc ( if_keyword_loc )
1590
- q . text ( " " )
1591
- q . nest ( 3 ) { q . format ( predicate ) }
1592
- end
1585
+ . if_break { format_break ( q ) }
1586
+ . if_flat { q . format_flat_parentheses { format_flat ( q ) } }
1587
+ end
1588
+ end
1589
+ end
1593
1590
1594
- q . indent do
1595
- q . breakable_space
1596
- q . format ( statements )
1597
- end
1591
+ private
1598
1592
1599
- if consequent
1600
- q . breakable_space
1601
- q . format ( consequent )
1602
- end
1593
+ def format_break ( q )
1594
+ q . group do
1595
+ q . loc ( if_keyword_loc )
1596
+ q . text ( " " )
1597
+ q . nest ( 3 ) { q . format ( predicate ) }
1598
+ end
1603
1599
1604
- q . breakable_space
1605
- q . text ( "end" )
1606
- end
1607
- . if_flat do
1608
- q . format ( statements )
1609
- q . text ( " if " )
1610
- q . format ( predicate )
1611
- end
1600
+ if statements
1601
+ q . indent do
1602
+ q . breakable_space
1603
+ q . format ( statements )
1612
1604
end
1613
1605
end
1606
+
1607
+ if consequent
1608
+ q . breakable_space
1609
+ q . format ( consequent )
1610
+ end
1611
+
1612
+ q . breakable_space
1613
+ q . text ( "end" )
1614
+ end
1615
+
1616
+ def format_flat ( q )
1617
+ q . format ( statements )
1618
+ q . text ( " if " )
1619
+ q . format ( predicate )
1614
1620
end
1615
1621
end
1616
1622
@@ -2939,55 +2945,58 @@ def format(q)
2939
2945
statements = self . statements
2940
2946
consequent = self . consequent
2941
2947
2942
- if !statements || consequent || predicate . contains_write?
2948
+ if !statements || consequent
2943
2949
q . group do
2944
- q . text ( "unless " )
2945
- q . nest ( 3 ) { q . format ( predicate ) }
2946
-
2947
- if statements
2948
- q . indent do
2949
- q . breakable_force
2950
- q . format ( statements )
2951
- end
2952
- end
2953
-
2954
- if consequent
2955
- q . breakable_force
2956
- q . format ( consequent )
2950
+ format_break ( q )
2951
+ q . break_parent
2952
+ end
2953
+ elsif predicate . contains_write? || statements . contains_write?
2954
+ if end_keyword_loc
2955
+ q . group do
2956
+ format_break ( q )
2957
+ q . break_parent
2957
2958
end
2958
-
2959
- q . breakable_force
2960
- q . text ( "end" )
2959
+ else
2960
+ q . group { format_flat ( q ) }
2961
2961
end
2962
2962
else
2963
2963
q . group do
2964
2964
q
2965
- . if_break do
2966
- q . group do
2967
- q . text ( "unless " )
2968
- q . nest ( 3 ) { q . format ( predicate ) }
2969
- end
2965
+ . if_break { format_break ( q ) }
2966
+ . if_flat { q . format_flat_parentheses { format_flat ( q ) } }
2967
+ end
2968
+ end
2969
+ end
2970
2970
2971
- q . indent do
2972
- q . breakable_space
2973
- q . format ( statements )
2974
- end
2971
+ private
2975
2972
2976
- if consequent
2977
- q . breakable_space
2978
- q . format ( consequent )
2979
- end
2973
+ def format_break ( q )
2974
+ q . group do
2975
+ q . loc ( keyword_loc )
2976
+ q . text ( " " )
2977
+ q . nest ( 3 ) { q . format ( predicate ) }
2978
+ end
2980
2979
2981
- q . breakable_space
2982
- q . text ( "end" )
2983
- end
2984
- . if_flat do
2985
- q . format ( statements )
2986
- q . text ( " unless " )
2987
- q . format ( predicate )
2988
- end
2980
+ if statements
2981
+ q . indent do
2982
+ q . breakable_space
2983
+ q . format ( statements )
2989
2984
end
2990
2985
end
2986
+
2987
+ if consequent
2988
+ q . breakable_space
2989
+ q . format ( consequent )
2990
+ end
2991
+
2992
+ q . breakable_space
2993
+ q . text ( "end" )
2994
+ end
2995
+
2996
+ def format_flat ( q )
2997
+ q . format ( statements )
2998
+ q . text ( " unless " )
2999
+ q . format ( predicate )
2991
3000
end
2992
3001
end
2993
3002
@@ -3001,13 +3010,22 @@ class UntilNode
3001
3010
# until foo do bar end
3002
3011
# ^^^^^^^^^^^^^^^^^^^^
3003
3012
def format ( q )
3004
- if begin_modifier? || statements &. contains_write?
3013
+ if begin_modifier?
3005
3014
q . group { format_flat ( q ) }
3006
- elsif statements . nil? || keyword_loc . comments . any? || closing_loc &.comments &.any? || predicate . contains_write?
3015
+ elsif statements . nil? || keyword_loc . comments . any? || closing_loc &.comments &.any?
3007
3016
q . group do
3008
3017
format_break ( q )
3009
3018
q . break_parent
3010
3019
end
3020
+ elsif predicate . contains_write? || statements . contains_write?
3021
+ if closing_loc
3022
+ q . group do
3023
+ format_break ( q )
3024
+ q . break_parent
3025
+ end
3026
+ else
3027
+ q . group { format_flat ( q ) }
3028
+ end
3011
3029
else
3012
3030
q . group { q . if_break { format_break ( q ) } . if_flat { format_flat ( q ) } }
3013
3031
end
@@ -3025,9 +3043,11 @@ def format_break(q)
3025
3043
end
3026
3044
3027
3045
def format_flat ( q )
3028
- q . format ( statements )
3029
- q . text ( " until " )
3030
- q . format ( predicate )
3046
+ q . format_flat_parentheses do
3047
+ q . format ( statements )
3048
+ q . text ( " until " )
3049
+ q . format ( predicate )
3050
+ end
3031
3051
end
3032
3052
end
3033
3053
@@ -3069,13 +3089,22 @@ class WhileNode
3069
3089
# while foo do bar end
3070
3090
# ^^^^^^^^^^^^^^^^^^^^
3071
3091
def format ( q )
3072
- if begin_modifier? || statements &. contains_write?
3092
+ if begin_modifier?
3073
3093
q . group { format_flat ( q ) }
3074
- elsif statements . nil? || keyword_loc . comments . any? || closing_loc &.comments &.any? || predicate . contains_write?
3094
+ elsif statements . nil? || keyword_loc . comments . any? || closing_loc &.comments &.any?
3075
3095
q . group do
3076
3096
format_break ( q )
3077
3097
q . break_parent
3078
3098
end
3099
+ elsif predicate . contains_write? || statements . contains_write?
3100
+ if closing_loc
3101
+ q . group do
3102
+ format_break ( q )
3103
+ q . break_parent
3104
+ end
3105
+ else
3106
+ q . group { format_flat ( q ) }
3107
+ end
3079
3108
else
3080
3109
q . group { q . if_break { format_break ( q ) } . if_flat { format_flat ( q ) } }
3081
3110
end
@@ -3093,9 +3122,11 @@ def format_break(q)
3093
3122
end
3094
3123
3095
3124
def format_flat ( q )
3096
- q . format ( statements )
3097
- q . text ( " while " )
3098
- q . format ( predicate )
3125
+ q . format_flat_parentheses do
3126
+ q . format ( statements )
3127
+ q . text ( " while " )
3128
+ q . format ( predicate )
3129
+ end
3099
3130
end
3100
3131
end
3101
3132
0 commit comments