From 3eb7af7222c42ba56a886cbbd7cd5615d6ad36d6 Mon Sep 17 00:00:00 2001 From: Gil Ben-Shachar <111221618+gilbens-starkware@users.noreply.github.com> Date: Sun, 15 Sep 2024 09:57:11 +0300 Subject: [PATCH] Fix formatting precedence of for loop parts. (#6371) --- .../src/test/language_features/for_test.cairo | 6 ++--- .../src/node_properties.rs | 22 +++++++++++++++++++ .../test_data/cairo_files/linebreaking.cairo | 1 + .../expected_results/linebreaking.cairo | 5 +++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/corelib/src/test/language_features/for_test.cairo b/corelib/src/test/language_features/for_test.cairo index 6fa8feb31d8..9c29b2b7e83 100644 --- a/corelib/src/test/language_features/for_test.cairo +++ b/corelib/src/test/language_features/for_test.cairo @@ -21,9 +21,9 @@ fn test_for_loop_array_variables() { #[test] fn test_for_loop_array_tuples() { let mut i = 10; - for ( - x, y - ) in array![(10, 10), (11, 11), (12, 12)] { + for (x, y) in array![ + (10, 10), (11, 11), (12, 12), (13, 13), (14, 14), (15, 15), (16, 16), (17, 17) + ] { assert_eq!(x, i); assert_eq!(y, i); i += 1; diff --git a/crates/cairo-lang-formatter/src/node_properties.rs b/crates/cairo-lang-formatter/src/node_properties.rs index a69307bd586..ba682973b9c 100644 --- a/crates/cairo-lang-formatter/src/node_properties.rs +++ b/crates/cairo-lang-formatter/src/node_properties.rs @@ -299,6 +299,28 @@ impl SyntaxNodeFormat for SyntaxNode { | SyntaxKind::ExprUnary => Some(10), _ => None, }, + Some(SyntaxKind::ExprFor) => match self.kind(db) { + SyntaxKind::ExprBlock => Some(1), + SyntaxKind::ExprBinary + | SyntaxKind::ExprErrorPropagate + | SyntaxKind::ExprFieldInitShorthand + | SyntaxKind::ExprFunctionCall + | SyntaxKind::ExprIf + | SyntaxKind::ExprList + | SyntaxKind::ExprMatch + | SyntaxKind::ExprMissing + | SyntaxKind::ExprParenthesized + | SyntaxKind::ExprPath + | SyntaxKind::ExprStructCtorCall + | SyntaxKind::ExprListParenthesized + | SyntaxKind::ExprUnary + | SyntaxKind::ExprInlineMacro => Some(2), + SyntaxKind::PatternEnum + | SyntaxKind::PatternTuple + | SyntaxKind::PatternStruct + | SyntaxKind::PatternFixedSizeArray => Some(10), + _ => None, + }, Some(SyntaxKind::StatementLet) => match self.kind(db) { SyntaxKind::ExprBinary | SyntaxKind::ExprBlock diff --git a/crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo b/crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo index 485b9927da2..9ce18d113cd 100644 --- a/crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo +++ b/crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo @@ -10,6 +10,7 @@ fn foo(x: T) -> S { let x6 = (1+0+(2+0+(3+0+(4+0+(5+0+(6+0+(7+0+(8+0+(9+0+(1+0+(2+0+(3+0+(4+0))))))))))))); for i in 1 .. 2 {} for i in 1+2+3+4+1+2+3+4+1+2+3+4+1+2+3+4+1+2+3+4..1+2+3+4+1+2+3+4+1+2+3+4+1+2+3+4 {} + for (x, y) in array![(10, 10), (11, 11), (12, 12), (13, 13), (14, 14), (15, 15), (16, 16), (17, 17)] {do_something!(x, i); }; } fn bar(first_arg: T, second_arg: T, third_arg: T, fourth_arg: T, fifth_arg: T, sixth_arg: T, seventh_arg: T,) -> T { diff --git a/crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo b/crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo index f1aec22decd..9e77fe00a2c 100644 --- a/crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo +++ b/crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo @@ -113,6 +113,11 @@ fn foo(x: T) -> S { for i in 1..2 {} for i in 1 + 2 + 3 + 4 + 1 + 2 + 3 + 4 + 1 + 2 + 3 + 4 + 1 + 2 + 3 + 4 + 1 + 2 + 3 + 4 ..1 + 2 + 3 + 4 + 1 + 2 + 3 + 4 + 1 + 2 + 3 + 4 + 1 + 2 + 3 + 4 {} + for (x, y) in array![ + (10, 10), (11, 11), (12, 12), (13, 13), (14, 14), (15, 15), (16, 16), (17, 17) + ] { + do_something!(x, i); + }; } fn bar(