Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions sway-fmt-v2/src/items/item_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},
Expand Down
5 changes: 3 additions & 2 deletions sway-fmt-v2/src/utils/expr/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand All @@ -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);
}
}

Expand Down
20 changes: 18 additions & 2 deletions sway-fmt-v2/src/utils/expr/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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())?;
Expand All @@ -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(())
}
Expand Down
16 changes: 16 additions & 0 deletions sway-fmt-v2/src/utils/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
);