summaryrefslogtreecommitdiff
path: root/cli/fmt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/fmt.rs')
-rw-r--r--cli/fmt.rs24
1 files changed, 11 insertions, 13 deletions
diff --git a/cli/fmt.rs b/cli/fmt.rs
index b81573294..0a9727f7a 100644
--- a/cli/fmt.rs
+++ b/cli/fmt.rs
@@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//! This module provides file formating utilities using
-//! [`dprint`](https://github.com/dsherret/dprint).
+//! [`dprint-plugin-typescript`](https://github.com/dsherret/dprint-plugin-typescript).
//!
//! At the moment it is only consumed using CLI but in
//! the future it can be easily extended to provide
@@ -58,7 +58,6 @@ async fn check_source_files(
paths: Vec<PathBuf>,
) -> Result<(), ErrBox> {
let not_formatted_files_count = Arc::new(AtomicUsize::new(0));
- let formatter = Arc::new(dprint::Formatter::new(config));
// prevent threads outputting at the same time
let output_lock = Arc::new(Mutex::new(0));
@@ -67,11 +66,11 @@ async fn check_source_files(
let not_formatted_files_count = not_formatted_files_count.clone();
move |file_path| {
let file_text = read_file_contents(&file_path)?.text;
- let r = formatter.format_text(&file_path, &file_text);
+ let r = dprint::format_text(&file_path, &file_text, &config);
match r {
Ok(formatted_text) => {
if formatted_text != file_text {
- not_formatted_files_count.fetch_add(1, Ordering::SeqCst);
+ not_formatted_files_count.fetch_add(1, Ordering::Relaxed);
let _g = output_lock.lock().unwrap();
match diff(&file_text, &formatted_text) {
Ok(diff) => {
@@ -105,7 +104,7 @@ async fn check_source_files(
.await?;
let not_formatted_files_count =
- not_formatted_files_count.load(Ordering::SeqCst);
+ not_formatted_files_count.load(Ordering::Relaxed);
if not_formatted_files_count == 0 {
Ok(())
} else {
@@ -122,14 +121,13 @@ async fn format_source_files(
paths: Vec<PathBuf>,
) -> Result<(), ErrBox> {
let formatted_files_count = Arc::new(AtomicUsize::new(0));
- let formatter = Arc::new(dprint::Formatter::new(config));
let output_lock = Arc::new(Mutex::new(0)); // prevent threads outputting at the same time
run_parallelized(paths, {
let formatted_files_count = formatted_files_count.clone();
move |file_path| {
let file_contents = read_file_contents(&file_path)?;
- let r = formatter.format_text(&file_path, &file_contents.text);
+ let r = dprint::format_text(&file_path, &file_contents.text, &config);
match r {
Ok(formatted_text) => {
if formatted_text != file_contents.text {
@@ -140,7 +138,7 @@ async fn format_source_files(
text: formatted_text,
},
)?;
- formatted_files_count.fetch_add(1, Ordering::SeqCst);
+ formatted_files_count.fetch_add(1, Ordering::Relaxed);
let _g = output_lock.lock().unwrap();
println!("{}", file_path.to_string_lossy());
}
@@ -156,7 +154,7 @@ async fn format_source_files(
})
.await?;
- let formatted_files_count = formatted_files_count.load(Ordering::SeqCst);
+ let formatted_files_count = formatted_files_count.load(Ordering::Relaxed);
debug!(
"Formatted {} {}",
formatted_files_count,
@@ -173,10 +171,10 @@ fn format_stdin(check: bool) -> Result<(), ErrBox> {
if stdin().read_to_string(&mut source).is_err() {
return Err(ErrBox::error("Failed to read from stdin"));
}
- let formatter = dprint::Formatter::new(get_config());
+ let config = get_config();
// dprint will fallback to jsx parsing if parsing this as a .ts file doesn't work
- match formatter.format_text(&PathBuf::from("_stdin.ts"), &source) {
+ match dprint::format_text(&PathBuf::from("_stdin.ts"), &source, &config) {
Ok(formatted_text) => {
if check {
if formatted_text != source {
@@ -245,7 +243,7 @@ struct FileContents {
had_bom: bool,
}
-fn read_file_contents(file_path: &PathBuf) -> Result<FileContents, ErrBox> {
+fn read_file_contents(file_path: &Path) -> Result<FileContents, ErrBox> {
let file_bytes = fs::read(&file_path)?;
let charset = text_encoding::detect_charset(&file_bytes);
let file_text = text_encoding::convert_to_utf8(&file_bytes, charset)?;
@@ -261,7 +259,7 @@ fn read_file_contents(file_path: &PathBuf) -> Result<FileContents, ErrBox> {
}
fn write_file_contents(
- file_path: &PathBuf,
+ file_path: &Path,
file_contents: FileContents,
) -> Result<(), ErrBox> {
let file_text = if file_contents.had_bom {