This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rome_cli): format file in different flow
- Loading branch information
Showing
11 changed files
with
108 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ indent_size = 2 | |
indent_style = space | ||
|
||
[*.rs] | ||
indent_style = space | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters