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
30 changes: 21 additions & 9 deletions sway-fmt-v2/src/utils/expr/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ impl Format for IfExpr {
Self::open_curly_brace(formatted_code, formatter)?;
self.then_block.get().format(formatted_code, formatter)?;
Self::close_curly_brace(formatted_code, formatter)?;
if let Some(else_opt) = &self.else_opt {
write!(formatted_code, "{} ", else_opt.0.span().as_str())?;
match &else_opt.1 {
ControlFlow::Continue(if_expr) => if_expr.format(formatted_code, formatter)?,
if let Some((else_token, control_flow)) = &self.else_opt {
write!(formatted_code, " {}", else_token.span().as_str())?;
match &control_flow {
ControlFlow::Continue(if_expr) => {
write!(formatted_code, " ")?;
if_expr.format(formatted_code, formatter)?
}
ControlFlow::Break(code_block_contents) => {
Self::open_curly_brace(formatted_code, formatter)?;
code_block_contents
Expand All @@ -41,16 +44,25 @@ impl Format for IfExpr {
impl CurlyBrace for IfExpr {
fn open_curly_brace(
line: &mut FormattedCode,
_formatter: &mut Formatter,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(line, "{}", Delimiter::Brace.as_open_char())?;
formatter.shape.block_indent(&formatter.config);
write!(line, " {}", Delimiter::Brace.as_open_char())?;

Ok(())
}
fn close_curly_brace(
line: &mut FormattedCode,
_formatter: &mut Formatter,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(line, "{}", Delimiter::Brace.as_close_char())?;
formatter.shape.block_unindent(&formatter.config);
write!(
line,
"{}{}",
formatter.shape.indent.to_string(&formatter.config)?,
Delimiter::Brace.as_close_char()
)?;

Ok(())
}
}
Expand All @@ -71,7 +83,7 @@ impl Format for IfCondition {
eq_token,
rhs,
} => {
write!(formatted_code, "{} ", let_token.span().as_str())?;
write!(formatted_code, " {} ", let_token.span().as_str())?;
lhs.format(formatted_code, formatter)?;
write!(formatted_code, " {} ", eq_token.span().as_str())?;
rhs.format(formatted_code, formatter)?;
Expand Down
5 changes: 5 additions & 0 deletions sway-fmt-v2/src/utils/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ fmt_test!( multiline_match_stmt "match foo {\n Foo::foo => {}\n Foo::bar
intermediate_whitespace "match foo {\n Foo::foo => {}\n Foo::bar => {}\n}"
);

fmt_test!( if_else_block "if foo {\n foo();\n} else if bar {\n bar();\n} else {\n baz();\n}",
intermediate_whitespace " if foo { \n foo( ) ; \n } else if bar { \n bar( ) ; \n } else { \n baz(\n) ; \n }\n\n"
);

fmt_test!( match_branch_kind
"match foo {
Foo::foo => {
Expand All @@ -130,4 +134,5 @@ fmt_test!( match_branch_kind
intermediate_whitespace "match foo

\n{\n\n Foo::foo => {\n foo() ; \n bar( ); \n } \n Foo::\nbar => {\n baz();\n quux();\n }\n\n\n}"

);