Skip to content

Commit

Permalink
formatter: fix handling of statements ending with ; in if else bl…
Browse files Browse the repository at this point in the history
…ocks (FuelLabs#3557)

unblocks FuelLabs#3556
closes FuelLabs#3555 

~The situation occurring in the issue seems to happen when the formatter
tries to (incorrectly) assume that an if expression can be in-lined when
there's a newline character inside. During this check, the line kind
(multi-line vs inline) is decided solely depending on the width of the
line, ignoring the existence of a `\n` character.~

~This PR introduces something like a 'catch-all' during the above check
to default to `LineKind::Multiline` if a `\n` is found during the
look-ahead formatting done by the temporary formatter, which forces the
formatter to use multi-line style, so that at the very least we don't
end up with a broken inline format as seen in the issue.~

~This catch-all might unblock the comment formatting PR above as well.~

The above was my misunderstanding - the mistake was further upstream
instead within statement formatting. The inline style was not being
handled for let statements.
  • Loading branch information
eightfilms authored Dec 11, 2022
1 parent 4264e2f commit dc60866
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
55 changes: 55 additions & 0 deletions swayfmt/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,61 @@ pub trait Foo {
}
fn main() {}
"#;
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_if_else_multiline_to_inline() {
let sway_code_to_format = r#"script;
fn main() {
if foo {
let x = 1;
} else {
bar(y) ;
}
}
"#;
let correct_sway_code = r#"script;
fn main() {
if foo { let x = 1; } else { bar(y); }
}
"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
println!("formatted: {}", formatted_sway_code);
assert_eq!(correct_sway_code, formatted_sway_code);
assert!(test_stability(formatted_sway_code, formatter));
}

#[test]
fn test_if_else_retain_multiline() {
let sway_code_to_format = r#"script;
fn main() {
if foo {
let really_long_variable = 1;
} else {
bar(y) ;
}
}
"#;
let correct_sway_code = r#"script;
fn main() {
if foo {
let really_long_variable = 1;
} else {
bar(y);
}
}
"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Expand Down
27 changes: 26 additions & 1 deletion swayfmt/src/utils/language/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,34 @@ other_really_long_var
0 && other_really_long_var != 0 { foo(); }else{bar();}"
);

fmt_test!( if_else_control_flow "if foo { break; } else { continue; }",
fmt_test!( if_else_inline_1 "if foo { break; } else { continue; }",
intermediate_whitespace "if foo { \n break; \n} else {\n continue; \n}");

fmt_test!( if_else_inline_2
"if foo { let x = 1; } else { bar(y); }"
,

intermediate_whitespace
" if foo {
let x = 1;
} else {
bar(y) ;
} ");

fmt_test!( if_else_multiline
"if foo {
let really_long_variable = 1;
} else {
bar(y);
}",

intermediate_whitespace
" if foo {
let really_long_variable = 1;
} else {
bar(y) ;
} ");

fmt_test!( small_if_let "if let Result::Ok(x) = x { 100 } else { 1 }",
intermediate_whitespace "if let Result :: Ok( x ) = x { 100 } else { 1 }"
);
Expand Down
9 changes: 7 additions & 2 deletions swayfmt/src/utils/language/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ impl Format for StatementLet {
write!(formatted_code, " {} ", self.eq_token.span().as_str())?;
// expr
self.expr.format(formatted_code, formatter)?;
// `;\n`
writeln!(formatted_code, "{}", self.semicolon_token.span().as_str())?;
if formatter.shape.code_line.line_style == LineStyle::Inline {
// `;`
write!(formatted_code, "{}", self.semicolon_token.span().as_str())?;
} else {
// `;\n`
writeln!(formatted_code, "{}", self.semicolon_token.span().as_str())?;
}

Ok(())
}
Expand Down

0 comments on commit dc60866

Please sign in to comment.