From 56a157e5b01b62afd5c50a014ed15b379de8dfd6 Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Sat, 14 Nov 2026 16:22:22 +0000 Subject: [PATCH] language: Reset unused-code state when seeking buffer chunks (#66977) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary `BufferChunks::seek` rebuilds the diagnostic endpoints and zeroes the severity depths, but it left `unnecessary_depth` alone. After a seek from inside an unnecessary-code diagnostic, every later chunk reported `is_unnecessary`. `initialize_diagnostic_endpoints` now zeroes it with the other depths. `FoldChunks` seeks when it skips a fold, so text after a fold that starts in unused code could render faded. That symptom is inferred from the code; only the iterator is tested. `seek` also called `initialize_diagnostic_endpoints` twice when the iterator used cached highlights. The first call is removed. A standalone release binary linked against `language` timed `seek` on an iterator with cached highlights. The buffer is 4,000 lines of Rust with a diagnostic on each of the first 2,000 lines. Each seek stays inside the iterator's current range so highlights are not re-fetched. Median time per seek: | Diagnostics in seek range | Before | After | |---|---|---| | 0 | 210 ns | 114 ns | | 1 | 0.77 µs | 0.40 µs | | 10 | 1.28 µs | 0.68 µs | | 100 | 6.7 µs | 3.4 µs | | 1,000 | 59 µs | 29 µs | ## Testing `cargo test -p language -- chunk diagnostic` passes. `test_chunks_seek_out_of_unnecessary_diagnostic` is new and fails without the fix. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - Fixed unused-code fading sometimes extending past the unused code --- crates/language/src/buffer.rs | 2 +- crates/language/src/buffer_tests.rs | 48 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index f6d2bf19fd7..544e344e087 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -5763,7 +5763,6 @@ impl<'a> BufferChunks<'a> { "Attempted to seek on a language-aware buffer iterator without associated buffer snapshot" ); } - self.initialize_diagnostic_endpoints(); } else if let Some(highlights) = self.highlights.as_mut() { if old_range.start <= self.range.start && old_range.end >= self.range.end { // Reuse existing highlights stack, as the new range is a subrange of the old one. @@ -5832,6 +5831,7 @@ impl<'a> BufferChunks<'a> { self.error_depth = 0; self.warning_depth = 0; self.information_depth = 0; + self.unnecessary_depth = 0; } } diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rs index 9c8ec32f9ac..e2757003fe0 100644 --- a/crates/language/src/buffer_tests.rs +++ b/crates/language/src/buffer_tests.rs @@ -5667,6 +5667,54 @@ fn test_chunk_highlights_across_row_chunk_seeks(cx: &mut TestAppContext) { ); } +#[gpui::test] +fn test_chunks_seek_out_of_unnecessary_diagnostic(cx: &mut TestAppContext) { + cx.update(|cx| init_settings(cx, |_| {})); + + let first_row = "let unused = 1;\n"; + let second_row = "let used = 2;\n"; + let buffer = cx.new(|cx| { + let mut buffer = Buffer::local(format!("{first_row}{second_row}"), cx); + let diagnostics = DiagnosticSet::new( + [DiagnosticEntry::new( + PointUtf16::new(0, 4)..PointUtf16::new(0, 10), + Diagnostic { + is_unnecessary: true, + ..Default::default() + }, + )], + &buffer, + ); + buffer.update_diagnostics(LanguageServerId(0), diagnostics, cx); + buffer + }); + let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot()); + + let mut chunks = snapshot.chunks( + 0..snapshot.len(), + LanguageAwareStyling { + tree_sitter: false, + diagnostics: true, + }, + ); + let unnecessary_chunk = chunks.find(|chunk| chunk.is_unnecessary); + assert_eq!( + unnecessary_chunk.map(|chunk| chunk.text), + Some("unused"), + "the diagnostic range must be reported as unnecessary before seeking" + ); + + chunks.seek(first_row.len()..snapshot.len()); + let chunks_after_seek = chunks + .map(|chunk| (chunk.text, chunk.is_unnecessary)) + .collect::>(); + assert_eq!( + chunks_after_seek, + vec![(second_row, false)], + "seeking away from an unnecessary diagnostic must not leave later text marked unnecessary" + ); +} + #[gpui::test] fn test_oversized_chunks_bypass_the_highlight_cache(cx: &mut TestAppContext) { if std::env::var_os("ZED_DISABLE_HIGHLIGHT_CACHE").is_some() {