@@ -2885,27 +2885,15 @@ def deconstruct_keys(_keys)
2885
2885
end
2886
2886
2887
2887
def format ( q )
2888
- declaration = -> do
2889
- q . group do
2890
- q . text ( "class " )
2891
- q . format ( constant )
2892
-
2893
- if superclass
2894
- q . text ( " < " )
2895
- q . format ( superclass )
2896
- end
2897
- end
2898
- end
2899
-
2900
2888
if bodystmt . empty?
2901
2889
q . group do
2902
- declaration . call
2890
+ format_declaration ( q )
2903
2891
q . breakable_force
2904
2892
q . text ( "end" )
2905
2893
end
2906
2894
else
2907
2895
q . group do
2908
- declaration . call
2896
+ format_declaration ( q )
2909
2897
2910
2898
q . indent do
2911
2899
q . breakable_force
@@ -2917,6 +2905,20 @@ def format(q)
2917
2905
end
2918
2906
end
2919
2907
end
2908
+
2909
+ private
2910
+
2911
+ def format_declaration ( q )
2912
+ q . group do
2913
+ q . text ( "class " )
2914
+ q . format ( constant )
2915
+
2916
+ if superclass
2917
+ q . text ( " < " )
2918
+ q . format ( superclass )
2919
+ end
2920
+ end
2921
+ end
2920
2922
end
2921
2923
2922
2924
# Comma represents the use of the , operator.
@@ -5122,18 +5124,7 @@ def deconstruct_keys(_keys)
5122
5124
def format ( q )
5123
5125
parts = keywords . map { |( key , value ) | KeywordFormatter . new ( key , value ) }
5124
5126
parts << KeywordRestFormatter . new ( keyword_rest ) if keyword_rest
5125
-
5126
5127
nested = PATTERNS . include? ( q . parent . class )
5127
- contents = -> do
5128
- q . group { q . seplist ( parts ) { |part | q . format ( part , stackable : false ) } }
5129
-
5130
- # If there isn't a constant, and there's a blank keyword_rest, then we
5131
- # have an plain ** that needs to have a `then` after it in order to
5132
- # parse correctly on the next parse.
5133
- if !constant && keyword_rest && keyword_rest . value . nil? && !nested
5134
- q . text ( " then" )
5135
- end
5136
- end
5137
5128
5138
5129
# If there is a constant, we're going to format to have the constant name
5139
5130
# first and then use brackets.
@@ -5143,7 +5134,7 @@ def format(q)
5143
5134
q . text ( "[" )
5144
5135
q . indent do
5145
5136
q . breakable_empty
5146
- contents . call
5137
+ format_contents ( q , parts , nested )
5147
5138
end
5148
5139
q . breakable_empty
5149
5140
q . text ( "]" )
@@ -5160,7 +5151,7 @@ def format(q)
5160
5151
# If there's only one pair, then we'll just print the contents provided
5161
5152
# we're not inside another pattern.
5162
5153
if !nested && parts . size == 1
5163
- contents . call
5154
+ format_contents ( q , parts , nested )
5164
5155
return
5165
5156
end
5166
5157
@@ -5170,7 +5161,7 @@ def format(q)
5170
5161
q . text ( "{" )
5171
5162
q . indent do
5172
5163
q . breakable_space
5173
- contents . call
5164
+ format_contents ( q , parts , nested )
5174
5165
end
5175
5166
5176
5167
if q . target_ruby_version < Gem ::Version . new ( "2.7.3" )
@@ -5181,6 +5172,19 @@ def format(q)
5181
5172
end
5182
5173
end
5183
5174
end
5175
+
5176
+ private
5177
+
5178
+ def format_contents ( q , parts , nested )
5179
+ q . group { q . seplist ( parts ) { |part | q . format ( part , stackable : false ) } }
5180
+
5181
+ # If there isn't a constant, and there's a blank keyword_rest, then we
5182
+ # have an plain ** that needs to have a `then` after it in order to
5183
+ # parse correctly on the next parse.
5184
+ if !constant && keyword_rest && keyword_rest . value . nil? && !nested
5185
+ q . text ( " then" )
5186
+ end
5187
+ end
5184
5188
end
5185
5189
5186
5190
# The list of nodes that represent patterns inside of pattern matching so that
@@ -6543,22 +6547,15 @@ def deconstruct_keys(_keys)
6543
6547
end
6544
6548
6545
6549
def format ( q )
6546
- declaration = -> do
6547
- q . group do
6548
- q . text ( "module " )
6549
- q . format ( constant )
6550
- end
6551
- end
6552
-
6553
6550
if bodystmt . empty?
6554
6551
q . group do
6555
- declaration . call
6552
+ format_declaration ( q )
6556
6553
q . breakable_force
6557
6554
q . text ( "end" )
6558
6555
end
6559
6556
else
6560
6557
q . group do
6561
- declaration . call
6558
+ format_declaration ( q )
6562
6559
6563
6560
q . indent do
6564
6561
q . breakable_force
@@ -6570,6 +6567,15 @@ def format(q)
6570
6567
end
6571
6568
end
6572
6569
end
6570
+
6571
+ private
6572
+
6573
+ def format_declaration ( q )
6574
+ q . group do
6575
+ q . text ( "module " )
6576
+ q . format ( constant )
6577
+ end
6578
+ end
6573
6579
end
6574
6580
6575
6581
# MRHS represents the values that are being assigned on the right-hand side of
@@ -7023,27 +7029,35 @@ def format(q)
7023
7029
parts << KeywordRestFormatter . new ( keyword_rest ) if keyword_rest
7024
7030
parts << block if block
7025
7031
7026
- contents = -> do
7027
- q . seplist ( parts ) { | part | q . format ( part ) }
7028
- q . format ( rest ) if rest . is_a? ( ExcessedComma )
7032
+ if parts . empty?
7033
+ q . nest ( 0 ) { format_contents ( q , parts ) }
7034
+ return
7029
7035
end
7030
7036
7031
- if ![ Def , Defs , DefEndless ] . include? ( q . parent . class ) || parts . empty?
7032
- q . nest ( 0 , &contents )
7033
- else
7037
+ case q . parent
7038
+ when Def , Defs , DefEndless
7034
7039
q . nest ( 0 ) do
7035
7040
q . text ( "(" )
7036
7041
q . group do
7037
7042
q . indent do
7038
7043
q . breakable_empty
7039
- contents . call
7044
+ format_contents ( q , parts )
7040
7045
end
7041
7046
q . breakable_empty
7042
7047
end
7043
7048
q . text ( ")" )
7044
7049
end
7050
+ else
7051
+ q . nest ( 0 ) { format_contents ( q , parts ) }
7045
7052
end
7046
7053
end
7054
+
7055
+ private
7056
+
7057
+ def format_contents ( q , parts )
7058
+ q . seplist ( parts ) { |part | q . format ( part ) }
7059
+ q . format ( rest ) if rest . is_a? ( ExcessedComma )
7060
+ end
7047
7061
end
7048
7062
7049
7063
# Paren represents using balanced parentheses in a couple places in a Ruby
0 commit comments