mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:04:51 +00:00
use FormatResult::diff for checking, revert other changes
to FormatResult and to FormattedSource
This commit is contained in:
parent
959f55ae05
commit
2ce8f80877
2 changed files with 17 additions and 43 deletions
|
@ -295,10 +295,7 @@ pub(crate) fn format_path(
|
||||||
// Format the source.
|
// Format the source.
|
||||||
let format_result = match format_source(&unformatted, source_type, Some(path), settings, range)?
|
let format_result = match format_source(&unformatted, source_type, Some(path), settings, range)?
|
||||||
{
|
{
|
||||||
FormattedSource::Formatted {
|
FormattedSource::Formatted(formatted) => match mode {
|
||||||
formatted,
|
|
||||||
unformatted,
|
|
||||||
} => match mode {
|
|
||||||
FormatMode::Write => {
|
FormatMode::Write => {
|
||||||
let mut writer = File::create(path).map_err(|err| {
|
let mut writer = File::create(path).map_err(|err| {
|
||||||
FormatCommandError::Write(Some(path.to_path_buf()), err.into())
|
FormatCommandError::Write(Some(path.to_path_buf()), err.into())
|
||||||
|
@ -316,16 +313,9 @@ pub(crate) fn format_path(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FormatResult::Formatted {
|
FormatResult::Formatted
|
||||||
unformatted,
|
|
||||||
formatted,
|
|
||||||
}
|
}
|
||||||
}
|
FormatMode::Check | FormatMode::Diff => FormatResult::Diff {
|
||||||
FormatMode::Check => FormatResult::Formatted {
|
|
||||||
unformatted,
|
|
||||||
formatted,
|
|
||||||
},
|
|
||||||
FormatMode::Diff => FormatResult::Diff {
|
|
||||||
unformatted,
|
unformatted,
|
||||||
formatted,
|
formatted,
|
||||||
},
|
},
|
||||||
|
@ -350,10 +340,7 @@ pub(crate) fn format_path(
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) enum FormattedSource {
|
pub(crate) enum FormattedSource {
|
||||||
/// The source was formatted, and the [`SourceKind`] contains the transformed source code.
|
/// The source was formatted, and the [`SourceKind`] contains the transformed source code.
|
||||||
Formatted {
|
Formatted(SourceKind),
|
||||||
formatted: SourceKind,
|
|
||||||
unformatted: SourceKind,
|
|
||||||
},
|
|
||||||
/// The source was unchanged.
|
/// The source was unchanged.
|
||||||
Unchanged,
|
Unchanged,
|
||||||
}
|
}
|
||||||
|
@ -361,13 +348,7 @@ pub(crate) enum FormattedSource {
|
||||||
impl From<FormattedSource> for FormatResult {
|
impl From<FormattedSource> for FormatResult {
|
||||||
fn from(value: FormattedSource) -> Self {
|
fn from(value: FormattedSource) -> Self {
|
||||||
match value {
|
match value {
|
||||||
FormattedSource::Formatted {
|
FormattedSource::Formatted { .. } => FormatResult::Formatted,
|
||||||
formatted,
|
|
||||||
unformatted,
|
|
||||||
} => FormatResult::Formatted {
|
|
||||||
formatted,
|
|
||||||
unformatted,
|
|
||||||
},
|
|
||||||
FormattedSource::Unchanged => FormatResult::Unchanged,
|
FormattedSource::Unchanged => FormatResult::Unchanged,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -420,10 +401,7 @@ pub(crate) fn format_source(
|
||||||
if formatted.len() == unformatted.len() && formatted == *unformatted {
|
if formatted.len() == unformatted.len() && formatted == *unformatted {
|
||||||
Ok(FormattedSource::Unchanged)
|
Ok(FormattedSource::Unchanged)
|
||||||
} else {
|
} else {
|
||||||
Ok(FormattedSource::Formatted {
|
Ok(FormattedSource::Formatted(SourceKind::Python(formatted)))
|
||||||
formatted: SourceKind::Python(formatted),
|
|
||||||
unformatted: SourceKind::Python(unformatted.to_string()),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SourceKind::IpyNotebook(notebook) => {
|
SourceKind::IpyNotebook(notebook) => {
|
||||||
|
@ -508,10 +486,9 @@ pub(crate) fn format_source(
|
||||||
let mut formatted = notebook.clone();
|
let mut formatted = notebook.clone();
|
||||||
formatted.update(&source_map, output);
|
formatted.update(&source_map, output);
|
||||||
|
|
||||||
Ok(FormattedSource::Formatted {
|
Ok(FormattedSource::Formatted(SourceKind::IpyNotebook(
|
||||||
formatted: SourceKind::IpyNotebook(formatted),
|
formatted,
|
||||||
unformatted: SourceKind::IpyNotebook(notebook.clone()),
|
)))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -519,13 +496,10 @@ pub(crate) fn format_source(
|
||||||
/// The result of an individual formatting operation.
|
/// The result of an individual formatting operation.
|
||||||
#[derive(Debug, Clone, is_macro::Is)]
|
#[derive(Debug, Clone, is_macro::Is)]
|
||||||
pub(crate) enum FormatResult {
|
pub(crate) enum FormatResult {
|
||||||
/// The file was formatted.
|
/// The file was formatted and written back to disk.
|
||||||
Formatted {
|
Formatted,
|
||||||
unformatted: SourceKind,
|
|
||||||
formatted: SourceKind,
|
|
||||||
},
|
|
||||||
|
|
||||||
/// The file was formatted, [`SourceKind`] contains the formatted code
|
/// The file needs to be formatted, as the `formatted` and `unformatted` contents differ.
|
||||||
Diff {
|
Diff {
|
||||||
unformatted: SourceKind,
|
unformatted: SourceKind,
|
||||||
formatted: SourceKind,
|
formatted: SourceKind,
|
||||||
|
@ -562,7 +536,7 @@ impl<'a> FormatResults<'a> {
|
||||||
/// Returns `true` if any of the files require formatting.
|
/// Returns `true` if any of the files require formatting.
|
||||||
fn any_formatted(&self) -> bool {
|
fn any_formatted(&self) -> bool {
|
||||||
self.results.iter().any(|result| match result.result {
|
self.results.iter().any(|result| match result.result {
|
||||||
FormatResult::Formatted { .. } | FormatResult::Diff { .. } => true,
|
FormatResult::Formatted | FormatResult::Diff { .. } => true,
|
||||||
FormatResult::Unchanged | FormatResult::Skipped => false,
|
FormatResult::Unchanged | FormatResult::Skipped => false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -597,7 +571,7 @@ impl<'a> FormatResults<'a> {
|
||||||
.results
|
.results
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|result| {
|
.filter_map(|result| {
|
||||||
if result.result.is_formatted() {
|
if result.result.is_diff() {
|
||||||
Some(result.path.as_path())
|
Some(result.path.as_path())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -710,7 +684,7 @@ impl<'a> FormatResults<'a> {
|
||||||
let mut unchanged = 0u32;
|
let mut unchanged = 0u32;
|
||||||
for result in self.results {
|
for result in self.results {
|
||||||
match &result.result {
|
match &result.result {
|
||||||
FormatResult::Formatted { .. } => {
|
FormatResult::Formatted => {
|
||||||
changed += 1;
|
changed += 1;
|
||||||
}
|
}
|
||||||
FormatResult::Unchanged => unchanged += 1,
|
FormatResult::Unchanged => unchanged += 1,
|
||||||
|
@ -774,7 +748,7 @@ impl<'a> FormatResults<'a> {
|
||||||
self.results
|
self.results
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|result| {
|
.filter_map(|result| {
|
||||||
let FormatResult::Formatted {
|
let FormatResult::Diff {
|
||||||
unformatted,
|
unformatted,
|
||||||
formatted,
|
formatted,
|
||||||
} = &result.result
|
} = &result.result
|
||||||
|
|
|
@ -109,7 +109,7 @@ fn format_source_code(
|
||||||
let formatted = format_source(&source_kind, source_type, path, settings, range)?;
|
let formatted = format_source(&source_kind, source_type, path, settings, range)?;
|
||||||
|
|
||||||
match &formatted {
|
match &formatted {
|
||||||
FormattedSource::Formatted { formatted, .. } => match mode {
|
FormattedSource::Formatted(formatted) => match mode {
|
||||||
FormatMode::Write => {
|
FormatMode::Write => {
|
||||||
let mut writer = stdout().lock();
|
let mut writer = stdout().lock();
|
||||||
formatted
|
formatted
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue