@@ -13,6 +13,14 @@ use super::generation::generate;
1313pub use super :: generation:: ExternalFormatter ;
1414use super :: swc:: parse_swc_ast;
1515
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+
1624/// Formats a file.
1725///
1826/// Returns the file text or an error when it failed to parse.
@@ -36,31 +44,24 @@ use super::swc::parse_swc_ast;
3644/// // now format many files (it is recommended to parallelize this)
3745/// let files_to_format = vec![(PathBuf::from("path/to/file.ts"), "const t = 5 ;")];
3846/// for (file_path, file_text) in files_to_format {
39- /// 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+ /// });
4054/// // save result here...
4155/// }
4256/// ```
43- pub fn format_text ( file_path : & Path , file_extension : Option < & str > , file_text : String , config : & Configuration ) -> Result < Option < String > > {
44- format_text_inner ( file_path, file_extension, file_text, config, None )
45- }
46-
47- pub fn format_text_with_external_formatter (
48- file_path : & Path ,
49- file_extension : Option < & str > ,
50- file_text : String ,
51- config : & Configuration ,
52- external_formatter : ExternalFormatter ,
53- ) -> Result < Option < String > > {
54- format_text_inner ( file_path, file_extension, file_text, config, Some ( external_formatter) )
55- }
56-
57- fn format_text_inner (
58- file_path : & Path ,
59- file_extension : Option < & str > ,
60- file_text : String ,
61- config : & Configuration ,
62- external_formatter : Option < ExternalFormatter > ,
63- ) -> 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;
6465 if super :: utils:: file_text_has_ignore_comment ( & file_text, & config. ignore_file_comment_text ) {
6566 Ok ( None )
6667 } else {
@@ -82,17 +83,16 @@ fn format_text_inner(
8283}
8384
8485/// Formats an already parsed source. This is useful as a performance optimization.
85- 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 > > {
8687 if super :: utils:: file_text_has_ignore_comment ( source. text ( ) , & config. ignore_file_comment_text ) {
8788 Ok ( None )
8889 } else {
8990 ensure_no_specific_syntax_errors ( source) ?;
90- // TODO(bartlomieju): support external formatter
91- inner_format ( source, config, None )
91+ inner_format ( source, config, external_formatter)
9292 }
9393}
9494
95- fn inner_format ( parsed_source : & ParsedSource , config : & Configuration , external_formatter : Option < ExternalFormatter > ) -> Result < Option < String > > {
95+ fn inner_format ( parsed_source : & ParsedSource , config : & Configuration , external_formatter : Option < & ExternalFormatter > ) -> Result < Option < String > > {
9696 let result = dprint_core:: formatting:: format (
9797 || {
9898 #[ allow( clippy:: let_and_return) ]
@@ -133,9 +133,15 @@ mod test {
133133 fn strips_bom ( ) {
134134 for input_text in [ "\u{FEFF} const t = 5;\n " , "\u{FEFF} const t = 5;" ] {
135135 let config = crate :: configuration:: ConfigurationBuilder :: new ( ) . build ( ) ;
136- let result = format_text ( & std:: path:: PathBuf :: from ( "test.ts" ) , None , input_text. into ( ) , & config)
137- . unwrap ( )
138- . 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 ( ) ;
139145 assert_eq ! ( result, "const t = 5;\n " ) ;
140146 }
141147 }
0 commit comments