Skip to content

Commit

Permalink
Merge pull request #380 from etiennebarrie/lambda-keyword-block
Browse files Browse the repository at this point in the history
Always use do/end for multiline lambdas
  • Loading branch information
kddnewton authored Jun 1, 2023
2 parents 20c62ef + 32d6d8a commit 909b45a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
33 changes: 7 additions & 26 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7210,36 +7210,17 @@ def format(q)
q.text(" ")
q
.if_break do
force_parens =
q.parents.any? do |node|
node.is_a?(Command) || node.is_a?(CommandCall)
end

if force_parens
q.text("{")
q.text("do")

unless statements.empty?
q.indent do
q.breakable_space
q.format(statements)
end
unless statements.empty?
q.indent do
q.breakable_space
q.format(statements)
end

q.text("}")
else
q.text("do")

unless statements.empty?
q.indent do
q.breakable_space
q.format(statements)
end
end

q.breakable_space
q.text("end")
end

q.breakable_space
q.text("end")
end
.if_flat do
q.text("{")
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/lambda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,31 @@
-> do # comment1
# comment2
end
% # multiline lambda in a command
command "arg" do
-> {
multi
line
}
end
-
command "arg" do
-> do
multi
line
end
end
% # multiline lambda in a command call
command.call "arg" do
-> {
multi
line
}
end
-
command.call "arg" do
-> do
multi
line
end
end

0 comments on commit 909b45a

Please sign in to comment.