Respect `--output-format` with `--watch` (#21097)

Summary
--

Fixes #19550

This PR copies our non-watch diagnostic rendering code into
`Printer::write_continuously` in preview mode, allowing it to use
whatever output format is passed in.

I initially marked this as also fixing #19552, but I guess that's not
true currently but will be true once this is stabilized and we can
remove the warning.

Test Plan
--

Existing tests, but I don't think we have any `watch` tests, so some
manual testing as well. The default with just `ruff check --watch` is
still `concise`, adding just `--preview` still gives the `full` output,
and then specifying any other output format works, with JSON as one
example:

<img width="695" height="719" alt="Screenshot 2025-10-27 at 9 21 41 AM"
src="https://github.com/user-attachments/assets/98957911-d216-4fc4-8b6c-22c56c963b3f"
/>
This commit is contained in:
Brent Westbrook 2025-10-27 12:04:55 -04:00 committed by GitHub
parent fffbe5a879
commit 96b60c11d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 12 deletions

View File

@ -9,9 +9,7 @@ use itertools::{Itertools, iterate};
use ruff_linter::linter::FixTable; use ruff_linter::linter::FixTable;
use serde::Serialize; use serde::Serialize;
use ruff_db::diagnostic::{ use ruff_db::diagnostic::{Diagnostic, DisplayDiagnosticConfig, SecondaryCode};
Diagnostic, DiagnosticFormat, DisplayDiagnosticConfig, DisplayDiagnostics, SecondaryCode,
};
use ruff_linter::fs::relativize_path; use ruff_linter::fs::relativize_path;
use ruff_linter::logging::LogLevel; use ruff_linter::logging::LogLevel;
use ruff_linter::message::{EmitterContext, render_diagnostics}; use ruff_linter::message::{EmitterContext, render_diagnostics};
@ -390,21 +388,18 @@ impl Printer {
let context = EmitterContext::new(&diagnostics.notebook_indexes); let context = EmitterContext::new(&diagnostics.notebook_indexes);
let format = if preview { let format = if preview {
DiagnosticFormat::Full self.format
} else { } else {
DiagnosticFormat::Concise OutputFormat::Concise
}; };
let config = DisplayDiagnosticConfig::default() let config = DisplayDiagnosticConfig::default()
.preview(preview)
.hide_severity(true) .hide_severity(true)
.color(!cfg!(test) && colored::control::SHOULD_COLORIZE.should_colorize()) .color(!cfg!(test) && colored::control::SHOULD_COLORIZE.should_colorize())
.with_show_fix_status(show_fix_status(self.fix_mode, fixables.as_ref())) .with_show_fix_status(show_fix_status(self.fix_mode, fixables.as_ref()))
.format(format) .with_fix_applicability(self.unsafe_fixes.required_applicability())
.with_fix_applicability(self.unsafe_fixes.required_applicability()); .show_fix_diff(preview);
write!( render_diagnostics(writer, format, config, &context, &diagnostics.inner)?;
writer,
"{}",
DisplayDiagnostics::new(&context, &config, &diagnostics.inner)
)?;
} }
writer.flush()?; writer.flush()?;