From b8ec43eb46d0c28b740f46dbc8924a1452c81a79 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Thu, 19 May 2022 17:30:18 -0400 Subject: [PATCH] Better formatting for array patterns in rassign --- lib/syntax_tree/node.rb | 53 +++++++++++++++++++++++++++++----------- test/fixtures/rassign.rb | 8 ++++++ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index 0a1fc394..6c2617cc 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -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 @@ -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("[]") @@ -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 @@ -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 diff --git a/test/fixtures/rassign.rb b/test/fixtures/rassign.rb index 882ce890..ce749550 100644 --- a/test/fixtures/rassign.rb +++ b/test/fixtures/rassign.rb @@ -12,3 +12,11 @@ - foooooooooooooooooooooooooooooooooooooo => barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr +% +foo => [ + ConstantConstantConstant, + ConstantConstantConstant, + ConstantConstantConstant, + ConstantConstantConstant, + ConstantConstantConstant +]