@@ -6,6 +6,8 @@ use std::collections::HashMap;
66use std:: iter:: Peekable ;
77use std:: { fmt, slice} ;
88
9+ use clarity:: types:: StacksEpochId ;
10+ use clarity:: vm:: ast:: stack_depth_checker:: StackDepthLimits ;
911use clarity:: vm:: functions:: define:: DefineFunctions ;
1012use clarity:: vm:: functions:: NativeFunctions ;
1113use clarity:: vm:: representations:: { PreSymbolicExpression , PreSymbolicExpressionType } ;
@@ -68,9 +70,13 @@ impl ClarityFormatter {
6870 Self { settings }
6971 }
7072 /// formatting for files to ensure a newline at the end
71- pub fn format_file ( & self , source : & str ) -> String {
73+ pub fn format_file ( & self , source : & str , epoch : Option < StacksEpochId > ) -> String {
7274 let trimmed_source = source. trim_start_matches ( [ '\n' , '\r' ] ) ;
73- let pse = clarity:: vm:: ast:: parser:: v2:: parse ( trimmed_source) . unwrap ( ) ;
75+ let pse = clarity:: vm:: ast:: parser:: v2:: parse (
76+ trimmed_source,
77+ StackDepthLimits :: for_epoch ( epoch. unwrap_or ( StacksEpochId :: latest ( ) ) ) ,
78+ )
79+ . unwrap ( ) ;
7480 let agg = Aggregator :: new ( & self . settings , & pse, Some ( trimmed_source) ) ;
7581 let result = agg. generate ( ) ;
7682
@@ -83,12 +89,20 @@ impl ClarityFormatter {
8389 agg. generate ( )
8490 }
8591 /// Alias `format_file` to `format`
86- pub fn format ( & self , source : & str ) -> String {
87- self . format_file ( source)
92+ pub fn format ( & self , source : & str , epoch : Option < StacksEpochId > ) -> String {
93+ self . format_file ( source, epoch )
8894 }
8995 /// for range formatting within editors
90- pub fn format_section ( & self , source : & str ) -> Result < String , String > {
91- let pse = clarity:: vm:: ast:: parser:: v2:: parse ( source) . map_err ( |e| e. to_string ( ) ) ?;
96+ pub fn format_section (
97+ & self ,
98+ source : & str ,
99+ epoch : Option < StacksEpochId > ,
100+ ) -> Result < String , String > {
101+ let pse = clarity:: vm:: ast:: parser:: v2:: parse (
102+ source,
103+ StackDepthLimits :: for_epoch ( epoch. unwrap_or ( StacksEpochId :: latest ( ) ) ) ,
104+ )
105+ . map_err ( |e| e. to_string ( ) ) ?;
92106
93107 // range formatting specifies to the aggregator that we're
94108 // starting mid-source and thus should pre-populate
@@ -1889,6 +1903,8 @@ mod tests_formatter {
18891903 #[ allow( unused_imports) ]
18901904 use std:: assert_eq;
18911905
1906+ use clarity:: types:: StacksEpochId ;
1907+ use clarity:: vm:: ast:: stack_depth_checker:: StackDepthLimits ;
18921908 use indoc:: indoc;
18931909
18941910 use super :: { ClarityFormatter , Settings } ;
@@ -1902,12 +1918,12 @@ mod tests_formatter {
19021918
19031919 fn format_with_default ( source : & str ) -> String {
19041920 let formatter = ClarityFormatter :: new ( Settings :: default ( ) ) ;
1905- formatter. format_section ( source) . unwrap ( )
1921+ formatter. format_section ( source, None ) . unwrap ( )
19061922 }
19071923
19081924 fn format_with ( source : & str , settings : Settings ) -> String {
19091925 let formatter = ClarityFormatter :: new ( settings) ;
1910- formatter. format_section ( source) . unwrap ( )
1926+ formatter. format_section ( source, None ) . unwrap ( )
19111927 }
19121928
19131929 #[ test]
@@ -2743,7 +2759,11 @@ mod tests_formatter {
27432759 #[ test]
27442760 fn format_ast_without_source ( ) {
27452761 let src = "(define-private (noop) (begin (+ 1 2) (ok true)))" ;
2746- let ast = clarity:: vm:: ast:: parser:: v2:: parse ( src) . unwrap ( ) ;
2762+ let ast = clarity:: vm:: ast:: parser:: v2:: parse (
2763+ src,
2764+ StackDepthLimits :: for_epoch ( StacksEpochId :: latest ( ) ) ,
2765+ )
2766+ . unwrap ( ) ;
27472767 let formatter = ClarityFormatter :: new ( Settings :: default ( ) ) ;
27482768 let expected = format_with_default ( src) ;
27492769 let result = formatter. format_ast ( & ast) ;
@@ -2753,7 +2773,11 @@ mod tests_formatter {
27532773 #[ test]
27542774 fn format_ast_without_source_handle_indentation ( ) {
27552775 let src = " (begin (+ 1 2) (ok true))" ;
2756- let ast = clarity:: vm:: ast:: parser:: v2:: parse ( src) . unwrap ( ) ;
2776+ let ast = clarity:: vm:: ast:: parser:: v2:: parse (
2777+ src,
2778+ StackDepthLimits :: for_epoch ( StacksEpochId :: latest ( ) ) ,
2779+ )
2780+ . unwrap ( ) ;
27572781 let expected = format_with_default ( src) ;
27582782 let formatter = ClarityFormatter :: new ( Settings :: default ( ) ) ;
27592783 let result = formatter. format_ast ( & ast) ;
@@ -2879,7 +2903,11 @@ mod tests_formatter {
28792903 fn test_list_type_signature ( ) {
28802904 fn assert_list_type_signature ( src : & str , expected : bool ) {
28812905 let settings = Settings :: default ( ) ;
2882- let exprs = clarity:: vm:: ast:: parser:: v2:: parse ( src) . unwrap ( ) ;
2906+ let exprs = clarity:: vm:: ast:: parser:: v2:: parse (
2907+ src,
2908+ StackDepthLimits :: for_epoch ( StacksEpochId :: latest ( ) ) ,
2909+ )
2910+ . unwrap ( ) ;
28832911 let aggregator = Aggregator :: new ( & settings, & exprs, Some ( src) ) ;
28842912 let list_exprs = exprs[ 0 ] . match_list ( ) . unwrap ( ) ;
28852913 assert_eq ! ( aggregator. is_list_type_signature( list_exprs) , expected) ;
0 commit comments