Skip to content

Commit

Permalink
Fixes issue with multiline collections formatting. (FuelLabs#3158)
Browse files Browse the repository at this point in the history
Fixes issue FuelLabs#3014.

The following code ...
```
let x = (
    "hello",
    "world",
);
```
... was formatted into:
```
let x = ("hello", "world", );
```
After the fix it is formatted into:
```
let x = ("hello", "world");
```

Co-authored-by: Joshua Batty <[email protected]>
Co-authored-by: Kaya Gökalp <[email protected]>
  • Loading branch information
3 people authored Oct 28, 2022
1 parent c0880eb commit ca70f23
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions swayfmt/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,28 @@ fn struct_destructuring() {
nested_tuple: (a, (b, (c, d))),
} = tuple_in_struct;
}
"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert_eq!(correct_sway_code, formatted_sway_code);
assert!(test_stability(formatted_sway_code, formatter));
}

#[test]
fn test_multiline_collections() {
let sway_code_to_format = r#"library test_multiline_collections;
fn func_with_multiline_collections() {
let x = (
"hello",
"world",
);
}
"#;
let correct_sway_code = r#"library test_multiline_collections;
fn func_with_multiline_collections() {
let x = ("hello", "world");
}
"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Expand Down
3 changes: 3 additions & 0 deletions swayfmt/src/utils/language/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ where

if let Some(final_value) = &self.final_value_opt {
final_value.format(formatted_code, formatter)?;
} else {
formatted_code.pop();
formatted_code.pop();
}
}
LineStyle::Inline => {
Expand Down

0 comments on commit ca70f23

Please sign in to comment.