Skip to content

Commit

Permalink
Better formatting for array patterns in rassign
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed May 20, 2022
1 parent cc1f4c8 commit b8ec43e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
53 changes: 39 additions & 14 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,11 @@ def format(q)
q.group do
q.format(constant)
q.text("[")
q.seplist(parts) { |part| q.format(part) }
q.indent do
q.breakable("")
q.seplist(parts) { |part| q.format(part) }
end
q.breakable("")
q.text("]")
end

Expand All @@ -1141,7 +1145,11 @@ def format(q)
parent = q.parent
if parts.length == 1 || PATTERNS.include?(parent.class)
q.text("[")
q.seplist(parts) { |part| q.format(part) }
q.indent do
q.breakable("")
q.seplist(parts) { |part| q.format(part) }
end
q.breakable("")
q.text("]")
elsif parts.empty?
q.text("[]")
Expand Down Expand Up @@ -2777,10 +2785,17 @@ def format(q)
q.format(value)
q.text(" ")
q.format(operator)
q.group do
q.indent do
q.breakable
q.format(pattern)

case pattern
in AryPtn | FndPtn | HshPtn
q.text(" ")
q.format(pattern)
else
q.group do
q.indent do
q.breakable
q.format(pattern)
end
end
end
end
Expand Down Expand Up @@ -4573,16 +4588,26 @@ def deconstruct_keys(_keys)

def format(q)
q.format(constant) if constant
q.group(0, "[", "]") do
q.text("*")
q.format(left)
q.comma_breakable

q.seplist(values) { |value| q.format(value) }
q.comma_breakable
q.group do
q.text("[")

q.text("*")
q.format(right)
q.indent do
q.breakable("")

q.text("*")
q.format(left)
q.comma_breakable

q.seplist(values) { |value| q.format(value) }
q.comma_breakable

q.text("*")
q.format(right)
end

q.breakable("")
q.text("]")
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/rassign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@
-
foooooooooooooooooooooooooooooooooooooo =>
barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
%
foo => [
ConstantConstantConstant,
ConstantConstantConstant,
ConstantConstantConstant,
ConstantConstantConstant,
ConstantConstantConstant
]

0 comments on commit b8ec43e

Please sign in to comment.