From ec1caa6828af50e96fc0773b7479bad71f590567 Mon Sep 17 00:00:00 2001 From: dean-starkware Date: Mon, 13 Jan 2025 16:08:58 +0200 Subject: [PATCH] added closures to protected zone (#7065) --- .../src/node_properties.rs | 15 +++++++----- .../test_data/cairo_files/linebreaking.cairo | 5 ++++ .../expected_results/linebreaking.cairo | 23 +++++++++++++------ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/crates/cairo-lang-formatter/src/node_properties.rs b/crates/cairo-lang-formatter/src/node_properties.rs index 014266a7bee..ed5581739df 100644 --- a/crates/cairo-lang-formatter/src/node_properties.rs +++ b/crates/cairo-lang-formatter/src/node_properties.rs @@ -282,6 +282,14 @@ impl SyntaxNodeFormat for SyntaxNode { | SyntaxKind::ExprUnary => Some(3), _ => None, }, + + Some(SyntaxKind::ExprClosure) => match self.kind(db) { + SyntaxKind::ClosureParamWrapperNAry => Some(3), + SyntaxKind::ReturnTypeClause => Some(2), + SyntaxKind::ExprBlock => Some(1), + _ => None, + }, + Some(SyntaxKind::ExprIf) => match self.kind(db) { SyntaxKind::ExprBlock => Some(1), SyntaxKind::ConditionExpr | SyntaxKind::ConditionLet => Some(2), @@ -533,12 +541,7 @@ impl SyntaxNodeFormat for SyntaxNode { trailing: trailing_break_point, } } - SyntaxKind::ParamList - if !matches!( - parent_kind(db, self), - Some(SyntaxKind::ClosureParamWrapperNAry) - ) => - { + SyntaxKind::ParamList => { let leading_break_point = BreakLinePointProperties::new( 2, BreakLinePointIndentation::IndentedWithTail, 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 841523493a8..9cf9725167c 100644 --- a/crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo +++ b/crates/cairo-lang-formatter/test_data/cairo_files/linebreaking.cairo @@ -82,3 +82,8 @@ fn comment_remains_after_last_comma() { fn struct_line_breaking() { let MyStruct {long_long_long_long_key_a, long_long_long_long_key_b, long_long_long_long_key_c, long_long_long_long_key_d, long_long_long_long_key_e, } = my_val; } +fn closure_line_breaking() { + let closure_with_a_very_very_very_very_very_very_very_very_large_name = |left: usize, right: usize| -> usize { + left + right + }; +} 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 e16e6b0bdd7..608880f7a2e 100644 --- a/crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo +++ b/crates/cairo-lang-formatter/test_data/expected_results/linebreaking.cairo @@ -161,13 +161,15 @@ fn bar( 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 21 | 2 | 2 | 3 => 4, } - let foo = |first_arg: T, - second_arg: T, - third_arg: T, - fourth_arg: T, - fifth_arg: T, - sixth_arg: T, - seventh_arg: T| { + let foo = | + first_arg: T, + second_arg: T, + third_arg: T, + fourth_arg: T, + fifth_arg: T, + sixth_arg: T, + seventh_arg: T, + | { 1 + 2 + 3 @@ -304,3 +306,10 @@ fn struct_line_breaking() { long_long_long_long_key_e, } = my_val; } +fn closure_line_breaking() { + let closure_with_a_very_very_very_very_very_very_very_very_large_name = | + left: usize, right: usize, + | -> usize { + left + right + }; +}