From 22a7f027033c1d7abb618b3c27f9bbf2f0fb5468 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 2 Oct 2025 17:36:09 -0700 Subject: [PATCH] Start work on limiting the range where buffer queries execute --- Cargo.lock | 4 ---- Cargo.toml | 2 ++ crates/language/src/buffer.rs | 24 ++++++++++++++++++------ crates/language/src/syntax_map.rs | 16 +++++++++++++++- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 94e409bc7f39716f33039556da5c205b9945c811..e21b6281125edd29f33c631199febad794bc2121 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16908,8 +16908,6 @@ dependencies = [ [[package]] name = "tree-sitter" version = "0.25.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78f873475d258561b06f1c595d93308a7ed124d9977cb26b148c2084a4a3cc87" dependencies = [ "cc", "regex", @@ -17068,8 +17066,6 @@ dependencies = [ [[package]] name = "tree-sitter-language" version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4013970217383f67b18aef68f6fb2e8d409bc5755227092d32efb0422ba24b8" [[package]] name = "tree-sitter-md" diff --git a/Cargo.toml b/Cargo.toml index 9c64ef0464db0ad2d557c1e5d2e107fd3e6c4804..b1ea54400796bd518353d1d971f75746273e76f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -770,6 +770,8 @@ features = [ ] [patch.crates-io] +tree-sitter = { path = "../tree-sitter/lib" } +tree-sitter-language = { path = "../tree-sitter/lib/language" } notify = { git = "https://github.com/zed-industries/notify.git", rev = "bbb9ea5ae52b253e095737847e367c30653a2e96" } notify-types = { git = "https://github.com/zed-industries/notify.git", rev = "bbb9ea5ae52b253e095737847e367c30653a2e96" } windows-capture = { git = "https://github.com/zed-industries/windows-capture.git", rev = "f0d6c1b6691db75461b732f6d5ff56eed002eeb9" } diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index ecb7c9d0460b9821270ba909cf6a76b44f6217a5..b52596ce28412de3042cbaa691b6b2219199fb9e 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -3017,9 +3017,15 @@ impl BufferSnapshot { let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0); let end = Point::new(row_range.end, 0); let range = (start..end).to_offset(&self.text); - let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| { - Some(&grammar.indents_config.as_ref()?.query) - }); + let mut matches = self.syntax.matches_with_options( + range.clone(), + &self.text, + TreeSitterOptions { + max_distance_from_inclusion_byte_range: Some(10 * 1024), + max_start_depth: None, + }, + |grammar| Some(&grammar.indents_config.as_ref()?.query), + ); let indent_configs = matches .grammars() .iter() @@ -4052,9 +4058,15 @@ impl BufferSnapshot { &self, range: Range, ) -> impl Iterator + '_ { - let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| { - grammar.brackets_config.as_ref().map(|c| &c.query) - }); + let mut matches = self.syntax.matches_with_options( + range.clone(), + &self.text, + TreeSitterOptions { + max_distance_from_inclusion_byte_range: Some(10 * 1024), + max_start_depth: None, + }, + |grammar| grammar.brackets_config.as_ref().map(|c| &c.query), + ); let configs = matches .grammars() .iter() diff --git a/crates/language/src/syntax_map.rs b/crates/language/src/syntax_map.rs index 528b25d47193ebdbbf0dbe12cd18c66e31ad37d0..d010200f5eb4ad670ecb002c576a72673fad90e5 100644 --- a/crates/language/src/syntax_map.rs +++ b/crates/language/src/syntax_map.rs @@ -1083,12 +1083,15 @@ impl<'a> SyntaxMapCaptures<'a> { #[derive(Default)] pub struct TreeSitterOptions { - max_start_depth: Option, + pub max_start_depth: Option, + pub max_distance_from_inclusion_byte_range: Option, } + impl TreeSitterOptions { pub fn max_start_depth(max_start_depth: u32) -> Self { Self { max_start_depth: Some(max_start_depth), + max_distance_from_inclusion_byte_range: None, } } } @@ -1122,6 +1125,12 @@ impl<'a> SyntaxMapMatches<'a> { }; cursor.set_max_start_depth(options.max_start_depth); + if let Some(window) = options.max_distance_from_inclusion_byte_range { + let containing_range = + range.start.saturating_sub(window)..range.end.saturating_add(window); + cursor.set_containing_byte_range(containing_range); + } + cursor.set_byte_range(range.clone()); let matches = cursor.matches(query, layer.node(), TextProvider(text)); let grammar_index = result @@ -1616,6 +1625,9 @@ impl<'a> SyntaxLayer<'a> { let mut query_cursor = QueryCursorHandle::new(); query_cursor.set_byte_range(offset.saturating_sub(1)..offset.saturating_add(1)); + query_cursor.set_containing_byte_range( + offset.saturating_sub(10 * 1024)..offset.saturating_add(10 * 1024), + ); let mut smallest_match: Option<(u32, Range)> = None; let mut matches = query_cursor.matches(&config.query, self.node(), text); @@ -1902,6 +1914,8 @@ impl Drop for QueryCursorHandle { let mut cursor = self.0.take().unwrap(); cursor.set_byte_range(0..usize::MAX); 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()); QUERY_CURSORS.lock().push(cursor) } }