@@ -36,7 +36,7 @@ use write::FormatWrite;
3636pub use crate :: options:: * ;
3737pub use crate :: service:: source_type:: get_supported_source_type;
3838use crate :: {
39- formatter:: FormatContext ,
39+ formatter:: { FormatContext , Formatted , format_element :: document :: Document } ,
4040 generated:: ast_nodes:: { AstNode , AstNodes } ,
4141} ;
4242
@@ -53,20 +53,31 @@ impl<'a> Formatter<'a> {
5353 Self { allocator, source_text : "" , options }
5454 }
5555
56+ /// Formats the given AST `Program` and returns the IR before printing.
57+ pub fn doc ( mut self , program : & ' a Program < ' a > ) -> Document < ' a > {
58+ let formatted = self . format ( program) ;
59+ formatted. into_document ( )
60+ }
61+
62+ /// Formats the given AST `Program` and returns the formatted string.
5663 pub fn build ( mut self , program : & Program < ' a > ) -> String {
64+ let formatted = self . format ( program) ;
65+ formatted. print ( ) . unwrap ( ) . into_code ( )
66+ }
67+
68+ fn format ( mut self , program : & ' a Program < ' a > ) -> Formatted < ' a > {
5769 let parent = self . allocator . alloc ( AstNodes :: Dummy ( ) ) ;
5870 let program_node = AstNode :: new ( program, parent, self . allocator ) ;
5971
6072 let source_text = program. source_text ;
6173 self . source_text = source_text;
6274 let context = FormatContext :: new ( program, self . allocator , self . options ) ;
63- let formatted = formatter:: format (
75+ formatter:: format (
6476 program,
6577 context,
6678 formatter:: Arguments :: new ( & [ formatter:: Argument :: new ( & program_node) ] ) ,
6779 )
68- . unwrap ( ) ;
69- formatted. print ( ) . unwrap ( ) . into_code ( )
80+ . unwrap ( )
7081 }
7182}
7283
0 commit comments