Skip to content

Commit

Permalink
fix traits spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Dec 18, 2024
1 parent 5736d15 commit dcc2f2c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
56 changes: 38 additions & 18 deletions components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,19 @@ pub fn format_source_exprs(
| DefineFunctions::PrivateFunction => format_function(settings, list),
DefineFunctions::Constant => format_constant(settings, list),
DefineFunctions::Map => format_map(settings, list, previous_indentation),
// DefineFunctions::UseTrait => format_use_trait(settings, list),
DefineFunctions::UseTrait | DefineFunctions::ImplTrait => {
follow_with_newline(&format!(
"({})",
format_source_exprs(
settings,
list,
previous_indentation,
previous_expr,
acc
)
))
}
// DefineFunctions::Trait => format_trait(settings, list),
// DefineFunctions::ImplTrait => format_impl_trait(settings, list),
// DefineFunctions::PersistedVariable
// DefineFunctions::FungibleToken
// DefineFunctions::NonFungibleToken
Expand Down Expand Up @@ -214,6 +224,10 @@ pub fn format_source_exprs(
acc.to_owned()
}

fn follow_with_newline(expr: &str) -> String {
format!("{}\n", expr)
}

// trim but leaves newlines preserved
fn t(input: &str) -> &str {
let start = input
Expand Down Expand Up @@ -628,32 +642,28 @@ fn display_pse(
has_previous_expr: bool,
) -> String {
match pse.pre_expr {
PreSymbolicExpressionType::Atom(ref value) => {
// println!("atom: {}", value.as_str());
t(value.as_str()).to_string()
}
PreSymbolicExpressionType::AtomValue(ref value) => {
// println!("atomvalue: {}", value);
value.to_string()
}
PreSymbolicExpressionType::Atom(ref value) => t(value.as_str()).to_string(),
PreSymbolicExpressionType::AtomValue(ref value) => value.to_string(),
PreSymbolicExpressionType::List(ref items) => {
format_list(settings, items, previous_indentation)
// items.iter().map(display_pse).collect::<Vec<_>>().join(" ")
}
PreSymbolicExpressionType::Tuple(ref items) => {
// println!("tuple: {:?}", items);
format_key_value_sugar(settings, items, previous_indentation)
// items.iter().map(display_pse).collect::<Vec<_>>().join(", ")
}
PreSymbolicExpressionType::SugaredContractIdentifier(ref name) => name.to_string(),
PreSymbolicExpressionType::SugaredContractIdentifier(ref name) => {
format!(".{}", name)
}
PreSymbolicExpressionType::SugaredFieldIdentifier(ref contract, ref field) => {
format!("{}.{}", contract, field)
println!("sugar field id");
format!(".{}.{}", contract, field)
}
PreSymbolicExpressionType::FieldIdentifier(ref trait_id) => {
// println!("field id: {}", trait_id);
trait_id.to_string()
format!("'{}", trait_id)
}
PreSymbolicExpressionType::TraitReference(ref name) => {
println!("trait ref: {}", name);
name.to_string()
}
PreSymbolicExpressionType::TraitReference(ref name) => name.to_string(),
PreSymbolicExpressionType::Comment(ref text) => {
// println!("{:?}", has_previous_expr);
if has_previous_expr {
Expand Down Expand Up @@ -952,6 +962,16 @@ mod tests_formatter {
assert_eq!(src, result);
}

#[test]
fn test_traits() {
let src = "(use-trait token-a-trait 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF.token-a.token-trait)\n";
let result = format_with(&String::from(src), Settings::new(Indentation::Space(4), 80));
assert_eq!(src, result);
let src = "(as-contract (contract-call? .tokens mint! u19)) ;; Returns (ok u19)\n";
let result = format_with(&String::from(src), Settings::new(Indentation::Space(4), 80));
assert_eq!(src, result);
}

#[test]
#[ignore]
fn test_irl_contracts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(define-read-only (is-borroweable-isolated
(asset principal)
)
(match (index-of? (contract-call? pool-reserve-data get-borroweable-isolated-read) asset)
(match (index-of? (contract-call? .pool-reserve-data get-borroweable-isolated-read) asset)
res
true
false
Expand Down
7 changes: 7 additions & 0 deletions components/clarinet-format/tests/golden-intended/traits.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(use-trait token-a-trait 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF.token-a.token-trait)

(define-public (forward-get-balance (user principal) (contract <token-a-trait>))
(begin
(ok (contract-of contract)) ;; returns the principal of the contract implementing <token-a-trait>
)
)
4 changes: 4 additions & 0 deletions components/clarinet-format/tests/golden/traits.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(use-trait token-a-trait 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF.token-a.token-trait)
(define-public (forward-get-balance (user principal) (contract <token-a-trait>))
(begin
(ok (contract-of contract)))) ;; returns the principal of the contract implementing <token-a-trait>

0 comments on commit dcc2f2c

Please sign in to comment.