Skip to content

Commit

Permalink
fix key value sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Dec 19, 2024
1 parent 4d6d022 commit 972c49c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 19 deletions.
58 changes: 39 additions & 19 deletions components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn format_source_exprs(
acc: &str,
) -> String {
// print_pre_expr(expressions);
// println!("exprs: {:?}", expressions);
println!("exprs: {:?}", expressions);
// println!("previous: {:?}", previous_expr);
if let Some((expr, remaining)) = expressions.split_first() {
let cur = display_pse(
Expand Down Expand Up @@ -330,9 +330,6 @@ fn format_begin(
fn is_comment(pse: &PreSymbolicExpression) -> bool {
matches!(pse.pre_expr, PreSymbolicExpressionType::Comment(_))
}
fn is_list(pse: &PreSymbolicExpression) -> bool {
matches!(pse.pre_expr, PreSymbolicExpressionType::List(_))
}
pub fn without_comments_len(exprs: &[PreSymbolicExpression]) -> usize {
exprs.iter().filter(|expr| !is_comment(expr)).count()
}
Expand Down Expand Up @@ -509,41 +506,52 @@ fn format_key_value_sugar(
) -> String {
let indentation = &settings.indentation.to_string();
let space = format!("{}{}", previous_indentation, indentation);
let over_2_kvs = without_comments_len(exprs) > 2;
let mut acc = "{".to_string();
if over_2_kvs {
acc.push('\n');
}

// TODO this logic depends on comments not screwing up the even numbered
// chunkable attrs
if exprs.len() > 2 {
for (i, chunk) in exprs.chunks(2).enumerate() {
if let [key, value] = chunk {
let fkey = display_pse(settings, key, "", None);
if i + 1 < exprs.len() / 2 {
// TODO this code is horrible
if over_2_kvs {
let mut counter = 1;
for (i, expr) in exprs.iter().enumerate() {
if is_comment(expr) {
acc.push_str(&format!(
"{}{}\n",
space,
format_source_exprs(settings, &[expr.clone()], previous_indentation, None, "")
))
} else {
let last = i == exprs.len() - 1;
// if counter is even we're on the value
if counter % 2 == 0 {
acc.push_str(&format!(
"\n{}{fkey}: {},\n",
space,
": {}{}\n",
format_source_exprs(
settings,
&[value.clone()],
&[expr.clone()],
previous_indentation,
None,
""
)
),
if last { "" } else { "," }
));
} else {
// if counter is odd we're on the key
acc.push_str(&format!(
"{}{fkey}: {}\n",
"{}{}",
space,
format_source_exprs(
settings,
&[value.clone()],
&[expr.clone()],
previous_indentation,
None,
""
)
));
}
} else {
panic!("Unpaired key values: {:?}", chunk);
counter += 1
}
}
} else {
Expand Down Expand Up @@ -775,6 +783,7 @@ mod tests_formatter {
assert_eq!(result, "(ok true)");
}

#[test]
#[test]
fn test_manual_tuple() {
let result = format_with_default(&String::from("(tuple (n1 1))"));
Expand Down Expand Up @@ -923,6 +932,17 @@ mod tests_formatter {
assert_eq!(result, "{\n name: (buff 48),\n a: uint\n}");
}

#[test]
fn test_key_value_sugar_comment_midrecord() {
let src = r#"{
name: (buff 48),
;; comment
owner: send-to
}"#;
let result = format_with_default(&String::from(src));
assert_eq!(src, result);
}

#[test]
fn test_basic_slice() {
let src = "(slice? (1 2 3 4 5) u5 u9)";
Expand Down
26 changes: 26 additions & 0 deletions components/clarinet-format/tests/golden-intended/nested_map.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(define-public (mng-name-register)
(map-set name-properties
{
name: name, namespace: namespace
}
{
registered-at: (some burn-block-height),
imported-at: none,
hashed-salted-fqn-preorder: (some hashed-salted-fqn),
preordered-by: (some send-to),
;; Updated this to be u0, so that renewals are handled through the namespace manager
renewal-height: u0,
stx-burn: u0,
owner: send-to,
}
)
(print
{
topic: "new-name",
owner: send-to,
name: {name: name, namespace: namespace},
id: id-to-be-minted,
properties: (map-get? name-properties {name: name, namespace: namespace})
}
)
)
26 changes: 26 additions & 0 deletions components/clarinet-format/tests/golden/nested_map.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(define-public (mng-name-register)
(map-set name-properties
{
name: name, namespace: namespace
}
{
registered-at: (some burn-block-height),
imported-at: none,
hashed-salted-fqn-preorder: (some hashed-salted-fqn),
preordered-by: (some send-to),
;; Updated this to be u0, so that renewals are handled through the namespace manager
renewal-height: u0,
stx-burn: u0,
owner: send-to,
}
)
(print
{
topic: "new-name",
owner: send-to,
name: {name: name, namespace: namespace},
id: id-to-be-minted,
properties: (map-get? name-properties {name: name, namespace: namespace})
}
)
)

0 comments on commit 972c49c

Please sign in to comment.