From d69a5fbe1a4f909b7eba0eac81dd111fb7229232 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Tue, 18 May 2021 12:05:46 +0530 Subject: feat(lsp): support formatting json and markdown files (#10180) Resolves #9447 Resolves #9415 --- cli/tools/fmt.rs | 60 +++++++++++++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 36 deletions(-) (limited to 'cli/tools/fmt.rs') diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 9a16afeca..869403f07 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -152,6 +152,22 @@ fn format_json(file_text: &str) -> Result { dprint_plugin_json::format_text(&file_text, &json_config) } +/// Formats a single TS, TSX, JS, JSX, JSONC, JSON, or MD file. +pub fn format_file( + file_path: &Path, + file_text: &str, + config: dprint_plugin_typescript::configuration::Configuration, +) -> Result { + let ext = get_extension(file_path).unwrap_or_else(String::new); + if ext == "md" { + format_markdown(&file_text, config) + } else if matches!(ext.as_str(), "json" | "jsonc") { + format_json(&file_text) + } else { + dprint_plugin_typescript::format_text(&file_path, &file_text, &config) + } +} + async fn check_source_files( config: dprint_plugin_typescript::configuration::Configuration, paths: Vec, @@ -168,15 +184,8 @@ async fn check_source_files( move |file_path| { checked_files_count.fetch_add(1, Ordering::Relaxed); let file_text = read_file_contents(&file_path)?.text; - let ext = get_extension(&file_path).unwrap_or_else(String::new); - let r = if ext == "md" { - format_markdown(&file_text, config.clone()) - } else if matches!(ext.as_str(), "json" | "jsonc") { - format_json(&file_text) - } else { - dprint_plugin_typescript::format_text(&file_path, &file_text, &config) - }; - match r { + + match format_file(&file_path, &file_text, config) { Ok(formatted_text) => { if formatted_text != file_text { not_formatted_files_count.fetch_add(1, Ordering::Relaxed); @@ -229,19 +238,8 @@ async fn format_source_files( move |file_path| { checked_files_count.fetch_add(1, Ordering::Relaxed); let file_contents = read_file_contents(&file_path)?; - let ext = get_extension(&file_path).unwrap_or_else(String::new); - let r = if ext == "md" { - format_markdown(&file_contents.text, config.clone()) - } else if matches!(ext.as_str(), "json" | "jsonc") { - format_json(&file_contents.text) - } else { - dprint_plugin_typescript::format_text( - &file_path, - &file_contents.text, - &config, - ) - }; - match r { + + match format_file(&file_path, &file_contents.text, config) { Ok(formatted_text) => { if formatted_text != file_contents.text { write_file_contents( @@ -293,19 +291,9 @@ pub fn format_stdin(check: bool, ext: String) -> Result<(), AnyError> { return Err(generic_error("Failed to read from stdin")); } let config = get_typescript_config(); - let r = if ext.as_str() == "md" { - format_markdown(&source, config) - } else if matches!(ext.as_str(), "json" | "jsonc") { - format_json(&source) - } else { - // dprint will fallback to jsx parsing if parsing this as a .ts file doesn't work - dprint_plugin_typescript::format_text( - &PathBuf::from("_stdin.ts"), - &source, - &config, - ) - }; - match r { + let file_path = PathBuf::from(format!("_stdin.{}", ext)); + + match format_file(&file_path, &source, config) { Ok(formatted_text) => { if check { if formatted_text != source { @@ -330,7 +318,7 @@ fn files_str(len: usize) -> &'static str { } } -fn get_typescript_config( +pub fn get_typescript_config( ) -> dprint_plugin_typescript::configuration::Configuration { dprint_plugin_typescript::configuration::ConfigurationBuilder::new() .deno() -- cgit v1.2.3