From acf53a6680ac986e02f38b28446f1c31ac2f1ed3 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 3 Jul 2023 14:32:25 +0100 Subject: [PATCH] refactor(rome_cli): format file in different flow --- .editorconfig | 1 + crates/rome_cli/src/execute/mod.rs | 1 - crates/rome_cli/src/execute/process_file.rs | 23 ++++-- .../src/execute/process_file/format_file.rs | 76 +++++++++++++++++++ .../execute/{ => process_file}/lint_file.rs | 5 +- .../main_commands_format/file_too_large.snap | 4 +- .../file_too_large_cli_limit.snap | 4 +- .../file_too_large_config_limit.snap | 4 +- .../main_commands_lint/file_too_large.snap | 2 +- .../file_too_large_cli_limit.snap | 2 +- .../file_too_large_config_limit.snap | 2 +- 11 files changed, 108 insertions(+), 16 deletions(-) create mode 100644 crates/rome_cli/src/execute/process_file/format_file.rs rename crates/rome_cli/src/execute/{ => process_file}/lint_file.rs (99%) diff --git a/.editorconfig b/.editorconfig index 115b0ff1dc1..3f3d473a6d5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -14,4 +14,5 @@ indent_size = 2 indent_style = space [*.rs] +indent_style = space indent_size = 4 diff --git a/crates/rome_cli/src/execute/mod.rs b/crates/rome_cli/src/execute/mod.rs index 737c9c5f204..ec7b64d9891 100644 --- a/crates/rome_cli/src/execute/mod.rs +++ b/crates/rome_cli/src/execute/mod.rs @@ -1,5 +1,4 @@ mod diagnostics; -mod lint_file; mod migrate; mod process_file; mod std_in; diff --git a/crates/rome_cli/src/execute/process_file.rs b/crates/rome_cli/src/execute/process_file.rs index 051345382bb..f4f932b724d 100644 --- a/crates/rome_cli/src/execute/process_file.rs +++ b/crates/rome_cli/src/execute/process_file.rs @@ -1,5 +1,9 @@ +mod format_file; +mod lint_file; + use crate::execute::diagnostics::{ResultExt, ResultIoExt, SkippedDiagnostic, UnhandledDiagnostic}; -use crate::execute::lint_file::{lint_file, LintFile}; +use crate::execute::process_file::format_file::{format_file, FormatFile}; +use crate::execute::process_file::lint_file::{lint_file, LintFile}; use crate::execute::traverse::TraversalOptions; use crate::execute::TraversalMode; use crate::{CliDiagnostic, FormatterReportFileDetail}; @@ -182,11 +186,20 @@ pub(crate) fn process_file(ctx: &TraversalOptions, path: &Path) -> FileResult { // lower the changes to break the business logic of other traversal. // // This would definitely repeat the code, but it's worth the effort in the long run. - if let TraversalMode::Lint { .. } = ctx.execution.traversal_mode { - // the unsupported case should be handled already at this point - if file_features.supports_for(&FeatureName::Lint) { - return lint_file(LintFile { ctx, path }); + match ctx.execution.traversal_mode { + TraversalMode::Lint { .. } => { + // the unsupported case should be handled already at this point + if file_features.supports_for(&FeatureName::Lint) { + return lint_file(LintFile { ctx, path }); + } } + TraversalMode::Format { .. } => { + // the unsupported case should be handled already at this point + if file_features.supports_for(&FeatureName::Format) { + return format_file(FormatFile { ctx, path }); + } + } + _ => {} } let open_options = OpenOptions::default() diff --git a/crates/rome_cli/src/execute/process_file/format_file.rs b/crates/rome_cli/src/execute/process_file/format_file.rs new file mode 100644 index 00000000000..96d72763a65 --- /dev/null +++ b/crates/rome_cli/src/execute/process_file/format_file.rs @@ -0,0 +1,76 @@ +use crate::execute::diagnostics::{ResultExt, ResultIoExt}; +use crate::execute::process_file::{DiffKind, FileResult, FileStatus, Message}; +use crate::execute::traverse::TraversalOptions; +use crate::execute::TraversalMode; +use crate::FormatterReportFileDetail; +use rome_diagnostics::category; +use rome_fs::{OpenOptions, RomePath}; +use rome_service::file_handlers::Language; +use rome_service::workspace::{FileGuard, OpenFileParams}; +use std::path::Path; + +pub(crate) struct FormatFile<'ctx, 'app> { + pub(crate) ctx: &'app TraversalOptions<'ctx, 'app>, + pub(crate) path: &'app Path, +} + +pub(crate) fn format_file(payload: FormatFile) -> FileResult { + let FormatFile { ctx, path } = payload; + let rome_path = RomePath::new(path); + let open_options = OpenOptions::default() + .read(true) + .write(ctx.execution.requires_write_access()); + let mut file = ctx + .fs + .open_with_options(path, open_options) + .with_file_path(path.display().to_string())?; + + let mut input = String::new(); + file.read_to_string(&mut input) + .with_file_path(path.display().to_string())?; + + let file_guard = FileGuard::open( + ctx.workspace, + OpenFileParams { + path: rome_path, + version: 0, + content: input.clone(), + language_hint: Language::default(), + }, + ) + .with_file_path_and_code(path.display().to_string(), category!("internalError/fs"))?; + let printed = file_guard + .format_file() + .with_file_path_and_code(path.display().to_string(), category!("format"))?; + + let output = printed.into_code(); + let should_write = match ctx.execution.traversal_mode { + TraversalMode::Format { write, .. } => write, + _ => false, + }; + ctx.increment_processed(); + if output != input { + if should_write { + file.set_content(output.as_bytes()) + .with_file_path(path.display().to_string())?; + file_guard.change_file(file.file_version(), output)?; + } else { + if !ctx.execution.should_report_to_terminal() { + ctx.push_format_stat( + path.display().to_string(), + FormatterReportFileDetail { + formatted_content: Some(output.clone()), + }, + ) + } + + return Ok(FileStatus::Message(Message::Diff { + file_name: path.display().to_string(), + old: input, + new: output, + diff_kind: DiffKind::Format, + })); + } + } + Ok(FileStatus::Success) +} diff --git a/crates/rome_cli/src/execute/lint_file.rs b/crates/rome_cli/src/execute/process_file/lint_file.rs similarity index 99% rename from crates/rome_cli/src/execute/lint_file.rs rename to crates/rome_cli/src/execute/process_file/lint_file.rs index f82d028f7d5..a869b61f03d 100644 --- a/crates/rome_cli/src/execute/lint_file.rs +++ b/crates/rome_cli/src/execute/process_file/lint_file.rs @@ -41,6 +41,9 @@ pub(crate) fn lint_file(payload: LintFile) -> FileResult { }, ) .with_file_path_and_code(path.display().to_string(), category!("internalError/fs"))?; + + ctx.increment_processed(); + if let Some(fix_mode) = ctx.execution.as_fix_file_mode() { let fixed = file_guard .fix_file(*fix_mode, false) @@ -74,7 +77,7 @@ pub(crate) fn lint_file(payload: LintFile) -> FileResult { skipped_diagnostics: result.skipped_diagnostics, }) }; - ctx.increment_processed(); + if errors > 0 { return Ok(FileStatus::Message(Message::ApplyError( CliDiagnostic::file_apply_error(path.display().to_string()), diff --git a/crates/rome_cli/tests/snapshots/main_commands_format/file_too_large.snap b/crates/rome_cli/tests/snapshots/main_commands_format/file_too_large.snap index 96c3fa18a97..d3c8ddfbba4 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_format/file_too_large.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_format/file_too_large.snap @@ -16,7 +16,7 @@ internalError/io ━━━━━━━━━━━━━━━━━━━━━ # Emitted Messages ```block -format.js lint ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +format.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ × Size of format.js is 1.0 MiB which exceeds configured maximum of 1.0 MiB for this project. The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't. @@ -24,7 +24,7 @@ format.js lint ━━━━━━━━━━━━━━━━━━━━━ ``` ```block -Formatted 1 file(s) in