Skip to content

Commit a8ae2af

Browse files
committed
Remove VarRef array formatting
1 parent 5a9bf11 commit a8ae2af

File tree

2 files changed

+54
-99
lines changed

2 files changed

+54
-99
lines changed

lib/syntax_tree/node.rb

+44-96
Original file line numberDiff line numberDiff line change
@@ -1103,58 +1103,6 @@ def format(q)
11031103
end
11041104
end
11051105

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-
11581106
# This is a special formatter used if the array literal contains no values
11591107
# but _does_ contain comments. In this case we do some special formatting to
11601108
# make sure the comments gets indented properly.
@@ -1229,19 +1177,17 @@ def deconstruct_keys(_keys)
12291177
end
12301178

12311179
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
12411186

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
12451191
end
12461192

12471193
if empty_with_comments?
@@ -1273,39 +1219,24 @@ def ===(other)
12731219
private
12741220

12751221
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
12891232
end
1233+
end
12901234
end
12911235

12921236
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
13091240
end
13101241

13111242
# If we have an empty array that contains only comments, then we're going
@@ -6551,9 +6482,26 @@ def deconstruct_keys(_keys)
65516482

65526483
def format(q)
65536484
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
65576505
]
65586506

65596507
if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) ||

test/fixtures/array_literal.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424
-
2525
fooooooooooooooooo = 1
2626
[
27-
fooooooooooooooooo, fooooooooooooooooo, fooooooooooooooooo,
28-
fooooooooooooooooo, fooooooooooooooooo, fooooooooooooooooo,
29-
fooooooooooooooooo, fooooooooooooooooo, fooooooooooooooooo, fooooooooooooooooo
27+
fooooooooooooooooo,
28+
fooooooooooooooooo,
29+
fooooooooooooooooo,
30+
fooooooooooooooooo,
31+
fooooooooooooooooo,
32+
fooooooooooooooooo,
33+
fooooooooooooooooo,
34+
fooooooooooooooooo,
35+
fooooooooooooooooo,
36+
fooooooooooooooooo
3037
]
3138
%
3239
[

0 commit comments

Comments
 (0)