ruff_db: add context configuration

Instead of hard-coding a specific context window,
it seemed prudent to make this configurable. That
makes it easier to test different context window
sizes as well.

I am not totally convinced that this is the right
place for this configuration. I could see the context
window size being a property of `Diagnostic` instead,
since we might want to change the context window
size based not just on some end user configuration,
but perhaps also the specific diagnostic.

But for now, I think it's fine for it to live here,
and all of the rendering logic doesn't care where
it lives. So it should be relatively easy to change
in the future.
This commit is contained in:
Andrew Gallant 2025-03-13 11:22:28 -04:00 committed by Andrew Gallant
parent 2bcd2b4147
commit ef9a825827

View file

@ -516,7 +516,7 @@ impl Severity {
}
/// Configuration for rendering diagnostics.
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug)]
pub struct DisplayDiagnosticConfig {
/// The format to use for diagnostic rendering.
///
@ -526,6 +526,15 @@ pub struct DisplayDiagnosticConfig {
///
/// Disabled by default.
color: bool,
/// The number of non-empty lines to show around each snippet.
///
/// NOTE: It seems like making this a property of rendering *could*
/// be wrong. In particular, I have a suspicion that we may want
/// more granular control over this, perhaps based on the kind of
/// diagnostic or even the snippet itself. But I chose to put this
/// here for now as the most "sensible" place for it to live until
/// we had more concrete use cases. ---AG
context: usize,
}
impl DisplayDiagnosticConfig {
@ -538,6 +547,24 @@ impl DisplayDiagnosticConfig {
pub fn color(self, yes: bool) -> DisplayDiagnosticConfig {
DisplayDiagnosticConfig { color: yes, ..self }
}
/// Set the number of contextual lines to show around each snippet.
pub fn context(self, lines: usize) -> DisplayDiagnosticConfig {
DisplayDiagnosticConfig {
context: lines,
..self
}
}
}
impl Default for DisplayDiagnosticConfig {
fn default() -> DisplayDiagnosticConfig {
DisplayDiagnosticConfig {
format: DiagnosticFormat::default(),
color: false,
context: 2,
}
}
}
/// The diagnostic output format.