From 5057d1b99ddb38746989918329dd6918fc9e4eea Mon Sep 17 00:00:00 2001 From: "brady.ouren" Date: Mon, 2 Dec 2024 15:55:24 -0800 Subject: [PATCH] fix format_map --- .../clarinet-format/src/formatter/mod.rs | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/components/clarinet-format/src/formatter/mod.rs b/components/clarinet-format/src/formatter/mod.rs index e2be30f97..d0a6018a3 100644 --- a/components/clarinet-format/src/formatter/mod.rs +++ b/components/clarinet-format/src/formatter/mod.rs @@ -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") @@ -313,13 +315,17 @@ 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, "") )); @@ -327,10 +333,11 @@ fn format_key_value_sugar( } 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, "") @@ -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() { @@ -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() {