From 596dfdcf32d85d4d8d17a8887b4d0979ec7c5e14 Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Sun, 15 Nov 2026 09:13:15 +0000 Subject: [PATCH] language: Reset max start depth on pooled query cursors (#66983) ## Summary `QueryCursorHandle` returns its cursor to a shared pool on drop. It reset the cursor's byte and point ranges but not `max_start_depth`, and tree-sitter keeps that setting across executions. Only `SyntaxMapMatches` sets a depth, and it sets one on every query. The other pool users never set it: highlight captures, injection queries, `SyntaxLayer::override_id`, and `with_query_cursor`. After a depth-limited matches query, the next of those to take the same cursor matched only nodes within that depth. `Drop` now clears it. The depth-limited queries are the text object queries behind Vim's method, section, and comment motions (`] m`, `] ]`, `] /`), with depths of 3 to 6. Missing highlights after those motions are inferred from the code; only the syntax map is tested. ## Testing `cargo test -p language --lib` passes. `test_max_start_depth_does_not_carry_over_to_later_queries` 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 syntax highlighting sometimes going missing after Vim method, section, and comment motions --- crates/language/src/syntax_map.rs | 1 + .../src/syntax_map/syntax_map_tests.rs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/crates/language/src/syntax_map.rs b/crates/language/src/syntax_map.rs index 955ae3ead8a..41232cf250f 100644 --- a/crates/language/src/syntax_map.rs +++ b/crates/language/src/syntax_map.rs @@ -2274,6 +2274,7 @@ impl Drop for QueryCursorHandle { cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point()); cursor.set_containing_byte_range(0..usize::MAX); cursor.set_containing_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point()); + cursor.set_max_start_depth(None); QUERY_CURSORS.lock().push(cursor) } } diff --git a/crates/language/src/syntax_map/syntax_map_tests.rs b/crates/language/src/syntax_map/syntax_map_tests.rs index d0518b71bb2..d51682c7b7d 100644 --- a/crates/language/src/syntax_map/syntax_map_tests.rs +++ b/crates/language/src/syntax_map/syntax_map_tests.rs @@ -1385,6 +1385,34 @@ fn test_injection_grouped_by_host(cx: &mut App) { } } +#[gpui::test] +fn test_max_start_depth_does_not_carry_over_to_later_queries(cx: &mut App) { + fn highlights_query(grammar: &Grammar) -> Option<&Query> { + grammar + .highlights_config + .as_ref() + .map(|config| &config.query) + } + + let (buffer, syntax_map) = test_edit_sequence("Rust", &["fn a() { b(c); }"], cx); + let capture_count = || { + syntax_map + .captures(0..buffer.len(), &buffer, highlights_query) + .count() + }; + + let expected_capture_count = capture_count(); + assert!(expected_capture_count > 0); + + drop(syntax_map.matches_with_options( + 0..buffer.len(), + &buffer, + TreeSitterOptions::max_start_depth(1), + highlights_query, + )); + assert_eq!(capture_count(), expected_capture_count); +} + #[gpui::test] fn test_syntax_map_languages_loading_with_erb(cx: &mut App) { let text = r#"