@@ -1103,58 +1103,6 @@ def format(q)
1103
1103
end
1104
1104
end
1105
1105
1106
- # Formats an array that contains only a list of variable references. To make
1107
- # things simpler, if there are a bunch, we format them all using the "fill"
1108
- # algorithm as opposed to breaking them into a ton of lines. For example,
1109
- #
1110
- # [foo, bar, baz]
1111
- #
1112
- # instead of becoming:
1113
- #
1114
- # [
1115
- # foo,
1116
- # bar,
1117
- # baz
1118
- # ]
1119
- #
1120
- # would instead become:
1121
- #
1122
- # [
1123
- # foo, bar,
1124
- # baz
1125
- # ]
1126
- #
1127
- # provided the line length was hit between `bar` and `baz`.
1128
- class VarRefsFormatter
1129
- # The separator for the fill algorithm.
1130
- class Separator
1131
- def call ( q )
1132
- q . text ( "," )
1133
- q . fill_breakable
1134
- end
1135
- end
1136
-
1137
- # [Args] the contents of the array
1138
- attr_reader :contents
1139
-
1140
- def initialize ( contents )
1141
- @contents = contents
1142
- end
1143
-
1144
- def format ( q )
1145
- q . text ( "[" )
1146
- q . group do
1147
- q . indent do
1148
- q . breakable_empty
1149
- q . seplist ( contents . parts , Separator . new ) { |part | q . format ( part ) }
1150
- q . if_break { q . text ( "," ) } if q . trailing_comma?
1151
- end
1152
- q . breakable_empty
1153
- end
1154
- q . text ( "]" )
1155
- end
1156
- end
1157
-
1158
1106
# This is a special formatter used if the array literal contains no values
1159
1107
# but _does_ contain comments. In this case we do some special formatting to
1160
1108
# make sure the comments gets indented properly.
@@ -1229,19 +1177,17 @@ def deconstruct_keys(_keys)
1229
1177
end
1230
1178
1231
1179
def format ( q )
1232
- if qwords?
1233
- QWordsFormatter . new ( contents ) . format ( q )
1234
- return
1235
- end
1236
-
1237
- if qsymbols?
1238
- QSymbolsFormatter . new ( contents ) . format ( q )
1239
- return
1240
- end
1180
+ if lbracket . comments . empty? && contents && contents . comments . empty? &&
1181
+ contents . parts . length > 1
1182
+ if qwords?
1183
+ QWordsFormatter . new ( contents ) . format ( q )
1184
+ return
1185
+ end
1241
1186
1242
- if var_refs? ( q )
1243
- VarRefsFormatter . new ( contents ) . format ( q )
1244
- return
1187
+ if qsymbols?
1188
+ QSymbolsFormatter . new ( contents ) . format ( q )
1189
+ return
1190
+ end
1245
1191
end
1246
1192
1247
1193
if empty_with_comments?
@@ -1273,39 +1219,24 @@ def ===(other)
1273
1219
private
1274
1220
1275
1221
def qwords?
1276
- lbracket . comments . empty? && contents && contents . comments . empty? &&
1277
- contents . parts . length > 1 &&
1278
- contents . parts . all? do |part |
1279
- case part
1280
- when StringLiteral
1281
- part . comments . empty? && part . parts . length == 1 &&
1282
- part . parts . first . is_a? ( TStringContent ) &&
1283
- !part . parts . first . value . match? ( /[\s \[ \] \\ ]/ )
1284
- when CHAR
1285
- !part . value . match? ( /[\[ \] \\ ]/ )
1286
- else
1287
- false
1288
- end
1222
+ contents . parts . all? do |part |
1223
+ case part
1224
+ when StringLiteral
1225
+ part . comments . empty? && part . parts . length == 1 &&
1226
+ part . parts . first . is_a? ( TStringContent ) &&
1227
+ !part . parts . first . value . match? ( /[\s \[ \] \\ ]/ )
1228
+ when CHAR
1229
+ !part . value . match? ( /[\[ \] \\ ]/ )
1230
+ else
1231
+ false
1289
1232
end
1233
+ end
1290
1234
end
1291
1235
1292
1236
def qsymbols?
1293
- lbracket . comments . empty? && contents && contents . comments . empty? &&
1294
- contents . parts . length > 1 &&
1295
- contents . parts . all? do |part |
1296
- part . is_a? ( SymbolLiteral ) && part . comments . empty?
1297
- end
1298
- end
1299
-
1300
- def var_refs? ( q )
1301
- lbracket . comments . empty? && contents && contents . comments . empty? &&
1302
- contents . parts . all? do |part |
1303
- part . is_a? ( VarRef ) && part . comments . empty?
1304
- end &&
1305
- (
1306
- contents . parts . sum { |part | part . value . value . length + 2 } >
1307
- q . maxwidth * 2
1308
- )
1237
+ contents . parts . all? do |part |
1238
+ part . is_a? ( SymbolLiteral ) && part . comments . empty?
1239
+ end
1309
1240
end
1310
1241
1311
1242
# If we have an empty array that contains only comments, then we're going
@@ -6551,9 +6482,26 @@ def deconstruct_keys(_keys)
6551
6482
6552
6483
def format ( q )
6553
6484
force_flat = [
6554
- AliasNode , Assign , Break , Command , CommandCall , Heredoc , IfNode , IfOp ,
6555
- Lambda , MAssign , Next , OpAssign , RescueMod , ReturnNode , Super , Undef ,
6556
- UnlessNode , VoidStmt , YieldNode , ZSuper
6485
+ AliasNode ,
6486
+ Assign ,
6487
+ Break ,
6488
+ Command ,
6489
+ CommandCall ,
6490
+ Heredoc ,
6491
+ IfNode ,
6492
+ IfOp ,
6493
+ Lambda ,
6494
+ MAssign ,
6495
+ Next ,
6496
+ OpAssign ,
6497
+ RescueMod ,
6498
+ ReturnNode ,
6499
+ Super ,
6500
+ Undef ,
6501
+ UnlessNode ,
6502
+ VoidStmt ,
6503
+ YieldNode ,
6504
+ ZSuper
6557
6505
]
6558
6506
6559
6507
if q . parent . is_a? ( Paren ) || force_flat . include? ( truthy . class ) ||
0 commit comments