Skip to content

Commit

Permalink
chore(fmt): add fn_call_width (#3480)
Browse files Browse the repository at this point in the history
  • Loading branch information
kek kek kek authored Nov 15, 2023
1 parent fc84894 commit 0a367b6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions tooling/nargo_fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ config! {
error_on_lost_comment: bool, true, "Error if unable to get comments";
short_array_element_width_threshold: usize, 10, "Width threshold for an array element to be considered short";
array_width: usize, 100, "Maximum width of an array literal before falling back to vertical formatting";
fn_call_width: usize, 60, "Maximum width of the args of a function call before falling back to vertical formatting";
single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else expressions";
}

Expand Down
22 changes: 18 additions & 4 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ impl FmtVisitor<'_> {
self.span_before(call_expr.func.span.end()..span.end(), Token::LeftParen);

let callee = self.format_sub_expr(*call_expr.func);
let args = format_parens(self.fork(), false, call_expr.arguments, args_span);
let args = format_parens(
self.config.fn_call_width.into(),
self.fork(),
false,
call_expr.arguments,
args_span,
);

format!("{callee}{args}")
}
Expand All @@ -74,7 +80,13 @@ impl FmtVisitor<'_> {

let object = self.format_sub_expr(method_call_expr.object);
let method = method_call_expr.method_name.to_string();
let args = format_parens(self.fork(), false, method_call_expr.arguments, args_span);
let args = format_parens(
self.config.fn_call_width.into(),
self.fork(),
false,
method_call_expr.arguments,
args_span,
);

format!("{object}.{method}{args}")
}
Expand All @@ -92,7 +104,7 @@ impl FmtVisitor<'_> {
format!("{collection}{index}")
}
ExpressionKind::Tuple(exprs) => {
format_parens(self.fork(), exprs.len() == 1, exprs, span)
format_parens(None, self.fork(), exprs.len() == 1, exprs, span)
}
ExpressionKind::Literal(literal) => match literal {
Literal::Integer(_) | Literal::Bool(_) | Literal::Str(_) | Literal::FmtStr(_) => {
Expand Down Expand Up @@ -392,12 +404,14 @@ fn format_brackets(
}

fn format_parens(
max_width: Option<usize>,
visitor: FmtVisitor,
trailing_comma: bool,
exprs: Vec<Expression>,
span: Span,
) -> String {
format_expr_seq("(", ")", visitor, trailing_comma, exprs, span, Tactic::Horizontal)
let tactic = max_width.map(Tactic::LimitedHorizontalVertical).unwrap_or(Tactic::Horizontal);
format_expr_seq("(", ")", visitor, trailing_comma, exprs, span, tactic)
}

fn format_exprs(
Expand Down
9 changes: 7 additions & 2 deletions tooling/nargo_fmt/tests/expected/call.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ fn foo() {
Comment */
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);

Expand All @@ -28,4 +30,7 @@ fn foo() {
assert(x, "message");

assert(x == y);

assert(p4_affine.eq(Gaffine::new(6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889)));
}
4 changes: 3 additions & 1 deletion tooling/nargo_fmt/tests/expected/contract.nr
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ 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(), compute_selector("broadcast(Field)"), [owner]);
let _callStackItem1 = context.call_public_function(context.this_address(),
compute_selector("broadcast(Field)"),
[owner]);
}
// Est ultricies integer quis auctor elit sed. In nibh mauris cursus mattis molestie a iaculis.
#[aztec(public)]
Expand Down
3 changes: 2 additions & 1 deletion tooling/nargo_fmt/tests/expected/let.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@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] = [// Padded hash
Expand Down
2 changes: 2 additions & 0 deletions tooling/nargo_fmt/tests/input/call.nr
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ fn foo() {
assert( x, "message" );

assert( x == y );

assert(p4_affine.eq(Gaffine::new(6890855772600357754907169075114257697580319025794532037257385534741338397365, 4338620300185947561074059802482547481416142213883829469920100239455078257889)));
}

0 comments on commit 0a367b6

Please sign in to comment.