Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kek kek kek committed Oct 23, 2023
1 parent e3d9739 commit b282634
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 16 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 @@ -47,6 +47,7 @@ config! {
remove_nested_parens: bool, true, "Remove nested parens";
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";
single_line_if_else_max_width: usize, 100, "Maximum line length for single line if-else expressions";
}

impl Config {
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_fmt/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ struct Shape {
indent: Indent,
}

#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Debug)]
pub(crate) enum ExpressionType {
Statement,
SubExpression,
Expand Down
52 changes: 39 additions & 13 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,32 +186,58 @@ impl FmtVisitor<'_> {
if allow_single_line {
let mut visitor = self.fork();
visitor.indent = Indent::default();
if let Some(line) = visitor.format_if_single_line(*if_expr) {
if let Some(line) = visitor.format_if_single_line(*if_expr.clone()) {
return line;
}
}

todo!()
self.format_if(*if_expr)
}
ExpressionKind::Variable(_) => self.slice(span).to_string(),
ExpressionKind::Lambda(_) => todo!(),
ExpressionKind::Error => todo!(),
_ => self.slice(span).to_string(),
}
}

fn format_if(&self, if_expr: IfExpression) -> String {
let condition_str = self.format_subexpr(if_expr.condition);
let consequence_str = self.format_subexpr(if_expr.consequence);

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

if let Some(alternative) = if_expr.alternative {
let alternative = if let Some(ExpressionKind::If(if_expr)) =
extract_simple_expr(alternative.clone()).map(|expr| expr.kind)
{
self.format_if(*if_expr)
} else {
self.format_expr(alternative, ExpressionType::Statement)
};

result.push_str(" else ");
result.push_str(&alternative);
};

result
}

fn format_if_single_line(&self, if_expr: IfExpression) -> Option<String> {
let condition_str = self.format_subexpr(if_expr.condition);
let consequence_str =
self.format_subexpr(extract_simple_expr(if_expr.consequence).unwrap());
let consequence_str = self.format_subexpr(extract_simple_expr(if_expr.consequence)?);

let if_str = if let Some(alternative) = if_expr.alternative {
let alternative_str = if let Some(ExpressionKind::If(_)) =
extract_simple_expr(alternative.clone()).map(|expr| expr.kind)
{
return None;
} else {
self.format_expr(extract_simple_expr(alternative)?, ExpressionType::Statement)
};

if let Some(alternative_expr) = if_expr.alternative {
let alternative_str =
self.format_subexpr(extract_simple_expr(alternative_expr).unwrap());
format!("if {} {{ {} }} else {{ {} }}", condition_str, consequence_str, alternative_str)
.into()
} else {
format!("if {{{}}} {{{}}}", condition_str, consequence_str).into()
}
format!("if {{{}}} {{{}}}", condition_str, consequence_str)
};

(if_str.len() <= self.config.single_line_if_else_max_width).then_some(if_str)
}

fn format_struct_lit(
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_fmt/src/visitor/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ impl super::FmtVisitor<'_> {
pub(crate) fn visit_stmts(&mut self, stmts: Vec<Statement>) {
let len = stmts.len();

for (Statement { kind, span }, index) in zip(stmts, 0..) {
for (Statement { kind, span }, index) in zip(stmts, 1..) {
let is_last = index == len;

match kind {
Expand Down
26 changes: 26 additions & 0 deletions tooling/nargo_fmt/tests/expected/expr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,29 @@ fn parenthesized() {
fn constructor() {
Point { x: 5, y: 10 };
}

fn if_expr() {
if true {
println("Hello :D");
}
}

fn return_if_expr() {
if true { 42 } else { 40 + 2 }
}

fn return_if_expr() {
if true {
42
};

if true { 42 } else { 40 + 2 }
}

fn if_if() {
if cond {
some();
} else {
none();
}.bar().baz();
}
40 changes: 40 additions & 0 deletions tooling/nargo_fmt/tests/expected/if.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
fn main() {
if false {
();
();
}

if false // lone if comment
{
();
();
}

let a = if 0 > 1 { 0 } else { 0 };

if true {
();
} else if false {
();
();
} else {
();
();
();
}

if true // else-if-chain if comment
{
();
}
else if false // else-if-chain else-if comment
{
();
();
} else // else-if-chain else comment
{
();
();
();
}
}
8 changes: 7 additions & 1 deletion tooling/nargo_fmt/tests/expected/nested_if_else.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
fn nested_if_else() {
if false { 1 } else if false { 2 } else { 3 }
if false {
1
} else if false {
2
} else {
3
}
}
29 changes: 29 additions & 0 deletions tooling/nargo_fmt/tests/input/expr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,33 @@ fn parenthesized() {
fn constructor() {
Point{x :5,
y: 10 };
}

fn if_expr() {
if true { println("Hello :D"); }
}

fn return_if_expr() {
if true {
42
}
else
{ 40 + 2 }
}

fn return_if_expr() {
if true {42};

if true {
42
}
else {
40 +
2 }
}

fn if_if() {
if cond { some(); } else { none(); }
.bar()
.baz();
}
52 changes: 52 additions & 0 deletions tooling/nargo_fmt/tests/input/if.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
fn main() {
if false
{
();
();
}

if false // lone if comment
{
();
();
}


let a =
if 0 > 1 {
0
}
else
{
0
};


if true
{
();
} else if false {
();
();
}
else {
();
();
();
}

if true // else-if-chain if comment
{
();
}
else if false // else-if-chain else-if comment
{
();
();
} else // else-if-chain else comment
{
();
();
();
}
}

0 comments on commit b282634

Please sign in to comment.