Skip to content

Commit d78b251

Browse files
committed
switch to options bag
1 parent ba200dc commit d78b251

6 files changed

Lines changed: 48 additions & 36 deletions

File tree

src/format_text.rs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ use super::generation::generate;
1313
pub use super::generation::ExternalFormatter;
1414
use 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
}

src/generation/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ use crate::utils::Stack;
4545
/// cases the templates will be left as they are.
4646
///
4747
/// Only templates with no interpolation are supported.
48-
pub type ExternalFormatter = Box<dyn Fn(MediaType, String, &Configuration) -> Option<String>>;
48+
pub type ExternalFormatter = dyn Fn(MediaType, String, &Configuration) -> Option<String>;
4949

5050
pub struct Context<'a> {
5151
pub media_type: MediaType,
5252
pub program: Program<'a>,
5353
pub config: &'a Configuration,
5454
pub comments: CommentTracker<'a>,
55-
pub external_formatter: Option<ExternalFormatter>,
55+
pub external_formatter: Option<&'a ExternalFormatter>,
5656
pub token_finder: TokenFinder<'a>,
5757
pub current_node: Node<'a>,
5858
pub parent_stack: Stack<Node<'a>>,
@@ -79,7 +79,7 @@ impl<'a> Context<'a> {
7979
current_node: Node<'a>,
8080
program: Program<'a>,
8181
config: &'a Configuration,
82-
external_formatter: Option<ExternalFormatter>,
82+
external_formatter: Option<&'a ExternalFormatter>,
8383
) -> Context<'a> {
8484
Context {
8585
media_type,

src/generation/generate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use super::*;
2525
use crate::configuration::*;
2626
use crate::utils;
2727

28-
pub fn generate(parsed_source: &ParsedSource, config: &Configuration, external_formatter: Option<ExternalFormatter>) -> PrintItems {
28+
pub fn generate(parsed_source: &ParsedSource, config: &Configuration, external_formatter: Option<&ExternalFormatter>) -> PrintItems {
2929
// eprintln!("Leading: {:?}", parsed_source.comments().leading_map());
3030
// eprintln!("Trailing: {:?}", parsed_source.comments().trailing_map());
3131

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ mod utils;
1818

1919
pub use format_text::format_parsed_source;
2020
pub use format_text::format_text;
21-
pub use format_text::format_text_with_external_formatter;
2221
pub use format_text::ExternalFormatter;
22+
pub use format_text::FormatTextOptions;
2323

2424
#[cfg(feature = "tracing")]
2525
pub use format_text::trace_file;

src/swc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,6 @@ Merge conflict marker encountered. at file:///test.ts:6:1
359359
// a source file that had a non-fatal diagnostic
360360
let parsed_source = parse_inner_no_diagnostic_check(&file_path, None, text.into()).unwrap();
361361
let config = ConfigurationBuilder::new().build();
362-
assert_eq!(crate::format_parsed_source(&parsed_source, &config).err().unwrap().to_string(), expected);
362+
assert_eq!(crate::format_parsed_source(&parsed_source, &config, None).err().unwrap().to_string(), expected);
363363
}
364364
}

tests/spec_test.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ fn main() {
9696
let config_result = resolve_config(spec_config, &global_config);
9797
ensure_no_diagnostics(&config_result.diagnostics);
9898

99-
format_text_with_external_formatter(file_name, None, file_text.into(), &config_result.config, Box::new(external_formatter))
99+
format_text(FormatTextOptions {
100+
path: file_name,
101+
extension: None,
102+
text: file_text.into(),
103+
config: &config_result.config,
104+
external_formatter: Some(&external_formatter),
105+
})
100106
})
101107
},
102108
Arc::new(move |_file_name, _file_text, _spec_config| {

0 commit comments

Comments
 (0)