Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Alignment on command and command call for ternaries
  • Loading branch information
kddnewton committed Apr 22, 2022
commit e739d62689e2cff52fc3526d65dde4b9a33b94d2
30 changes: 17 additions & 13 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2614,26 +2614,24 @@ def deconstruct_keys(keys)
def format(q)
q.group do
q.format(message)
q.text(" ")

if align?(self)
q.nest(message.value.length + 1) { q.format(arguments) }
else
q.format(arguments)
end
align(q, self) { q.format(arguments) }
end
end

private

def align?(node)
def align(q, node, &block)
case node.arguments
in Args[parts: [Def | Defs | DefEndless]]
false
q.text(" ")
yield
in Args[parts: [IfOp]]
yield
in Args[parts: [Command => command]]
align?(command)
align(q, command, &block)
else
true
q.text(" ")
q.nest(message.value.length + 1) { yield }
end
end
end
Expand Down Expand Up @@ -2705,9 +2703,15 @@ def format(q)
q.format(message)
end

if arguments
case arguments
in Args[parts: [IfOp]]
q.if_flat { q.text(" ") }
q.format(arguments)
in Args
q.text(" ")
q.nest(argument_alignment(q, doc)) { q.format(arguments) }
else
# If there are no arguments, print nothing.
end
end
end
Expand Down Expand Up @@ -8467,7 +8471,7 @@ def format(q)
if parentheses
q.text(")")
elsif ternary
q.if_break {}.if_flat { q.text(")") }
q.if_flat { q.text(")") }
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/syntax_tree/prettyprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ def if_break
IfBreakBuilder.new
end

# Also effectively unnecessary, but here for compatibility.
def if_flat
end

# A noop that immediately yields.
def indent
yield
Expand Down Expand Up @@ -1011,6 +1015,15 @@ def if_break
IfBreakBuilder.new(self, doc)
end

# This is similar to if_break in that it also inserts an IfBreak node into the
# print tree, however it's starting from the flat contents, and cannot be used
# to build the break contents.
def if_flat
doc = IfBreak.new

with_target(doc.flat_contents) { yield }
end

# Very similar to the #nest method, this indents the nested content by one
# level by inserting an Indent node into the print tree. The contents of the
# node are determined by the block.
Expand Down