Skip to content

Commit

Permalink
Transform multiline brace blocks into do blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
reese committed Jan 4, 2023
1 parent 3b3a5fd commit 536ce6c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 14 additions & 1 deletion librubyfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
11 changes: 11 additions & 0 deletions librubyfmt/src/ripper_tree_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 536ce6c

Please sign in to comment.