diff --git a/fixtures/small/block_local_variables_expected.rb b/fixtures/small/block_local_variables_expected.rb index 9fa6566a..35d5ef6a 100644 --- a/fixtures/small/block_local_variables_expected.rb +++ b/fixtures/small/block_local_variables_expected.rb @@ -1,10 +1,10 @@ y = 12 -lambda { |x, ;y| +lambda do |x, ;y| puts(x) puts(y) y = 19 puts(y) -} +end .call(17) puts(y) diff --git a/fixtures/small/blocks_with_only_comments_expected.rb b/fixtures/small/blocks_with_only_comments_expected.rb index 1166db4f..b629db05 100644 --- a/fixtures/small/blocks_with_only_comments_expected.rb +++ b/fixtures/small/blocks_with_only_comments_expected.rb @@ -15,7 +15,7 @@ def foo end # More comments!! - commented_brace_block { + commented_brace_block do # Such plainness of the pre-baroque # Hardly involves the eye, until # It meets his left-hand gauntlet, still @@ -23,7 +23,7 @@ def foo # One sees, with a sharp tender shock, # His hand withdrawn, holding her hand. - } + end method_call end diff --git a/fixtures/small/brace_to_do_blocks_actual.rb b/fixtures/small/brace_to_do_blocks_actual.rb new file mode 100644 index 00000000..92f9672d --- /dev/null +++ b/fixtures/small/brace_to_do_blocks_actual.rb @@ -0,0 +1,12 @@ +things.map { |thing| stuff; thing ** 2 } + +things.map { |thing| + # A comment makes this multiline! + thing +} + +class Foo + # !! `example_dsl [].map { |k| k; k+1 }` is not equivalent to + # !! `example_dsl [].map do |k| k; k+1 end` + example_dsl [].map { |k| k; k+1 } +end diff --git a/fixtures/small/brace_to_do_blocks_expected.rb b/fixtures/small/brace_to_do_blocks_expected.rb new file mode 100644 index 00000000..f4e64c33 --- /dev/null +++ b/fixtures/small/brace_to_do_blocks_expected.rb @@ -0,0 +1,20 @@ +things.map do |thing| + stuff + thing ** 2 +end + +things.map do |thing| + # A comment makes this multiline! + thing +end + +class Foo + # !! `example_dsl [].map { |k| k; k+1 }` is not equivalent to + # !! `example_dsl [].map do |k| k; k+1 end` + example_dsl( + [].map do |k| + k + k + 1 + end + ) +end diff --git a/fixtures/small/curly_line_breaking_expected.rb b/fixtures/small/curly_line_breaking_expected.rb index 7cbad2d1..30308007 100644 --- a/fixtures/small/curly_line_breaking_expected.rb +++ b/fixtures/small/curly_line_breaking_expected.rb @@ -1,5 +1,5 @@ do_something { |a| [a] } -do_something { |a| +do_something do |a| [ 1, 2, @@ -105,4 +105,4 @@ 3, 3 ] -} +end diff --git a/fixtures/small/method_chains_expected.rb b/fixtures/small/method_chains_expected.rb index 99ed221d..ec99f3a8 100644 --- a/fixtures/small/method_chains_expected.rb +++ b/fixtures/small/method_chains_expected.rb @@ -1,8 +1,8 @@ returns_array - .map { |foo| + .map do |foo| thing = foo.idk thing.call - } + end .chain do |bar| bar.do_stuff! end @@ -110,10 +110,10 @@ def example .foo .bar .baz - .map { |x| + .map do |x| multiline block - } + end .bacon .next_call( a: "", diff --git a/fixtures/small/multiline_chain_in_block_expected.rb b/fixtures/small/multiline_chain_in_block_expected.rb index 95bee5bd..b5d72be7 100644 --- a/fixtures/small/multiline_chain_in_block_expected.rb +++ b/fixtures/small/multiline_chain_in_block_expected.rb @@ -1,8 +1,8 @@ -sig { +sig do params( route: String ).void -} +end def ajax_get(route) super end diff --git a/librubyfmt/src/format.rs b/librubyfmt/src/format.rs index 8cf9f7e4..c27621f8 100644 --- a/librubyfmt/src/format.rs +++ b/librubyfmt/src/format.rs @@ -3072,7 +3072,20 @@ fn should_multiline_call_chain(ps: &mut dyn ConcreteParserState, method_call: &M } pub fn format_block(ps: &mut dyn ConcreteParserState, b: Block) { - match b { + let block = match b { + Block::DoBlock(..) => b, + Block::BraceBlock(bb) => { + // Transform multiline BraceBlocks into do/end blocks + if bb.2.len() > 1 + || ps.will_render_as_multiline(Box::new(|ps| format_brace_block(ps, bb.clone()))) + { + Block::DoBlock(bb.into_do_block()) + } else { + Block::BraceBlock(bb) + } + } + }; + match block { Block::BraceBlock(bb) => format_brace_block(ps, bb), Block::DoBlock(db) => format_do_block(ps, db), } diff --git a/librubyfmt/src/ripper_tree_types.rs b/librubyfmt/src/ripper_tree_types.rs index 26f47cff..a7d70911 100644 --- a/librubyfmt/src/ripper_tree_types.rs +++ b/librubyfmt/src/ripper_tree_types.rs @@ -1912,6 +1912,17 @@ pub struct BraceBlock( pub StartEnd, ); +impl BraceBlock { + pub fn into_do_block(self) -> DoBlock { + DoBlock( + do_block_tag, + self.1, + Box::new(BodyStmt(bodystmt_tag, self.2, None, None, None)), + self.3, + ) + } +} + def_tag!(while_tag, "while"); #[derive(Deserialize, Debug, Clone)] pub struct While(