Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c3c204d

Browse files
committed
Alignment on command and command call for ternaries
1 parent 20be531 commit c3c204d

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
@@ -2607,26 +2607,24 @@ def deconstruct_keys(keys)
26072607
def format(q)
26082608
q.group do
26092609
q.format(message)
2610-
q.text(" ")
2611-
2612-
if align?(self)
2613-
q.nest(message.value.length + 1) { q.format(arguments) }
2614-
else
2615-
q.format(arguments)
2616-
end
2610+
align(q, self) { q.format(arguments) }
26172611
end
26182612
end
26192613

26202614
private
26212615

2622-
def align?(node)
2616+
def align(q, node, &block)
26232617
case node.arguments
26242618
in Args[parts: [Def | Defs | DefEndless]]
2625-
false
2619+
q.text(" ")
2620+
yield
2621+
in Args[parts: [IfOp]]
2622+
yield
26262623
in Args[parts: [Command => command]]
2627-
align?(command)
2624+
align(q, command, &block)
26282625
else
2629-
true
2626+
q.text(" ")
2627+
q.nest(message.value.length + 1) { yield }
26302628
end
26312629
end
26322630
end
@@ -2698,9 +2696,15 @@ def format(q)
26982696
q.format(message)
26992697
end
27002698

2701-
if arguments
2699+
case arguments
2700+
in Args[parts: [IfOp]]
2701+
q.if_flat { q.text(" ") }
2702+
q.format(arguments)
2703+
in Args
27022704
q.text(" ")
27032705
q.nest(argument_alignment(q, doc)) { q.format(arguments) }
2706+
else
2707+
# If there are no arguments, print nothing.
27042708
end
27052709
end
27062710
end
@@ -8460,7 +8464,7 @@ def format(q)
84608464
if parentheses
84618465
q.text(")")
84628466
elsif ternary
8463-
q.if_break {}.if_flat { q.text(")") }
8467+
q.if_flat { q.text(")") }
84648468
end
84658469
end
84668470
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)