Skip to content

Commit

Permalink
fix format_map
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Dec 3, 2024
1 parent b8e4451 commit 5057d1b
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,21 @@ fn format_map(
// this is hacked in to handle situations where the contents of
// map is a 'tuple'
PreSymbolicExpressionType::Tuple(list) => acc.push_str(&format!(
"\n{}{}",
"\n{}{}{}",
previous_indentation,
indentation,
format_key_value_sugar(settings, &list.to_vec(), previous_indentation)
)),
_ => acc.push_str(&format!(
"\n{}{}",
"\n{}{}{}",
previous_indentation,
indentation,
format_source_exprs(settings, &[arg.clone()], previous_indentation, "")
)),
}
}

acc.push_str("\n)");
acc.push_str(&format!("\n{})\n", previous_indentation));
acc.to_owned()
} else {
panic!("define-map without a name is silly")
Expand Down Expand Up @@ -313,24 +315,29 @@ fn format_key_value_sugar(
let fkey = display_pse(settings, key, "");
if i + 1 < exprs.len() / 2 {
acc.push_str(&format!(
"\n{}{fkey}: {},",
"\n{}{}{}{fkey}: {},",
previous_indentation,
indentation,
indentation,
format_source_exprs(settings, &[value.clone()], previous_indentation, "")
));
} else {
acc.push_str(&format!(
"\n{}{fkey}: {}\n",
"\n{}{}{}{fkey}: {}\n",
previous_indentation,
indentation,
indentation,
format_source_exprs(settings, &[value.clone()], previous_indentation, "")
));
}
} else {
panic!("Unpaired key values: {:?}", chunk);
}
acc.push_str(&format!("{}{}", previous_indentation, &indentation));
}
} else {
// for cases where we keep it on the same line with 1 k/v pair
let fkey = display_pse(settings, &exprs[0], "");
let fkey = display_pse(settings, &exprs[0], previous_indentation);
acc.push_str(&format!(
" {fkey}: {} ",
format_source_exprs(settings, &[exprs[1].clone()], previous_indentation, "")
Expand Down Expand Up @@ -520,11 +527,6 @@ mod tests_formatter {
let result = format_with_default(&String::from("( ok true )"));
assert_eq!(result, "(ok true)");
}
#[test]
fn test_two_expr_formatter() {
let result = format_with_default(&String::from("(ok true)(ok true)"));
assert_eq!(result, "(ok true)\n(ok true)");
}

#[test]
fn test_manual_tuple() {
Expand Down Expand Up @@ -608,18 +610,8 @@ mod tests_formatter {
let result = format_with_default(&String::from(src));
assert_eq!(
result,
"(define-map something\n {\n name: (buff 48),\n a: uint\n }\n uint)"
"(define-map something\n {\n name: (buff 48),\n a: uint\n }\n uint\n)"
);
// let src2 = "(define-map something { name: (buff 48), namespace: (buff 20) } uint\n)";
// let result2 = format_with_default(&String::from(src2));
// let expected2 = r#"(define-map something
// {
// name: (buff 48),
// namespace: (buff 20)
// }
// uint
// )"#;
// assert_eq!(result2, expected2);
}
#[test]
fn test_constant() {
Expand Down

0 comments on commit 5057d1b

Please sign in to comment.