Skip to content

Commit 329e7e2

Browse files
authored
Adds formatting for if_else control flow blocks (#2564)
* add formatting for if_else blocks * fmt
1 parent 1ed1cf1 commit 329e7e2

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

sway-fmt-v2/src/utils/expr/conditional.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ impl Format for IfExpr {
2020
Self::open_curly_brace(formatted_code, formatter)?;
2121
self.then_block.get().format(formatted_code, formatter)?;
2222
Self::close_curly_brace(formatted_code, formatter)?;
23-
if let Some(else_opt) = &self.else_opt {
24-
write!(formatted_code, "{} ", else_opt.0.span().as_str())?;
25-
match &else_opt.1 {
26-
ControlFlow::Continue(if_expr) => if_expr.format(formatted_code, formatter)?,
23+
if let Some((else_token, control_flow)) = &self.else_opt {
24+
write!(formatted_code, " {}", else_token.span().as_str())?;
25+
match &control_flow {
26+
ControlFlow::Continue(if_expr) => {
27+
write!(formatted_code, " ")?;
28+
if_expr.format(formatted_code, formatter)?
29+
}
2730
ControlFlow::Break(code_block_contents) => {
2831
Self::open_curly_brace(formatted_code, formatter)?;
2932
code_block_contents
@@ -41,16 +44,25 @@ impl Format for IfExpr {
4144
impl CurlyBrace for IfExpr {
4245
fn open_curly_brace(
4346
line: &mut FormattedCode,
44-
_formatter: &mut Formatter,
47+
formatter: &mut Formatter,
4548
) -> Result<(), FormatterError> {
46-
write!(line, "{}", Delimiter::Brace.as_open_char())?;
49+
formatter.shape.block_indent(&formatter.config);
50+
write!(line, " {}", Delimiter::Brace.as_open_char())?;
51+
4752
Ok(())
4853
}
4954
fn close_curly_brace(
5055
line: &mut FormattedCode,
51-
_formatter: &mut Formatter,
56+
formatter: &mut Formatter,
5257
) -> Result<(), FormatterError> {
53-
write!(line, "{}", Delimiter::Brace.as_close_char())?;
58+
formatter.shape.block_unindent(&formatter.config);
59+
write!(
60+
line,
61+
"{}{}",
62+
formatter.shape.indent.to_string(&formatter.config)?,
63+
Delimiter::Brace.as_close_char()
64+
)?;
65+
5466
Ok(())
5567
}
5668
}
@@ -71,7 +83,7 @@ impl Format for IfCondition {
7183
eq_token,
7284
rhs,
7385
} => {
74-
write!(formatted_code, "{} ", let_token.span().as_str())?;
86+
write!(formatted_code, " {} ", let_token.span().as_str())?;
7587
lhs.format(formatted_code, formatter)?;
7688
write!(formatted_code, " {} ", eq_token.span().as_str())?;
7789
rhs.format(formatted_code, formatter)?;

sway-fmt-v2/src/utils/expr/tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ fmt_test!( multiline_match_stmt "match foo {\n Foo::foo => {}\n Foo::bar
116116
intermediate_whitespace "match foo {\n Foo::foo => {}\n Foo::bar => {}\n}"
117117
);
118118

119+
fmt_test!( if_else_block "if foo {\n foo();\n} else if bar {\n bar();\n} else {\n baz();\n}",
120+
intermediate_whitespace " if foo { \n foo( ) ; \n } else if bar { \n bar( ) ; \n } else { \n baz(\n) ; \n }\n\n"
121+
);
122+
119123
fmt_test!( match_branch_kind
120124
"match foo {
121125
Foo::foo => {
@@ -130,4 +134,5 @@ fmt_test!( match_branch_kind
130134
intermediate_whitespace "match foo
131135
132136
\n{\n\n Foo::foo => {\n foo() ; \n bar( ); \n } \n Foo::\nbar => {\n baz();\n quux();\n }\n\n\n}"
137+
133138
);

0 commit comments

Comments
 (0)