Skip to content

Commit

Permalink
chore: more progress
Browse files Browse the repository at this point in the history
  • Loading branch information
f01dab1e committed Nov 23, 2023
1 parent 075d146 commit 2f1e10d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion tooling/nargo_fmt/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ mod infix;
mod parenthesized;

pub(crate) use array::rewrite as array;
pub(crate) use expr::{rewrite as expr, rewrite_subexpr as subexpr};
pub(crate) use expr::{rewrite as expr, rewrite_sub_expr as sub_expr};
pub(crate) use infix::rewrite as infix;
pub(crate) use parenthesized::rewrite as parenthesized;
2 changes: 1 addition & 1 deletion tooling/nargo_fmt/src/rewrite/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) fn rewrite(mut visitor: FmtVisitor, array: Vec<Expression>, array_spa
let end = item_span.start();

let leading = visitor.slice(start..end).trim_matches(pattern);
let item = super::subexpr(&visitor, item, visitor.shape());
let item = super::sub_expr(&visitor, visitor.shape(), item);
let next_start = items.peek().map_or(end_position, |expr| expr.span.start());
let trailing = visitor.slice(item_span.end()..next_start);
let offset = trailing
Expand Down
24 changes: 13 additions & 11 deletions tooling/nargo_fmt/src/rewrite/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::visitor::{
ExpressionType, FmtVisitor, Indent, Shape,
};

pub(crate) fn rewrite_subexpr(
pub(crate) fn rewrite_sub_expr(
visitor: &FmtVisitor,
expression: Expression,
shape: Shape,
expression: Expression,
) -> String {
rewrite(visitor, expression, ExpressionType::SubExpression, shape)
}
Expand Down Expand Up @@ -39,10 +39,10 @@ pub(crate) fn rewrite(
}
};

format!("{op}{}", rewrite_subexpr(visitor, prefix.rhs, shape))
format!("{op}{}", rewrite_sub_expr(visitor, shape, prefix.rhs))
}
ExpressionKind::Cast(cast) => {
format!("{} as {}", rewrite_subexpr(visitor, cast.lhs, shape), cast.r#type)
format!("{} as {}", rewrite_sub_expr(visitor, shape, cast.lhs), cast.r#type)
}
kind @ ExpressionKind::Infix(_) => {
super::infix(visitor.fork(), Expression { kind, span }, shape)
Expand All @@ -51,7 +51,7 @@ pub(crate) fn rewrite(
let args_span =
visitor.span_before(call_expr.func.span.end()..span.end(), Token::LeftParen);

let callee = rewrite_subexpr(visitor, *call_expr.func, shape);
let callee = rewrite_sub_expr(visitor, shape, *call_expr.func);
let args = format_parens(
visitor.config.fn_call_width.into(),
visitor.fork(),
Expand All @@ -70,7 +70,7 @@ pub(crate) fn rewrite(
Token::LeftParen,
);

let object = rewrite_subexpr(visitor, method_call_expr.object, shape);
let object = rewrite_sub_expr(visitor, shape, method_call_expr.object);
let method = method_call_expr.method_name.to_string();
let args = format_parens(
visitor.config.fn_call_width.into(),
Expand All @@ -85,14 +85,14 @@ pub(crate) fn rewrite(
format!("{object}.{method}{args}")
}
ExpressionKind::MemberAccess(member_access_expr) => {
let lhs_str = rewrite_subexpr(visitor, member_access_expr.lhs, shape);
let lhs_str = rewrite_sub_expr(visitor, shape, member_access_expr.lhs);
format!("{}.{}", lhs_str, member_access_expr.rhs)
}
ExpressionKind::Index(index_expr) => {
let index_span = visitor
.span_before(index_expr.collection.span.end()..span.end(), Token::LeftBracket);

let collection = rewrite_subexpr(visitor, index_expr.collection, shape);
let collection = rewrite_sub_expr(visitor, shape, index_expr.collection);
let index = format_brackets(visitor.fork(), false, vec![index_expr.index], index_span);

format!("{collection}{index}")
Expand All @@ -105,8 +105,8 @@ pub(crate) fn rewrite(
visitor.slice(span).to_string()
}
Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => {
let repeated = rewrite_subexpr(visitor, *repeated_element, shape);
let length = rewrite_subexpr(visitor, *length, shape);
let repeated = rewrite_sub_expr(visitor, shape, *repeated_element);
let length = rewrite_sub_expr(visitor, shape, *length);

format!("[{repeated}; {length}]")
}
Expand All @@ -115,7 +115,9 @@ pub(crate) fn rewrite(
}
Literal::Unit => "()".to_string(),
},
ExpressionKind::Parenthesized(sub_expr) => super::parenthesized(visitor, span, *sub_expr),
ExpressionKind::Parenthesized(sub_expr) => {
super::parenthesized(visitor, shape, span, *sub_expr)
}
ExpressionKind::Constructor(constructor) => {
let type_name = visitor.slice(span.start()..constructor.type_name.span().end());
let fields_span = visitor
Expand Down
8 changes: 4 additions & 4 deletions tooling/nargo_fmt/src/rewrite/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub(crate) fn rewrite(visitor: FmtVisitor, expr: Expression, shape: Shape) -> St

format!(
"{} {} {}",
rewrite::subexpr(&visitor, infix.lhs, shape),
rewrite::sub_expr(&visitor, shape, infix.lhs),
infix.operator.contents.as_string(),
rewrite::subexpr(&visitor, infix.rhs, shape)
rewrite::sub_expr(&visitor, shape, infix.rhs)
)
}
}
Expand Down Expand Up @@ -88,10 +88,10 @@ pub(crate) fn flatten(
}
_ => {
let rewrite = if result.is_empty() {
rewrite::subexpr(&visitor, node.clone(), visitor.shape())
rewrite::sub_expr(&visitor, visitor.shape(), node.clone())
} else {
visitor.indent.block_indent(visitor.config);
rewrite::subexpr(&visitor, node.clone(), visitor.shape())
rewrite::sub_expr(&visitor, visitor.shape(), node.clone())
};

result.push(rewrite);
Expand Down
7 changes: 4 additions & 3 deletions tooling/nargo_fmt/src/rewrite/parenthesized.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use noirc_frontend::{hir::resolution::errors::Span, Expression, ExpressionKind};

use crate::visitor::FmtVisitor;
use crate::visitor::{FmtVisitor, Shape};

pub(crate) fn rewrite(
visitor: &FmtVisitor<'_>,
shape: Shape,
mut span: Span,
mut sub_expr: Expression,
) -> String {
Expand Down Expand Up @@ -31,7 +32,7 @@ pub(crate) fn rewrite(
}

if !leading.contains("//") && !trailing.contains("//") {
let sub_expr = super::subexpr(visitor, sub_expr, visitor.shape());
let sub_expr = super::sub_expr(visitor, shape, sub_expr);
format!("({leading}{sub_expr}{trailing})")
} else {
let mut visitor = visitor.fork();
Expand All @@ -40,7 +41,7 @@ pub(crate) fn rewrite(
visitor.indent.block_indent(visitor.config);
let nested_indent = visitor.indent.to_string_with_newline();

let sub_expr = super::subexpr(&visitor, sub_expr, visitor.shape());
let sub_expr = super::sub_expr(&visitor, shape, sub_expr);

let mut result = String::new();
result.push('(');
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_fmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl Item for Expression {
}

fn format(self, visitor: &FmtVisitor, shape: Shape) -> String {
rewrite::subexpr(visitor, self, shape)
rewrite::sub_expr(visitor, shape, self)
}
}

Expand All @@ -235,7 +235,7 @@ impl Item for (Ident, Expression) {
let (name, expr) = self;

let name = name.0.contents;
let expr = rewrite::subexpr(visitor, expr, shape);
let expr = rewrite::sub_expr(visitor, shape, expr);

if name == expr {
name
Expand Down
8 changes: 4 additions & 4 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ impl FmtVisitor<'_> {
}

pub(crate) fn format_if(&self, if_expr: IfExpression) -> String {
let condition_str = rewrite::subexpr(self, if_expr.condition, self.shape());
let consequence_str = rewrite::subexpr(self, if_expr.consequence, self.shape());
let condition_str = rewrite::sub_expr(self, self.shape(), if_expr.condition);
let consequence_str = rewrite::sub_expr(self, self.shape(), if_expr.consequence);

let mut result = format!("if {condition_str} {consequence_str}");

Expand All @@ -41,9 +41,9 @@ impl FmtVisitor<'_> {
}

pub(crate) fn format_if_single_line(&self, if_expr: IfExpression) -> Option<String> {
let condition_str = rewrite::subexpr(self, if_expr.condition, self.shape());
let condition_str = rewrite::sub_expr(self, self.shape(), if_expr.condition);
let consequence_str =
rewrite::subexpr(self, extract_simple_expr(if_expr.consequence)?, self.shape());
rewrite::sub_expr(self, self.shape(), extract_simple_expr(if_expr.consequence)?);

let if_str = if let Some(alternative) = if_expr.alternative {
let alternative_str = if let Some(ExpressionKind::If(_)) =
Expand Down
18 changes: 9 additions & 9 deletions tooling/nargo_fmt/src/visitor/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl super::FmtVisitor<'_> {
let let_str =
self.slice(span.start()..let_stmt.expression.span.start()).trim_end();

let expr_str = rewrite::subexpr(self, let_stmt.expression, self.shape());
let expr_str = rewrite::sub_expr(self, self.shape(), let_stmt.expression);

self.push_rewrite(format!("{let_str} {expr_str};"), span);
}
Expand All @@ -37,22 +37,22 @@ impl super::FmtVisitor<'_> {
message.map_or(String::new(), |message| format!(", \"{message}\""));
let constrain = match kind {
ConstrainKind::Assert => {
let assertion = rewrite::subexpr(self, expr, self.shape());
let assertion = rewrite::sub_expr(self, self.shape(), expr);

format!("assert({assertion}{message});")
}
ConstrainKind::AssertEq => {
if let ExpressionKind::Infix(infix) = expr.kind {
let lhs = rewrite::subexpr(self, infix.lhs, self.shape());
let rhs = rewrite::subexpr(self, infix.rhs, self.shape());
let lhs = rewrite::sub_expr(self, self.shape(), infix.lhs);
let rhs = rewrite::sub_expr(self, self.shape(), infix.rhs);

format!("assert_eq({lhs}, {rhs}{message});")
} else {
unreachable!()
}
}
ConstrainKind::Constrain => {
let expr = rewrite::subexpr(self, expr, self.shape());
let expr = rewrite::sub_expr(self, self.shape(), expr);
format!("constrain {expr};")
}
};
Expand All @@ -64,12 +64,12 @@ impl super::FmtVisitor<'_> {
let range = match for_stmt.range {
ForRange::Range(start, end) => format!(
"{}..{}",
rewrite::subexpr(self, start, self.shape()),
rewrite::subexpr(self, end, self.shape())
rewrite::sub_expr(self, self.shape(), start),
rewrite::sub_expr(self, self.shape(), end)
),
ForRange::Array(array) => rewrite::subexpr(self, array, self.shape()),
ForRange::Array(array) => rewrite::sub_expr(self, self.shape(), array),
};
let block = rewrite::subexpr(self, for_stmt.block, self.shape());
let block = rewrite::sub_expr(self, self.shape(), for_stmt.block);

let result = format!("for {identifier} in {range} {block}");
self.push_rewrite(result, span);
Expand Down

0 comments on commit 2f1e10d

Please sign in to comment.