diff --git a/crates/ruff_cli/src/cache.rs b/crates/ruff_cli/src/cache.rs index 6d58a2a21b220..c9794a9c4b1ca 100644 --- a/crates/ruff_cli/src/cache.rs +++ b/crates/ruff_cli/src/cache.rs @@ -571,7 +571,6 @@ mod tests { use ruff_cache::CACHE_DIR_NAME; use ruff_linter::settings::flags; use ruff_linter::settings::types::UnsafeFixes; - use ruff_linter::source_kind::SourceKind; use ruff_python_ast::PySourceType; use ruff_workspace::Settings; @@ -1061,7 +1060,6 @@ mod tests { format_path( &file_path, &self.settings.formatter, - &SourceKind::Python(std::fs::read_to_string(&file_path).unwrap()), PySourceType::Python, FormatMode::Write, Some(cache), diff --git a/crates/ruff_cli/src/commands/format.rs b/crates/ruff_cli/src/commands/format.rs index e9ff23ee4f45b..51898ffe4e555 100644 --- a/crates/ruff_cli/src/commands/format.rs +++ b/crates/ruff_cli/src/commands/format.rs @@ -124,18 +124,6 @@ pub(crate) fn format( return None; } - // Extract the sources from the file. - let unformatted = match SourceKind::from_path(path, source_type) { - Ok(Some(source_kind)) => source_kind, - Ok(None) => return None, - Err(err) => { - return Some(Err(FormatCommandError::Read( - Some(path.to_path_buf()), - err, - ))); - } - }; - let package = path .parent() .and_then(|parent| package_roots.get(parent).copied()) @@ -148,7 +136,6 @@ pub(crate) fn format( format_path( path, &resolved_settings.formatter, - &unformatted, source_type, mode, cache, @@ -156,7 +143,6 @@ pub(crate) fn format( }) { Ok(inner) => inner.map(|result| FormatPathResult { path: resolved_file.path().to_path_buf(), - unformatted, result, }), Err(error) => Err(FormatCommandError::Panic( @@ -253,7 +239,6 @@ pub(crate) fn format( pub(crate) fn format_path( path: &Path, settings: &FormatterSettings, - unformatted: &SourceKind, source_type: PySourceType, mode: FormatMode, cache: Option<&Cache>, @@ -270,8 +255,18 @@ pub(crate) fn format_path( } } + // Extract the sources from the file. + let unformatted = match SourceKind::from_path(path, source_type) { + Ok(Some(source_kind)) => source_kind, + // Non Python Jupyter notebook + Ok(None) => return Ok(FormatResult::Skipped), + Err(err) => { + return Err(FormatCommandError::Read(Some(path.to_path_buf()), err)); + } + }; + // Format the source. - let format_result = match format_source(unformatted, source_type, Some(path), settings)? { + let format_result = match format_source(&unformatted, source_type, Some(path), settings)? { FormattedSource::Formatted(formatted) => match mode { FormatMode::Write => { let mut writer = File::create(path).map_err(|err| { @@ -293,7 +288,10 @@ pub(crate) fn format_path( FormatResult::Formatted } FormatMode::Check => FormatResult::Formatted, - FormatMode::Diff => FormatResult::Diff(formatted), + FormatMode::Diff => FormatResult::Diff { + unformatted, + formatted, + }, }, FormattedSource::Unchanged => { if let Some(cache) = cache { @@ -425,16 +423,21 @@ pub(crate) enum FormatResult { /// The file was formatted. Formatted, /// The file was formatted, [`SourceKind`] contains the formatted code - Diff(SourceKind), + Diff { + unformatted: SourceKind, + formatted: SourceKind, + }, /// The file was unchanged, as the formatted contents matched the existing contents. Unchanged, + + /// Skipped formatting because its an unformatted file format + Skipped, } /// The coupling of a [`FormatResult`] with the path of the file that was analyzed. #[derive(Debug)] struct FormatPathResult { path: PathBuf, - unformatted: SourceKind, result: FormatResult, } @@ -456,15 +459,19 @@ impl<'a> FormatResults<'a> { fn any_formatted(&self) -> bool { self.results.iter().any(|result| match result.result { FormatResult::Formatted | FormatResult::Diff { .. } => true, - FormatResult::Unchanged => false, + FormatResult::Unchanged | FormatResult::Skipped => false, }) } fn write_diff(&self, f: &mut impl Write) -> io::Result<()> { for result in self.results { - if let FormatResult::Diff(formatted) = &result.result { + if let FormatResult::Diff { + unformatted, + formatted, + } = &result.result + { let text_diff = - TextDiff::from_lines(result.unformatted.source_code(), formatted.source_code()); + TextDiff::from_lines(unformatted.source_code(), formatted.source_code()); let mut unified_diff = text_diff.unified_diff(); unified_diff.header( &fs::relativize_path(&result.path), @@ -495,9 +502,10 @@ impl<'a> FormatResults<'a> { changed += 1; } FormatResult::Unchanged => unchanged += 1, - FormatResult::Diff(_) => { + FormatResult::Diff { .. } => { changed += 1; } + FormatResult::Skipped => {} } }