diff --git a/sway-fmt-v2/src/items/item_fn.rs b/sway-fmt-v2/src/items/item_fn.rs index 4bcb2c4d3b8..eac33372535 100644 --- a/sway-fmt-v2/src/items/item_fn.rs +++ b/sway-fmt-v2/src/items/item_fn.rs @@ -40,12 +40,12 @@ impl CurlyBrace for ItemFn { } ItemBraceStyle::SameLineWhere => match formatter.shape.has_where_clause { true => { - writeln!(line, "{}", open_brace)?; + write!(line, "{}", open_brace)?; formatter.shape.update_where_clause(); formatter.shape.block_indent(&formatter.config); } false => { - writeln!(line, " {}", open_brace)?; + write!(line, " {}", open_brace)?; formatter.shape.block_indent(&formatter.config); } }, diff --git a/sway-fmt-v2/src/utils/expr/code_block.rs b/sway-fmt-v2/src/utils/expr/code_block.rs index 829e3653da2..c996a5b4da5 100644 --- a/sway-fmt-v2/src/utils/expr/code_block.rs +++ b/sway-fmt-v2/src/utils/expr/code_block.rs @@ -15,6 +15,7 @@ impl Format for CodeBlockContents { formatted_code: &mut FormattedCode, formatter: &mut Formatter, ) -> Result<(), FormatterError> { + writeln!(formatted_code)?; for statement in self.statements.iter() { statement.format(formatted_code, formatter)?; } @@ -37,17 +38,17 @@ impl CurlyBrace for CodeBlockContents { line: &mut FormattedCode, formatter: &mut Formatter, ) -> Result<(), FormatterError> { + formatter.shape.block_indent(&formatter.config); + let brace_style = formatter.config.items.item_brace_style; match brace_style { ItemBraceStyle::AlwaysNextLine => { // Add openning brace to the next line. write!(line, "\n{}", Delimiter::Brace.as_open_char())?; - formatter.shape.block_indent(&formatter.config); } _ => { // Add opening brace to the same line write!(line, " {}", Delimiter::Brace.as_open_char())?; - formatter.shape.block_indent(&formatter.config); } } diff --git a/sway-fmt-v2/src/utils/expr/conditional.rs b/sway-fmt-v2/src/utils/expr/conditional.rs index ae1566f3f3b..f4f116e268d 100644 --- a/sway-fmt-v2/src/utils/expr/conditional.rs +++ b/sway-fmt-v2/src/utils/expr/conditional.rs @@ -126,6 +126,7 @@ impl CurlyBrace for MatchBranch { } } +// Later we should add logic to handle transforming `Block` -> `Expr` and vice versa. impl Format for MatchBranchKind { fn format( &self, @@ -138,7 +139,21 @@ impl Format for MatchBranchKind { comma_token_opt, } => { Self::open_curly_brace(formatted_code, formatter)?; - block.get().format(formatted_code, formatter)?; + let block = block.get(); + if block.statements.is_empty() && block.final_expr_opt.is_none() { + // even if there is no code block we still want to unindent + // before the closing brace + formatter.shape.block_unindent(&formatter.config); + } else { + block.format(formatted_code, formatter)?; + // we handle this here to avoid needless indents + formatter.shape.block_unindent(&formatter.config); + write!( + formatted_code, + "{}", + formatter.shape.indent.to_string(&formatter.config)? + )?; + } Self::close_curly_brace(formatted_code, formatter)?; if let Some(comma_token) = comma_token_opt { write!(formatted_code, "{}", comma_token.span().as_str())?; @@ -157,8 +172,9 @@ impl Format for MatchBranchKind { impl CurlyBrace for MatchBranchKind { fn open_curly_brace( line: &mut FormattedCode, - _formatter: &mut Formatter, + formatter: &mut Formatter, ) -> Result<(), FormatterError> { + formatter.shape.block_indent(&formatter.config); write!(line, "{}", Delimiter::Brace.as_open_char())?; Ok(()) } diff --git a/sway-fmt-v2/src/utils/expr/tests.rs b/sway-fmt-v2/src/utils/expr/tests.rs index 1e90ca4da37..4bca45c1ad4 100644 --- a/sway-fmt-v2/src/utils/expr/tests.rs +++ b/sway-fmt-v2/src/utils/expr/tests.rs @@ -115,3 +115,19 @@ fmt_test!( multiline_tuple "(\n \"reallyreallylongstring\",\n \"yetanothe fmt_test!( multiline_match_stmt "match foo {\n Foo::foo => {}\n Foo::bar => {}\n}", intermediate_whitespace "match foo {\n Foo::foo => {}\n Foo::bar => {}\n}" ); + +fmt_test!( match_branch_kind +"match foo { + Foo::foo => { + foo(); + bar(); + } + Foo::bar => { + baz(); + quux(); + } +}", + 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}" +);