Skip to content

Commit b8ec43e

Browse files
committed
Better formatting for array patterns in rassign
1 parent cc1f4c8 commit b8ec43e

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

lib/syntax_tree/node.rb

+39-14
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,11 @@ def format(q)
11311131
q.group do
11321132
q.format(constant)
11331133
q.text("[")
1134-
q.seplist(parts) { |part| q.format(part) }
1134+
q.indent do
1135+
q.breakable("")
1136+
q.seplist(parts) { |part| q.format(part) }
1137+
end
1138+
q.breakable("")
11351139
q.text("]")
11361140
end
11371141

@@ -1141,7 +1145,11 @@ def format(q)
11411145
parent = q.parent
11421146
if parts.length == 1 || PATTERNS.include?(parent.class)
11431147
q.text("[")
1144-
q.seplist(parts) { |part| q.format(part) }
1148+
q.indent do
1149+
q.breakable("")
1150+
q.seplist(parts) { |part| q.format(part) }
1151+
end
1152+
q.breakable("")
11451153
q.text("]")
11461154
elsif parts.empty?
11471155
q.text("[]")
@@ -2777,10 +2785,17 @@ def format(q)
27772785
q.format(value)
27782786
q.text(" ")
27792787
q.format(operator)
2780-
q.group do
2781-
q.indent do
2782-
q.breakable
2783-
q.format(pattern)
2788+
2789+
case pattern
2790+
in AryPtn | FndPtn | HshPtn
2791+
q.text(" ")
2792+
q.format(pattern)
2793+
else
2794+
q.group do
2795+
q.indent do
2796+
q.breakable
2797+
q.format(pattern)
2798+
end
27842799
end
27852800
end
27862801
end
@@ -4573,16 +4588,26 @@ def deconstruct_keys(_keys)
45734588

45744589
def format(q)
45754590
q.format(constant) if constant
4576-
q.group(0, "[", "]") do
4577-
q.text("*")
4578-
q.format(left)
4579-
q.comma_breakable
45804591

4581-
q.seplist(values) { |value| q.format(value) }
4582-
q.comma_breakable
4592+
q.group do
4593+
q.text("[")
45834594

4584-
q.text("*")
4585-
q.format(right)
4595+
q.indent do
4596+
q.breakable("")
4597+
4598+
q.text("*")
4599+
q.format(left)
4600+
q.comma_breakable
4601+
4602+
q.seplist(values) { |value| q.format(value) }
4603+
q.comma_breakable
4604+
4605+
q.text("*")
4606+
q.format(right)
4607+
end
4608+
4609+
q.breakable("")
4610+
q.text("]")
45864611
end
45874612
end
45884613
end

test/fixtures/rassign.rb

+8
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@
1212
-
1313
foooooooooooooooooooooooooooooooooooooo =>
1414
barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
15+
%
16+
foo => [
17+
ConstantConstantConstant,
18+
ConstantConstantConstant,
19+
ConstantConstantConstant,
20+
ConstantConstantConstant,
21+
ConstantConstantConstant
22+
]

0 commit comments

Comments
 (0)