diff options
Diffstat (limited to 'cli/fmt.rs')
| -rw-r--r-- | cli/fmt.rs | 65 |
1 files changed, 33 insertions, 32 deletions
diff --git a/cli/fmt.rs b/cli/fmt.rs index 8d2f982df..edfc27730 100644 --- a/cli/fmt.rs +++ b/cli/fmt.rs @@ -37,15 +37,23 @@ fn is_supported(path: &Path) -> bool { fn get_config() -> dprint::configuration::Configuration { use dprint::configuration::*; - ConfigurationBuilder::new() - .line_width(80) - .indent_width(2) - .next_control_flow_position(NextControlFlowPosition::SameLine) - .binary_expression_operator_position(OperatorPosition::SameLine) - .brace_position(BracePosition::SameLine) // default is NextLineIfHanging - .comment_line_force_space_after_slashes(false) - .construct_signature_space_after_new_keyword(true) - .build() + ConfigurationBuilder::new().prettier().build() +} + +// TODO(ry) dprint seems to panic unnecessarally sometimes. Until it matures +// we'll use a catch_unwind to avoid passing it on to our users. +fn format_text_ignore_panic( + file_path_str: &str, + file_contents: &str, + config: &dprint::configuration::Configuration, +) -> Result<Option<String>, String> { + let catch_result = std::panic::catch_unwind(|| { + dprint::format_text(file_path_str, file_contents, config) + }); + match catch_result { + Ok(dprint_result) => dprint_result, + Err(e) => Err(format!("dprint panic '{}' {:?}", file_path_str, e)), + } } fn check_source_files( @@ -57,7 +65,7 @@ fn check_source_files( for file_path in paths { let file_path_str = file_path.to_string_lossy(); let file_contents = fs::read_to_string(&file_path).unwrap(); - match dprint::format_text(&file_path_str, &file_contents, &config) { + match format_text_ignore_panic(&file_path_str, &file_contents, &config) { Ok(None) => { // nothing to format, pass } @@ -101,30 +109,23 @@ fn format_source_files( for file_path in paths { let file_path_str = file_path.to_string_lossy(); let file_contents = fs::read_to_string(&file_path)?; - // TODO(ry) dprint seems to panic unnecessarally sometimes. Until it matures - // we'll use a catch_unwind to avoid passing it on to our users. - let catch_unwind_result = std::panic::catch_unwind(|| { - dprint::format_text(&file_path_str, &file_contents, &config) - }); - if let Ok(dprint_result) = catch_unwind_result { - match dprint_result { - Ok(None) => { - // nothing to format, pass - } - Ok(Some(formatted_text)) => { - if formatted_text != file_contents { - println!("{}", file_path_str); - fs::write(&file_path, formatted_text)?; - not_formatted_files.push(file_path); - } - } - Err(e) => { - eprintln!("Error formatting: {}", &file_path_str); - eprintln!(" {}", e); + let dprint_result = + format_text_ignore_panic(&file_path_str, &file_contents, &config); + match dprint_result { + Ok(None) => { + // nothing to format, pass + } + Ok(Some(formatted_text)) => { + if formatted_text != file_contents { + println!("{}", file_path_str); + fs::write(&file_path, formatted_text)?; + not_formatted_files.push(file_path); } } - } else { - eprintln!("dprint panic {}", file_path_str); + Err(e) => { + eprintln!("Error formatting: {}", &file_path_str); + eprintln!(" {}", e); + } } } |
