From 14940a6a00e6b49a4cc35b390a94d52966f867a4 Mon Sep 17 00:00:00 2001 From: Dean Carmel Date: Tue, 7 Jan 2025 16:52:32 +0200 Subject: [PATCH] added closures to protected zone --- .../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 314e19add81..48d5de066f5 100644 --- a/crates/cairo-lang-formatter/src/node_properties.rs +++ b/crates/cairo-lang-formatter/src/node_properties.rs @@ -276,6 +276,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), @@ -527,12 +535,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..1f579a00922 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_vey_very_very_very_very_very_very_very_larg_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..31f6ab4fc57 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_vey_very_very_very_very_very_very_very_larg_name = | + left: usize, right: usize, + | -> usize { + left + right + }; +}