From eec92e4f5345aa48f45b14ccb96e227e84ed92b1 Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Wed, 22 Nov 2023 03:34:02 -0800 Subject: [PATCH] chore: move the first element to a new line if the tactic is vertical for call expression (#3526) Co-authored-by: kevaundray --- tooling/nargo_fmt/src/visitor/expr.rs | 7 +++-- tooling/nargo_fmt/tests/expected/call.nr | 32 ++++++++++++++------ tooling/nargo_fmt/tests/expected/contract.nr | 6 ++-- tooling/nargo_fmt/tests/expected/let.nr | 6 ++-- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index 8492fd5c05d..0eb192569c3 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -68,6 +68,7 @@ impl FmtVisitor<'_> { false, call_expr.arguments, args_span, + true, ); format!("{callee}{args}") @@ -86,6 +87,7 @@ impl FmtVisitor<'_> { false, method_call_expr.arguments, args_span, + true, ); format!("{object}.{method}{args}") @@ -104,7 +106,7 @@ impl FmtVisitor<'_> { format!("{collection}{index}") } ExpressionKind::Tuple(exprs) => { - format_parens(None, self.fork(), exprs.len() == 1, exprs, span) + format_parens(None, self.fork(), exprs.len() == 1, exprs, span, false) } ExpressionKind::Literal(literal) => match literal { Literal::Integer(_) | Literal::Bool(_) | Literal::Str(_) | Literal::FmtStr(_) => { @@ -412,9 +414,10 @@ fn format_parens( trailing_comma: bool, exprs: Vec, span: Span, + soft_newline: bool, ) -> String { let tactic = max_width.map(Tactic::LimitedHorizontalVertical).unwrap_or(Tactic::Horizontal); - format_seq("(", ")", visitor, trailing_comma, exprs, span, tactic, false) + format_seq("(", ")", visitor, trailing_comma, exprs, span, tactic, soft_newline) } fn format_exprs( diff --git a/tooling/nargo_fmt/tests/expected/call.nr b/tooling/nargo_fmt/tests/expected/call.nr index 3c9b36e8403..7824ba37089 100644 --- a/tooling/nargo_fmt/tests/expected/call.nr +++ b/tooling/nargo_fmt/tests/expected/call.nr @@ -1,25 +1,33 @@ fn foo() { my_function(10, some_value, another_func(20, 30)); - outer_function(some_function(), // Original inner function call + outer_function( + some_function(), // Original inner function call another_function() // Original inner function call ); - outer_function(some_function(), // Original inner function call + outer_function( + some_function(), // Original inner function call another_function() // Original inner function call ); - my_function(// Comment + my_function( + // Comment some_value, /* Multiline Comment */ - another_func(20, 30)); + another_func(20, 30) + ); - my_function(some_function(10, "arg1", another_function()), - another_func(20, some_function(), 30)); + my_function( + some_function(10, "arg1", another_function()), + another_func(20, some_function(), 30) + ); - outer_function(some_function(), - another_function(some_function(), some_value)); + outer_function( + some_function(), + another_function(some_function(), some_value) + ); assert_eq(x, y); @@ -31,6 +39,10 @@ fn foo() { assert(x == y); - assert(p4_affine.eq(Gaffine::new(6890855772600357754907169075114257697580319025794532037257385534741338397365, - 4338620300185947561074059802482547481416142213883829469920100239455078257889))); + assert(p4_affine.eq( + Gaffine::new( + 6890855772600357754907169075114257697580319025794532037257385534741338397365, + 4338620300185947561074059802482547481416142213883829469920100239455078257889 + ) + )); } diff --git a/tooling/nargo_fmt/tests/expected/contract.nr b/tooling/nargo_fmt/tests/expected/contract.nr index 0d9bd2f100d..8a77d97bbba 100644 --- a/tooling/nargo_fmt/tests/expected/contract.nr +++ b/tooling/nargo_fmt/tests/expected/contract.nr @@ -57,9 +57,11 @@ contract Benchmarking { fn increment_balance(owner: Field, value: Field) { let current = storage.balances.at(owner).read(); storage.balances.at(owner).write(current + value); - let _callStackItem1 = context.call_public_function(context.this_address(), + let _callStackItem1 = context.call_public_function( + context.this_address(), compute_selector("broadcast(Field)"), - [owner]); + [owner] + ); } // Est ultricies integer quis auctor elit sed. In nibh mauris cursus mattis molestie a iaculis. diff --git a/tooling/nargo_fmt/tests/expected/let.nr b/tooling/nargo_fmt/tests/expected/let.nr index b60ae644790..017a724b099 100644 --- a/tooling/nargo_fmt/tests/expected/let.nr +++ b/tooling/nargo_fmt/tests/expected/let.nr @@ -1,7 +1,9 @@ //@error_on_lost_comment=false fn let_() { - let fn_call = my_function(some_function(10, "arg1", another_function()), - another_func(20, some_function(), 30)); + let fn_call = my_function( + some_function(10, "arg1", another_function()), + another_func(20, some_function(), 30) + ); let array = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]; let padded_sha256_hash: [u8; 259] = [