From b8c3167a9de7dca14f4b3a2943dcfadd0ec5fe7a Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 8 May 2026 16:18:44 +0200 Subject: [PATCH] diagnostics: Always expand at least `multibuffer_context_lines` per diagnostic (#56172) Otherwise we can sometimes end up with single line excerpts which looks very off Release Notes: - Improved the minimum size of diagnostics pane excerpts --- Cargo.lock | 1 + crates/diagnostics/Cargo.toml | 1 + crates/diagnostics/src/diagnostics.rs | 27 +++++++++++++++------------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f4626a83d5c6b1e5575145efdc540f07ebd2f06c..ea52978db69d453ee8dc17ecf59af47c1780e154 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4941,6 +4941,7 @@ dependencies = [ "component", "ctor", "editor", + "futures-lite 1.13.0", "gpui", "indoc", "itertools 0.14.0", diff --git a/crates/diagnostics/Cargo.toml b/crates/diagnostics/Cargo.toml index fe850303e83f2bac4629b2a9c7066bda933716de..0c96524ec03bf18c0a11f8ca444e72cb399da947 100644 --- a/crates/diagnostics/Cargo.toml +++ b/crates/diagnostics/Cargo.toml @@ -19,6 +19,7 @@ collections.workspace = true component.workspace = true ctor.workspace = true editor.workspace = true +futures-lite.workspace = true gpui.workspace = true indoc.workspace = true itertools.workspace = true diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 642d16b6e254763f2758b3998708915acb71da5d..de99274d86aa1a277ad6a902e9f5db4983837207 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -981,24 +981,26 @@ async fn context_range_for_entry( snapshot: BufferSnapshot, cx: &mut AsyncApp, ) -> Range { - let range = if let Some(rows) = heuristic_syntactic_expand( + let expanded_range = heuristic_syntactic_expand( range.clone(), DIAGNOSTIC_EXPANSION_ROW_LIMIT, snapshot.clone(), cx, ) - .await - .filter(|rows| rows.start() != rows.end()) - { - Range { - start: Point::new(*rows.start(), 0), - end: snapshot.clip_point(Point::new(*rows.end(), u32::MAX), Bias::Left), - } + .await; + let row_range = expanded_range.unwrap_or_else(|| range.start.row..=range.end.row); + let row_count = row_range.end().saturating_sub(*row_range.start()) + 1; + let target_row_count = context.saturating_mul(2).saturating_add(1); + let row_range = if let Some(rows_to_add) = target_row_count.checked_sub(row_count) { + let rows_before = rows_to_add.div_ceil(2); + let rows_after = rows_to_add / 2; + row_range.start().saturating_sub(rows_before)..=row_range.end().saturating_add(rows_after) } else { - Range { - start: Point::new(range.start.row.saturating_sub(context), 0), - end: snapshot.clip_point(Point::new(range.end.row + context, u32::MAX), Bias::Left), - } + row_range + }; + let range = Range { + start: Point::new(*row_range.start(), 0), + end: snapshot.clip_point(Point::new(*row_range.end(), u32::MAX), Bias::Left), }; snapshot.anchor_after(range.start)..snapshot.anchor_before(range.end) } @@ -1133,6 +1135,7 @@ async fn heuristic_syntactic_expand( return None; }; node = parent; + futures_lite::future::yield_now().await; } }