diff --git a/swayfmt/src/formatter/mod.rs b/swayfmt/src/formatter/mod.rs index 0bf1c6de44e..c5209a21451 100644 --- a/swayfmt/src/formatter/mod.rs +++ b/swayfmt/src/formatter/mod.rs @@ -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 = diff --git a/swayfmt/src/utils/language/expr/tests.rs b/swayfmt/src/utils/language/expr/tests.rs index c67d7dcf542..4b81f5eef46 100644 --- a/swayfmt/src/utils/language/expr/tests.rs +++ b/swayfmt/src/utils/language/expr/tests.rs @@ -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 }" ); diff --git a/swayfmt/src/utils/language/statement.rs b/swayfmt/src/utils/language/statement.rs index 3d755d9e049..f19d8664fc7 100644 --- a/swayfmt/src/utils/language/statement.rs +++ b/swayfmt/src/utils/language/statement.rs @@ -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(()) }