diff options
Diffstat (limited to 'cli/tools/fmt.rs')
| -rw-r--r-- | cli/tools/fmt.rs | 176 |
1 files changed, 47 insertions, 129 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index d146c958e..56b6bd00b 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -8,7 +8,8 @@ //! the same functions as ops available in JS runtime. use crate::args::CliOptions; -use crate::args::FmtFlags; +use crate::args::FilesConfig; +use crate::args::FmtOptions; use crate::args::FmtOptionsConfig; use crate::args::ProseWrap; use crate::colors; @@ -17,7 +18,6 @@ use crate::util::file_watcher; use crate::util::file_watcher::ResolutionResult; use crate::util::fs::FileCollector; use crate::util::path::get_extension; -use crate::util::path::specifier_to_file_path; use crate::util::text_encoding; use deno_ast::ParsedSource; use deno_core::anyhow::bail; @@ -44,80 +44,41 @@ use crate::cache::IncrementalCache; /// Format JavaScript/TypeScript files. pub async fn format( - config: &CliOptions, - fmt_flags: FmtFlags, + cli_options: CliOptions, + fmt_options: FmtOptions, ) -> Result<(), AnyError> { - let maybe_fmt_config = config.to_fmt_config()?; - let deno_dir = config.resolve_deno_dir()?; - let FmtFlags { - files, - ignore, - check, - .. - } = fmt_flags.clone(); - - // First, prepare final configuration. - // Collect included and ignored files. CLI flags take precendence - // over config file, ie. if there's `files.ignore` in config file - // and `--ignore` CLI flag, only the flag value is taken into account. - let mut include_files = files.clone(); - let mut exclude_files = ignore; - - if let Some(fmt_config) = maybe_fmt_config.as_ref() { - if include_files.is_empty() { - include_files = fmt_config - .files - .include - .iter() - .filter_map(|s| specifier_to_file_path(s).ok()) - .collect::<Vec<_>>(); - } - - if exclude_files.is_empty() { - exclude_files = fmt_config - .files - .exclude - .iter() - .filter_map(|s| specifier_to_file_path(s).ok()) - .collect::<Vec<_>>(); - } + if fmt_options.is_stdin { + return format_stdin(fmt_options); } - if include_files.is_empty() { - include_files = [std::env::current_dir()?].to_vec(); - } - - // Now do the same for options - let fmt_options = resolve_fmt_options( - &fmt_flags, - maybe_fmt_config.map(|c| c.options).unwrap_or_default(), - ); + let files = fmt_options.files; + let check = fmt_options.check; + let fmt_config_options = fmt_options.options; let resolver = |changed: Option<Vec<PathBuf>>| { let files_changed = changed.is_some(); - let result = - collect_fmt_files(&include_files, &exclude_files).map(|files| { - let refmt_files = if let Some(paths) = changed { - if check { - files - .iter() - .any(|path| paths.contains(path)) - .then_some(files) - .unwrap_or_else(|| [].to_vec()) - } else { - files - .into_iter() - .filter(|path| paths.contains(path)) - .collect::<Vec<_>>() - } + let result = collect_fmt_files(&files).map(|files| { + let refmt_files = if let Some(paths) = changed { + if check { + files + .iter() + .any(|path| paths.contains(path)) + .then_some(files) + .unwrap_or_else(|| [].to_vec()) } else { files - }; - (refmt_files, fmt_options.clone()) - }); + .into_iter() + .filter(|path| paths.contains(path)) + .collect::<Vec<_>>() + } + } else { + files + }; + (refmt_files, fmt_config_options.clone()) + }); - let paths_to_watch = include_files.clone(); + let paths_to_watch = files.include.clone(); async move { if files_changed && matches!(result, Ok((ref files, _)) if files.is_empty()) @@ -131,6 +92,7 @@ pub async fn format( } } }; + let deno_dir = cli_options.resolve_deno_dir()?; let deno_dir = &deno_dir; let operation = |(paths, fmt_options): (Vec<PathBuf>, FmtOptionsConfig)| async move { let incremental_cache = Arc::new(IncrementalCache::new( @@ -148,40 +110,36 @@ pub async fn format( Ok(()) }; - if config.watch_paths().is_some() { + if cli_options.watch_paths().is_some() { file_watcher::watch_func( resolver, operation, file_watcher::PrintConfig { job_name: "Fmt".to_string(), - clear_screen: !config.no_clear_screen(), + clear_screen: !cli_options.no_clear_screen(), }, ) .await?; } else { - let files = - collect_fmt_files(&include_files, &exclude_files).and_then(|files| { - if files.is_empty() { - Err(generic_error("No target files found.")) - } else { - Ok(files) - } - })?; - operation((files, fmt_options.clone())).await?; + let files = collect_fmt_files(&files).and_then(|files| { + if files.is_empty() { + Err(generic_error("No target files found.")) + } else { + Ok(files) + } + })?; + operation((files, fmt_config_options)).await?; } Ok(()) } -fn collect_fmt_files( - include_files: &[PathBuf], - exclude_files: &[PathBuf], -) -> Result<Vec<PathBuf>, AnyError> { +fn collect_fmt_files(files: &FilesConfig) -> Result<Vec<PathBuf>, AnyError> { FileCollector::new(is_supported_ext_fmt) .ignore_git_folder() .ignore_node_modules() - .add_ignore_paths(exclude_files) - .collect_files(include_files) + .add_ignore_paths(&files.exclude) + .collect_files(&files.include) } /// Formats markdown (using <https://github.com/dprint/dprint-plugin-markdown>) and its code blocks @@ -275,11 +233,11 @@ pub fn format_file( pub fn format_parsed_source( parsed_source: &ParsedSource, - fmt_options: FmtOptionsConfig, + fmt_options: &FmtOptionsConfig, ) -> Result<Option<String>, AnyError> { dprint_plugin_typescript::format_parsed_source( parsed_source, - &get_resolved_typescript_config(&fmt_options), + &get_resolved_typescript_config(fmt_options), ) } @@ -501,19 +459,14 @@ fn format_ensure_stable( /// Format stdin and write result to stdout. /// Treats input as TypeScript or as set by `--ext` flag. /// Compatible with `--check` flag. -pub fn format_stdin( - fmt_flags: FmtFlags, - fmt_options: FmtOptionsConfig, -) -> Result<(), AnyError> { +fn format_stdin(fmt_options: FmtOptions) -> Result<(), AnyError> { let mut source = String::new(); if stdin().read_to_string(&mut source).is_err() { bail!("Failed to read from stdin"); } - let file_path = PathBuf::from(format!("_stdin.{}", fmt_flags.ext)); - let fmt_options = resolve_fmt_options(&fmt_flags, fmt_options); - - let formatted_text = format_file(&file_path, &source, &fmt_options)?; - if fmt_flags.check { + let file_path = PathBuf::from(format!("_stdin.{}", fmt_options.ext)); + let formatted_text = format_file(&file_path, &source, &fmt_options.options)?; + if fmt_options.check { if formatted_text.is_some() { println!("Not formatted stdin"); } @@ -531,41 +484,6 @@ fn files_str(len: usize) -> &'static str { } } -fn resolve_fmt_options( - fmt_flags: &FmtFlags, - options: FmtOptionsConfig, -) -> FmtOptionsConfig { - let mut options = options; - - if let Some(use_tabs) = fmt_flags.use_tabs { - options.use_tabs = Some(use_tabs); - } - - if let Some(line_width) = fmt_flags.line_width { - options.line_width = Some(line_width.get()); - } - - if let Some(indent_width) = fmt_flags.indent_width { - options.indent_width = Some(indent_width.get()); - } - - if let Some(single_quote) = fmt_flags.single_quote { - options.single_quote = Some(single_quote); - } - - if let Some(prose_wrap) = &fmt_flags.prose_wrap { - options.prose_wrap = Some(match prose_wrap.as_str() { - "always" => ProseWrap::Always, - "never" => ProseWrap::Never, - "preserve" => ProseWrap::Preserve, - // validators in `flags.rs` makes other values unreachable - _ => unreachable!(), - }); - } - - options -} - fn get_resolved_typescript_config( options: &FmtOptionsConfig, ) -> dprint_plugin_typescript::configuration::Configuration { |
