Skip to content

Commit

Permalink
Fix formatting precedence of for loop parts. (#6371)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbens-starkware authored Sep 15, 2024
1 parent d6163bd commit 3eb7af7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions corelib/src/test/language_features/for_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions crates/cairo-lang-formatter/src/node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Check failure on line 11 in crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo

View workflow job for this annotation

GitHub Actions / Test tree-sitter parser

ERROR (Linux)

crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo 0.71 ms 5585 bytes/ms (ERROR [10, 16] - [10, 17])

Check failure on line 11 in crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo

View workflow job for this annotation

GitHub Actions / Test tree-sitter parser

ERROR (Linux)

crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo 0.74 ms 5369 bytes/ms (ERROR [10, 16] - [10, 17])

Check failure on line 11 in crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo

View workflow job for this annotation

GitHub Actions / Test tree-sitter parser

ERROR (Linux)

crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo 0.72 ms 5485 bytes/ms (ERROR [10, 16] - [10, 17])
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ fn foo(x: T) -> S {
for i in 1..2 {}

Check failure on line 113 in crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo

View workflow job for this annotation

GitHub Actions / Test tree-sitter parser

ERROR (Linux)

crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo 0.85 ms 7866 bytes/ms (ERROR [112, 15] - [112, 16])

Check failure on line 113 in crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo

View workflow job for this annotation

GitHub Actions / Test tree-sitter parser

ERROR (Linux)

crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo 0.87 ms 7741 bytes/ms (ERROR [112, 15] - [112, 16])

Check failure on line 113 in crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo

View workflow job for this annotation

GitHub Actions / Test tree-sitter parser

ERROR (Linux)

crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo 0.85 ms 7856 bytes/ms (ERROR [112, 15] - [112, 16])
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(
Expand Down

0 comments on commit 3eb7af7

Please sign in to comment.