@@ -10,8 +10,17 @@ use crate::swc::ensure_no_specific_syntax_errors;
1010
1111use super :: configuration:: Configuration ;
1212use super :: generation:: generate;
13+ pub use super :: generation:: ExternalFormatter ;
1314use super :: swc:: parse_swc_ast;
1415
16+ pub struct FormatTextOptions < ' a > {
17+ pub path : & ' a Path ,
18+ pub extension : Option < & ' a str > ,
19+ pub text : String ,
20+ pub config : & ' a Configuration ,
21+ pub external_formatter : Option < & ' a ExternalFormatter > ,
22+ }
23+
1524/// Formats a file.
1625///
1726/// Returns the file text or an error when it failed to parse.
@@ -35,19 +44,32 @@ use super::swc::parse_swc_ast;
3544/// // now format many files (it is recommended to parallelize this)
3645/// let files_to_format = vec![(PathBuf::from("path/to/file.ts"), "const t = 5 ;")];
3746/// for (file_path, file_text) in files_to_format {
38- /// let result = format_text(&file_path, None, file_text.into(), &config);
47+ /// let result = format_text(FormatTextOptions {
48+ /// path: &file_path,
49+ /// extension: None,
50+ /// text: file_text.into(),
51+ /// config: &config,
52+ /// external_formatter: None,
53+ /// });
3954/// // save result here...
4055/// }
4156/// ```
42- pub fn format_text ( file_path : & Path , file_extension : Option < & str > , file_text : String , config : & Configuration ) -> Result < Option < String > > {
57+ pub fn format_text ( options : FormatTextOptions ) -> Result < Option < String > > {
58+ let FormatTextOptions {
59+ path : file_path,
60+ extension : file_extension,
61+ text : file_text,
62+ config,
63+ external_formatter,
64+ } = options;
4365 if super :: utils:: file_text_has_ignore_comment ( & file_text, & config. ignore_file_comment_text ) {
4466 Ok ( None )
4567 } else {
4668 let had_bom = file_text. starts_with ( "\u{FEFF} " ) ;
4769 let file_text = if had_bom { file_text[ 3 ..] . to_string ( ) } else { file_text } ;
4870 let file_text: Arc < str > = file_text. into ( ) ;
4971 let parsed_source = parse_swc_ast ( file_path, file_extension, file_text) ?;
50- match inner_format ( & parsed_source, config) ? {
72+ match inner_format ( & parsed_source, config, external_formatter ) ? {
5173 Some ( new_text) => Ok ( Some ( new_text) ) ,
5274 None => {
5375 if had_bom {
@@ -61,20 +83,20 @@ pub fn format_text(file_path: &Path, file_extension: Option<&str>, file_text: St
6183}
6284
6385/// Formats an already parsed source. This is useful as a performance optimization.
64- pub fn format_parsed_source ( source : & ParsedSource , config : & Configuration ) -> Result < Option < String > > {
86+ pub fn format_parsed_source ( source : & ParsedSource , config : & Configuration , external_formatter : Option < & ExternalFormatter > ) -> Result < Option < String > > {
6587 if super :: utils:: file_text_has_ignore_comment ( source. text ( ) , & config. ignore_file_comment_text ) {
6688 Ok ( None )
6789 } else {
6890 ensure_no_specific_syntax_errors ( source) ?;
69- inner_format ( source, config)
91+ inner_format ( source, config, external_formatter )
7092 }
7193}
7294
73- fn inner_format ( parsed_source : & ParsedSource , config : & Configuration ) -> Result < Option < String > > {
95+ fn inner_format ( parsed_source : & ParsedSource , config : & Configuration , external_formatter : Option < & ExternalFormatter > ) -> Result < Option < String > > {
7496 let result = dprint_core:: formatting:: format (
7597 || {
7698 #[ allow( clippy:: let_and_return) ]
77- let print_items = generate ( parsed_source, config) ;
99+ let print_items = generate ( parsed_source, config, external_formatter ) ;
78100 // println!("{}", print_items.get_as_text());
79101 print_items
80102 } ,
@@ -91,7 +113,7 @@ fn inner_format(parsed_source: &ParsedSource, config: &Configuration) -> Result<
91113pub fn trace_file ( file_path : & Path , file_text : & str , config : & Configuration ) -> dprint_core:: formatting:: TracingResult {
92114 let parsed_source = parse_swc_ast ( file_path, None , file_text. into ( ) ) . unwrap ( ) ;
93115 ensure_no_specific_syntax_errors ( & parsed_source) . unwrap ( ) ;
94- dprint_core:: formatting:: trace_printing ( || generate ( & parsed_source, config) , config_to_print_options ( file_text, config) )
116+ dprint_core:: formatting:: trace_printing ( || generate ( & parsed_source, config, None ) , config_to_print_options ( file_text, config) )
95117}
96118
97119fn config_to_print_options ( file_text : & str , config : & Configuration ) -> PrintOptions {
@@ -111,9 +133,15 @@ mod test {
111133 fn strips_bom ( ) {
112134 for input_text in [ "\u{FEFF} const t = 5;\n " , "\u{FEFF} const t = 5;" ] {
113135 let config = crate :: configuration:: ConfigurationBuilder :: new ( ) . build ( ) ;
114- let result = format_text ( & std:: path:: PathBuf :: from ( "test.ts" ) , None , input_text. into ( ) , & config)
115- . unwrap ( )
116- . unwrap ( ) ;
136+ let result = format_text ( FormatTextOptions {
137+ path : & std:: path:: PathBuf :: from ( "test.ts" ) ,
138+ extension : None ,
139+ text : input_text. into ( ) ,
140+ config : & config,
141+ external_formatter : None ,
142+ } )
143+ . unwrap ( )
144+ . unwrap ( ) ;
117145 assert_eq ! ( result, "const t = 5;\n " ) ;
118146 }
119147 }
0 commit comments