@@ -3,14 +3,16 @@ use std::path::Path;
33use dprint_core:: formatting:: * ;
44use dprint_core:: types:: ErrBox ;
55use dprint_core:: configuration:: { resolve_new_line_kind} ;
6+ use swc_ast_view:: TokenAndSpan ;
7+ use swc_common:: comments:: SingleThreadedCommentsMapInner ;
68
79use super :: parsing:: parse;
810use super :: swc:: parse_swc_ast;
911use super :: configuration:: Configuration ;
1012
1113/// Formats a file.
1214///
13- /// Returns the file text `Ok(formatted_text)` or an error when it failed to parse.
15+ /// Returns the file text or an error when it failed to parse.
1416///
1517/// # Example
1618///
@@ -42,17 +44,51 @@ pub fn format_text(file_path: &Path, file_text: &str, config: &Configuration) ->
4244
4345 let parsed_source_file = parse_swc_ast ( file_path, file_text) ?;
4446 Ok ( dprint_core:: formatting:: format ( || {
45- let print_items = parse ( & parsed_source_file, config) ;
47+ let print_items = parse ( & SourceFileInfo {
48+ module : & parsed_source_file. module ,
49+ info : & parsed_source_file. info ,
50+ tokens : & parsed_source_file. tokens ,
51+ leading_comments : & parsed_source_file. leading_comments ,
52+ trailing_comments : & parsed_source_file. trailing_comments ,
53+ } , config) ;
4654 // println!("{}", print_items.get_as_text());
4755 print_items
4856 } , config_to_print_options ( file_text, config) ) )
4957}
5058
59+ #[ derive( Clone ) ]
60+ pub struct SourceFileInfo < ' a > {
61+ pub module : & ' a swc_ecmascript:: ast:: Module ,
62+ pub info : & ' a dyn swc_ast_view:: SourceFile ,
63+ pub tokens : & ' a [ TokenAndSpan ] ,
64+ pub leading_comments : & ' a SingleThreadedCommentsMapInner ,
65+ pub trailing_comments : & ' a SingleThreadedCommentsMapInner ,
66+ }
67+
68+ /// Formats the already parsed file. This is useful as a performance optimization.
69+ pub fn format_parsed_file ( info : & SourceFileInfo < ' _ > , config : & Configuration ) -> Result < String , ErrBox > {
70+ if super :: utils:: file_text_has_ignore_comment ( info. info . text ( ) , & config. ignore_file_comment_text ) {
71+ return Ok ( info. info . text ( ) . to_string ( ) ) ;
72+ }
73+
74+ Ok ( dprint_core:: formatting:: format ( || {
75+ let print_items = parse ( & info, config) ;
76+ // println!("{}", print_items.get_as_text());
77+ print_items
78+ } , config_to_print_options ( info. info . text ( ) , config) ) )
79+ }
80+
5181#[ cfg( feature = "tracing" ) ]
5282pub fn trace_file ( file_path : & Path , file_text : & str , config : & Configuration ) -> dprint_core:: formatting:: TracingResult {
5383 let parsed_source_file = parse_swc_ast ( file_path, file_text) . expect ( "Expected to parse to SWC AST." ) ;
5484 dprint_core:: formatting:: trace_printing (
55- || parse ( & parsed_source_file, config) ,
85+ || parse ( & SourceFileInfo {
86+ info : & parsed_source_file. info ,
87+ module : & parsed_source_file. module ,
88+ tokens : & parsed_source_file. tokens ,
89+ leading_comments : & parsed_source_file. leading_comments ,
90+ trailing_comments : & parsed_source_file. trailing_comments ,
91+ } , config) ,
5692 config_to_print_options ( file_text, config) ,
5793 )
5894}
0 commit comments