Skip to content

Commit

Permalink
fix(fmt): match branch enum extra newline (FuelLabs#4084)
Browse files Browse the repository at this point in the history
## Description

Closes FuelLabs#3131

Simple check to see if we already end with a newline - if we do, don't
write one.

We could probably have a helper fn that condenses this into one line to
make the `Format` impls take up less vertical space.

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

Co-authored-by: Joshua Batty <[email protected]>
  • Loading branch information
eightfilms and JoshuaBatty authored Feb 17, 2023
1 parent ada81db commit 18f90e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
36 changes: 36 additions & 0 deletions swayfmt/src/utils/language/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,42 @@ fmt_test!( match_branch_kind
quux();\n }\n\n\n}"
);

fmt_test!( match_branch_kind_tuple_long
"match (foo, bar) {
(
VeryLongFoo::SomeLongFoo(some_foo),
VeryLongBar::SomeLongBar(some_bar),
) => {
foo();
}
(
VeryLongFoo::OtherLongFoo(other_foo),
VeryLongBar::OtherLongBar(other_bar),
) => {
bar();
}
_ => {
revert(0)
}
}",
intermediate_whitespace
"match (foo, bar) {
(
VeryLongFoo::SomeLongFoo(some_foo), \n \n VeryLongBar::SomeLongBar(some_bar)) => \n
\n{
\n
foo();
}
(VeryLongFoo::OtherLongFoo(other_foo), VeryLongBar::OtherLongBar(other_bar) \n ) => {
bar();
\n
}
_ \n=> {
\n revert(0)
}
}"
);

fmt_test!( basic_array "[1, 2, 3, 4, 5]",
intermediate_whitespace " \n [ 1 , 2 , 3 , 4 , 5 ] \n"
);
Expand Down
4 changes: 3 additions & 1 deletion swayfmt/src/utils/language/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ where
write!(formatted_code, " ")?;
}
LineStyle::Multiline => {
writeln!(formatted_code)?;
if !formatted_code.ends_with('\n') {
writeln!(formatted_code)?;
}
let value_pairs_iter = self.value_separator_pairs.iter();
for (type_field, comma_token) in value_pairs_iter.clone() {
write!(
Expand Down

0 comments on commit 18f90e4

Please sign in to comment.