From dcc2f2c0b928be60265f6e3cf0f60a4ae3c93736 Mon Sep 17 00:00:00 2001 From: "brady.ouren" Date: Wed, 18 Dec 2024 12:38:53 -0800 Subject: [PATCH] fix traits spacing --- .../clarinet-format/src/formatter/mod.rs | 56 +++++++++++++------ .../tests/golden-intended/match-or.clar | 2 +- .../tests/golden-intended/traits.clar | 7 +++ .../clarinet-format/tests/golden/traits.clar | 4 ++ 4 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 components/clarinet-format/tests/golden-intended/traits.clar create mode 100644 components/clarinet-format/tests/golden/traits.clar diff --git a/components/clarinet-format/src/formatter/mod.rs b/components/clarinet-format/src/formatter/mod.rs index 7e20aacca..1b190a878 100644 --- a/components/clarinet-format/src/formatter/mod.rs +++ b/components/clarinet-format/src/formatter/mod.rs @@ -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 @@ -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 @@ -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::>().join(" ") } PreSymbolicExpressionType::Tuple(ref items) => { - // println!("tuple: {:?}", items); format_key_value_sugar(settings, items, previous_indentation) - // items.iter().map(display_pse).collect::>().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 { @@ -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() { diff --git a/components/clarinet-format/tests/golden-intended/match-or.clar b/components/clarinet-format/tests/golden-intended/match-or.clar index 9448ff848..5a8607592 100644 --- a/components/clarinet-format/tests/golden-intended/match-or.clar +++ b/components/clarinet-format/tests/golden-intended/match-or.clar @@ -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 diff --git a/components/clarinet-format/tests/golden-intended/traits.clar b/components/clarinet-format/tests/golden-intended/traits.clar new file mode 100644 index 000000000..cace389b2 --- /dev/null +++ b/components/clarinet-format/tests/golden-intended/traits.clar @@ -0,0 +1,7 @@ +(use-trait token-a-trait 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF.token-a.token-trait) + +(define-public (forward-get-balance (user principal) (contract )) + (begin + (ok (contract-of contract)) ;; returns the principal of the contract implementing + ) +) diff --git a/components/clarinet-format/tests/golden/traits.clar b/components/clarinet-format/tests/golden/traits.clar new file mode 100644 index 000000000..7d2387518 --- /dev/null +++ b/components/clarinet-format/tests/golden/traits.clar @@ -0,0 +1,4 @@ +(use-trait token-a-trait 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF.token-a.token-trait) +(define-public (forward-get-balance (user principal) (contract )) + (begin + (ok (contract-of contract)))) ;; returns the principal of the contract implementing