Start work on limiting the range where buffer queries execute

Max Brunsfeld created

Change summary

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(-)

Detailed changes

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"

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" }

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<usize>,
     ) -> impl Iterator<Item = BracketMatch> + '_ {
-        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()

crates/language/src/syntax_map.rs 🔗

@@ -1083,12 +1083,15 @@ impl<'a> SyntaxMapCaptures<'a> {
 
 #[derive(Default)]
 pub struct TreeSitterOptions {
-    max_start_depth: Option<u32>,
+    pub max_start_depth: Option<u32>,
+    pub max_distance_from_inclusion_byte_range: Option<usize>,
 }
+
 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<usize>)> = 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)
     }
 }