From ef9a82582732b91e6f6b65bf2d471250c205830b Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Thu, 13 Mar 2025 11:22:28 -0400 Subject: [PATCH] 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. --- crates/ruff_db/src/diagnostic/mod.rs | 29 +++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/ruff_db/src/diagnostic/mod.rs b/crates/ruff_db/src/diagnostic/mod.rs index b0f88df90a..11b70100c1 100644 --- a/crates/ruff_db/src/diagnostic/mod.rs +++ b/crates/ruff_db/src/diagnostic/mod.rs @@ -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.