Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Method prefixes
We shouldn't indent Command nodes if they end in Def/Defs/DefEndless nodes.
  • Loading branch information
kddnewton committed Apr 8, 2022
commit fb04c4a746e07e68148cfbe0c379c98d3f138271
50 changes: 43 additions & 7 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1998,12 +1998,18 @@ def format(q)
q.group { q.format(left) }
q.text(" ") unless power

q.group do
if operator == :<<
q.text(operator)
q.text(" ")
q.format(right)
else
q.group do
q.text(operator)

q.indent do
q.breakable(power ? "" : " ")
q.format(right)
q.indent do
q.breakable(power ? "" : " ")
q.format(right)
end
end
end
end
Expand Down Expand Up @@ -3253,7 +3259,12 @@ def format(q)
q.group do
q.format(message)
q.text(" ")
q.nest(message.value.length + 1) { q.format(arguments) }

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

Expand All @@ -3280,6 +3291,18 @@ def to_json(*opts)
cmts: comments
}.to_json(*opts)
end

private

def align?(node)
if node.arguments in Args[parts: [Def | Defs | DefEndless]]
false
elsif node.arguments in Args[parts: [Command => command]]
align?(command)
else
true
end
end
end

# CommandCall represents a method call on an object with arguments and no
Expand Down Expand Up @@ -7766,9 +7789,15 @@ def format(q)
q.format(target)
q.text(" ")
q.format(operator)
q.indent do
q.breakable

if skip_indent?
q.text(" ")
q.format(value)
else
q.indent do
q.breakable
q.format(value)
end
end
end
end
Expand Down Expand Up @@ -7800,6 +7829,13 @@ def to_json(*opts)
cmts: comments
}.to_json(*opts)
end

private

def skip_indent?
target.comments.empty? &&
(target.is_a?(ARefField) || AssignFormatting.skip_indent?(value))
end
end

# If you have a modifier statement (for instance a modifier if statement or a
Expand Down
24 changes: 24 additions & 0 deletions test/fixtures/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,27 @@
-
foo barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr,
bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
%
meta1 def foo
end
%
meta2 meta1 def foo
end
%
meta3 meta2 meta1 def foo
end
%
meta1 def self.foo
end
%
meta2 meta1 def self.foo
end
%
meta3 meta2 meta1 def self.foo
end
%
meta1 def foo = 1
%
meta2 meta1 def foo = 1
%
meta3 meta2 meta1 def foo = 1