Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

refactor(rome_cli): format file in different flow #4651

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ indent_size = 2
indent_style = space

[*.rs]
indent_style = space
indent_size = 4
1 change: 0 additions & 1 deletion crates/rome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod diagnostics;
mod lint_file;
mod migrate;
mod process_file;
mod std_in;
Expand Down
23 changes: 18 additions & 5 deletions crates/rome_cli/src/execute/process_file.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
Expand Down
76 changes: 76 additions & 0 deletions crates/rome_cli/src/execute/process_file/format_file.rs
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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.
```

```block
Formatted 1 file(s) in <TIME>
Formatted 0 file(s) in <TIME>
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ internalError/io ━━━━━━━━━━━━━━━━━━━━━
# Emitted Messages

```block
format.js lint ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
format.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Size of format.js is 27 B which exceeds configured maximum of 16 B for this project. The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't.
```

```block
Compared 1 file(s) in <TIME>
Compared 0 file(s) in <TIME>
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ internalError/io ━━━━━━━━━━━━━━━━━━━━━
# Emitted Messages

```block
format.js lint ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
format.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Size of format.js is 27 B which exceeds configured maximum of 16 B for this project. The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't.
```

```block
Compared 1 file(s) in <TIME>
Compared 0 file(s) in <TIME>
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ check.js lint ━━━━━━━━━━━━━━━━━━━━━━
```

```block
Checked 0 file(s) in <TIME>
Checked 1 file(s) in <TIME>
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ check.js lint ━━━━━━━━━━━━━━━━━━━━━━
```

```block
Checked 0 file(s) in <TIME>
Checked 1 file(s) in <TIME>
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ check.js lint ━━━━━━━━━━━━━━━━━━━━━━
```

```block
Checked 0 file(s) in <TIME>
Checked 1 file(s) in <TIME>
```

```block
Expand Down