Skip to content

Commit

Permalink
index-of case
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Dec 20, 2024
1 parent 76cf5e7 commit 83afc00
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
52 changes: 45 additions & 7 deletions components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ pub fn format_source_exprs(
NativeFunctions::Match => {
format_match(settings, list, previous_indentation)
}
NativeFunctions::IndexOf | NativeFunctions::IndexOfAlias => {
format_index_of(settings, list, previous_indentation)
}
NativeFunctions::TupleCons => {
// if the kv map is defined with (tuple (c 1)) then we strip the
// ClarityName("tuple") out first and convert it to key/value syntax
Expand Down Expand Up @@ -298,6 +301,35 @@ fn is_same_line(expr1: &PreSymbolicExpression, expr2: &PreSymbolicExpression) ->
expr1.span().start_line == expr2.span().start_line
}

fn format_index_of(
settings: &Settings,
exprs: &[PreSymbolicExpression],
previous_indentation: &str,
) -> String {
println!("ASDFASDF");
let func_type = display_pse(settings, exprs.first().unwrap(), "");
let mut acc = format!("({func_type}");
acc.push(' ');
acc.push_str(&format!(
"{} {}",
format_source_exprs(
settings,
&[exprs[1].clone()],
previous_indentation,
None,
""
),
format_source_exprs(
settings,
&[exprs[2].clone()],
previous_indentation,
None,
""
)
));
acc.push(')');
acc.to_owned()
}
// *begin* never on one line
fn format_begin(
settings: &Settings,
Expand Down Expand Up @@ -402,7 +434,7 @@ fn format_booleans(
if break_up {
acc.push_str(&format!("\n{}", previous_indentation));
}
acc.push_str(")\n");
acc.push_str(")");
acc.to_owned()
}

Expand Down Expand Up @@ -511,6 +543,7 @@ fn format_key_value_sugar(
let mut acc = "{".to_string();

// TODO this code is horrible
// convert it to the peekable version like the rest
if over_2_kvs {
acc.push('\n');
let mut counter = 1;
Expand Down Expand Up @@ -814,12 +847,12 @@ mod tests_formatter {

#[test]
fn test_booleans() {
let src = "(or true false)\n";
let src = "(or true false)";
let result = format_with_default(&String::from(src));
assert_eq!(src, result);
let src = "(or true (is-eq 1 2) (is-eq 1 1))\n";
let src = "(or true (is-eq 1 2) (is-eq 1 1))";
let result = format_with_default(&String::from(src));
let expected = "(or\n true\n (is-eq 1 2)\n (is-eq 1 1)\n)\n";
let expected = "(or\n true\n (is-eq 1 2)\n (is-eq 1 1)\n)";
assert_eq!(expected, result);
}
#[test]
Expand All @@ -829,8 +862,7 @@ mod tests_formatter {
;; pre comment
(is-eq 1 2) ;; comment
(is-eq 1 1) ;; b
)
"#;
)"#;
let result = format_with_default(&String::from(src));
assert_eq!(src, result);
}
Expand All @@ -841,7 +873,7 @@ mod tests_formatter {
let src = "(try! (unwrap! (complete-deposit-wrapper (get txid deposit) (get vout-index deposit) (get amount deposit) (get recipient deposit) (get burn-hash deposit) (get burn-height deposit) (get sweep-txid deposit)) (err (+ ERR_DEPOSIT_INDEX_PREFIX (+ u10 index)))))";
let result = format_with_default(&String::from(src));
let expected = "(try! (unwrap! (complete-deposit-wrapper\n (get txid deposit)\n (get vout-index deposit)\n (get amount deposit)\n (get recipient deposit)\n (get burn-hash deposit)\n (get burn-height deposit)\n (get sweep-txid deposit)\n ) (err (+ ERR_DEPOSIT_INDEX_PREFIX (+ u10 index)))))";
assert_eq!(result, expected);
assert_eq!(expected, result);
}

#[test]
Expand Down Expand Up @@ -956,6 +988,12 @@ mod tests_formatter {
assert_eq!(src, result);
}

#[test]
fn test_index_of() {
let src = "(index-of? (contract-call? .pool borroweable) asset)";
let result = format_with_default(&String::from(src));
assert_eq!(src, result);
}
#[test]
fn test_traits() {
let src = "(use-trait token-a-trait 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF.token-a.token-trait)\n";
Expand Down
9 changes: 9 additions & 0 deletions test.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;; comment
(slice? "blockstack" u5 u10) ;; Returns (some "stack")
(slice? (list 1 2 3 4 5) u5 u9) ;; Returns none
(slice? (list 1 2 3 4 5) u3 u4) ;; Returns (some (4))
(slice? "abcd" u1 u3) ;; Returns (some "bc")
(slice? "abcd" u2 u2) ;; Returns (some "")
(slice? "abcd" u3 u1) ;; Returns none
;; whatever
;;asdf asdf

0 comments on commit 83afc00

Please sign in to comment.