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

Commit db96880

Browse files
committed
Handle trailing operators if necessary
1 parent 6a74297 commit db96880

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

lib/syntax_tree/node.rb

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,18 +2206,14 @@ def format(q)
22062206
end
22072207

22082208
class CallChainFormatter
2209-
# [Call] the top of the call chain
2209+
# [Call | MethodAddBlock] the top of the call chain
22102210
attr_reader :node
22112211

22122212
def initialize(node)
22132213
@node = node
22142214
end
22152215

22162216
def format(q)
2217-
q.group { q.if_break { format_chain(q) }.if_flat { node.format_contents(q) } }
2218-
end
2219-
2220-
def format_chain(q)
22212217
children = [node]
22222218

22232219
# First, walk down the chain until we get to the point where we're not
@@ -2235,6 +2231,14 @@ def format_chain(q)
22352231
end
22362232
end
22372233

2234+
if children.length > 2
2235+
q.group { q.if_break { format_chain(q, children) }.if_flat { node.format_contents(q) } }
2236+
else
2237+
node.format_contents(q)
2238+
end
2239+
end
2240+
2241+
def format_chain(q, children)
22382242
# We're going to have some specialized behavior for if it's an entire
22392243
# chain of calls without arguments except for the last one. This is common
22402244
# enough in Ruby source code that it's worth the extra complexity here.
@@ -2256,6 +2260,11 @@ def format_chain(q)
22562260
end
22572261

22582262
q.indent do
2263+
# We track another variable that checks if you need to move the
2264+
# operator to the previous line in case there are trailing comments
2265+
# and a trailing operator.
2266+
skip_operator = false
2267+
22592268
while child = children.pop
22602269
case child
22612270
in Call[receiver: Call[message: { value: "where" }], message: { value: "not" }]
@@ -2270,7 +2279,17 @@ def format_chain(q)
22702279
else
22712280
end
22722281

2273-
format_child(q, child, skip_attached: empty_except_last && children.empty?)
2282+
format_child(q, child, skip_operator: skip_operator, skip_attached: empty_except_last && children.empty?)
2283+
2284+
# If the parent call node has a comment on the message then we need
2285+
# to print the operator trailing in order to keep it working.
2286+
case children.last
2287+
in Call[message: { comments: [_, *] }, operator:]
2288+
q.format(CallOperatorFormatter.new(operator))
2289+
skip_operator = true
2290+
else
2291+
skip_operator = false
2292+
end
22742293

22752294
# Pop off the formatter's stack so that it aligns with what would
22762295
# have happened if we had been formatting normally.
@@ -2308,11 +2327,11 @@ def attach_directly?(child)
23082327
.include?(child.receiver.class)
23092328
end
23102329

2311-
def format_child(q, child, skip_attached: false)
2330+
def format_child(q, child, skip_operator: false, skip_attached: false)
23122331
# First, format the actual contents of the child.
23132332
case child
23142333
in Call
2315-
q.format(CallOperatorFormatter.new(child.operator))
2334+
q.format(CallOperatorFormatter.new(child.operator)) unless skip_operator
23162335
q.format(child.message) if child.message != :call
23172336
child.format_arguments(q) unless skip_attached
23182337
in MethodAddBlock
@@ -5918,6 +5937,17 @@ def deconstruct_keys(keys)
59185937
end
59195938

59205939
def format(q)
5940+
# If we're at the top of a call chain, then we're going to do some
5941+
# specialized printing in case we can print it nicely. We _only_ do this
5942+
# at the top of the chain to avoid weird recursion issues.
5943+
if !CallChainFormatter.chained?(q.parent) && CallChainFormatter.chained?(call)
5944+
q.group { q.if_break { CallChainFormatter.new(self).format(q) }.if_flat { format_contents(q) } }
5945+
else
5946+
format_contents(q)
5947+
end
5948+
end
5949+
5950+
def format_contents(q)
59215951
q.format(call)
59225952
q.format(block)
59235953
end

0 commit comments

Comments
 (0)