Skip to content

Commit e739d62

Browse files
committed
Alignment on command and command call for ternaries
1 parent b38f7e1 commit e739d62

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

lib/syntax_tree/node.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,26 +2614,24 @@ def deconstruct_keys(keys)
26142614
def format(q)
26152615
q.group do
26162616
q.format(message)
2617-
q.text(" ")
2618-
2619-
if align?(self)
2620-
q.nest(message.value.length + 1) { q.format(arguments) }
2621-
else
2622-
q.format(arguments)
2623-
end
2617+
align(q, self) { q.format(arguments) }
26242618
end
26252619
end
26262620

26272621
private
26282622

2629-
def align?(node)
2623+
def align(q, node, &block)
26302624
case node.arguments
26312625
in Args[parts: [Def | Defs | DefEndless]]
2632-
false
2626+
q.text(" ")
2627+
yield
2628+
in Args[parts: [IfOp]]
2629+
yield
26332630
in Args[parts: [Command => command]]
2634-
align?(command)
2631+
align(q, command, &block)
26352632
else
2636-
true
2633+
q.text(" ")
2634+
q.nest(message.value.length + 1) { yield }
26372635
end
26382636
end
26392637
end
@@ -2705,9 +2703,15 @@ def format(q)
27052703
q.format(message)
27062704
end
27072705

2708-
if arguments
2706+
case arguments
2707+
in Args[parts: [IfOp]]
2708+
q.if_flat { q.text(" ") }
2709+
q.format(arguments)
2710+
in Args
27092711
q.text(" ")
27102712
q.nest(argument_alignment(q, doc)) { q.format(arguments) }
2713+
else
2714+
# If there are no arguments, print nothing.
27112715
end
27122716
end
27132717
end
@@ -8467,7 +8471,7 @@ def format(q)
84678471
if parentheses
84688472
q.text(")")
84698473
elsif ternary
8470-
q.if_break {}.if_flat { q.text(")") }
8474+
q.if_flat { q.text(")") }
84718475
end
84728476
end
84738477
end

lib/syntax_tree/prettyprint.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,10 @@ def if_break
458458
IfBreakBuilder.new
459459
end
460460

461+
# Also effectively unnecessary, but here for compatibility.
462+
def if_flat
463+
end
464+
461465
# A noop that immediately yields.
462466
def indent
463467
yield
@@ -1011,6 +1015,15 @@ def if_break
10111015
IfBreakBuilder.new(self, doc)
10121016
end
10131017

1018+
# This is similar to if_break in that it also inserts an IfBreak node into the
1019+
# print tree, however it's starting from the flat contents, and cannot be used
1020+
# to build the break contents.
1021+
def if_flat
1022+
doc = IfBreak.new
1023+
1024+
with_target(doc.flat_contents) { yield }
1025+
end
1026+
10141027
# Very similar to the #nest method, this indents the nested content by one
10151028
# level by inserting an Indent node into the print tree. The contents of the
10161029
# node are determined by the block.

0 commit comments

Comments
 (0)