Skip to content

Method prefixes #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2022
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
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