Replace Default trait bound with a zero function on Summary/Dimension (#17975)

This lets us provide a context when constructing the zero value. We need
it so we can require anchors to be associated with a buffer id, which
we're doing as part of simplifying the multibuffer API.

Release Notes:

- N/A

Co-authored-by: Nathan <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra
2024-09-17 19:43:59 -06:00
committed by GitHub
co-authored by Nathan
parent 4d074fc737
commit 2e72fd210a
28 changed files with 706 additions and 349 deletions
+27 -12
View File
@@ -18,13 +18,12 @@ use sum_tree::{Bias, SeekTarget, SumTree};
use text::{Anchor, BufferSnapshot, OffsetRangeExt, Point, Rope, ToOffset, ToPoint};
use tree_sitter::{Node, Query, QueryCapture, QueryCaptures, QueryCursor, QueryMatches, Tree};
#[derive(Default)]
pub struct SyntaxMap {
snapshot: SyntaxSnapshot,
language_registry: Option<Arc<LanguageRegistry>>,
}
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct SyntaxSnapshot {
layers: SumTree<SyntaxLayerEntry>,
parsed_version: clock::Global,
@@ -212,8 +211,11 @@ struct ByteChunks<'a>(text::Chunks<'a>);
pub(crate) struct QueryCursorHandle(Option<QueryCursor>);
impl SyntaxMap {
pub fn new() -> Self {
Self::default()
pub fn new(text: &BufferSnapshot) -> Self {
Self {
snapshot: SyntaxSnapshot::new(text),
language_registry: None,
}
}
pub fn set_language_registry(&mut self, registry: Arc<LanguageRegistry>) {
@@ -242,12 +244,21 @@ impl SyntaxMap {
self.snapshot = snapshot;
}
pub fn clear(&mut self) {
self.snapshot = SyntaxSnapshot::default();
pub fn clear(&mut self, text: &BufferSnapshot) {
self.snapshot = SyntaxSnapshot::new(text);
}
}
impl SyntaxSnapshot {
fn new(text: &BufferSnapshot) -> Self {
Self {
layers: SumTree::new(text),
parsed_version: clock::Global::default(),
interpolated_version: clock::Global::default(),
language_registry_version: 0,
}
}
pub fn is_empty(&self) -> bool {
self.layers.is_empty()
}
@@ -262,10 +273,10 @@ impl SyntaxSnapshot {
return;
}
let mut layers = SumTree::new();
let mut layers = SumTree::new(text);
let mut first_edit_ix_for_depth = 0;
let mut prev_depth = 0;
let mut cursor = self.layers.cursor::<SyntaxLayerSummary>();
let mut cursor = self.layers.cursor::<SyntaxLayerSummary>(text);
cursor.next(text);
'outer: loop {
@@ -388,7 +399,7 @@ impl SyntaxSnapshot {
let mut resolved_injection_ranges = Vec::new();
let mut cursor = self
.layers
.filter::<_, ()>(|summary| summary.contains_unknown_injections);
.filter::<_, ()>(text, |summary| summary.contains_unknown_injections);
cursor.next(text);
while let Some(layer) = cursor.item() {
let SyntaxLayerContent::Pending { language_name } = &layer.content else {
@@ -430,9 +441,9 @@ impl SyntaxSnapshot {
log::trace!("reparse. invalidated ranges:{:?}", invalidated_ranges);
let max_depth = self.layers.summary().max_depth;
let mut cursor = self.layers.cursor::<SyntaxLayerSummary>();
let mut cursor = self.layers.cursor::<SyntaxLayerSummary>(text);
cursor.next(text);
let mut layers = SumTree::new();
let mut layers = SumTree::new(text);
let mut changed_regions = ChangeRegionSet::default();
let mut queue = BinaryHeap::new();
@@ -823,7 +834,7 @@ impl SyntaxSnapshot {
let start = buffer.anchor_before(start_offset);
let end = buffer.anchor_after(end_offset);
let mut cursor = self.layers.filter::<_, ()>(move |summary| {
let mut cursor = self.layers.filter::<_, ()>(buffer, move |summary| {
if summary.max_depth > summary.min_depth {
true
} else {
@@ -1666,6 +1677,10 @@ impl Default for SyntaxLayerSummary {
impl sum_tree::Summary for SyntaxLayerSummary {
type Context = BufferSnapshot;
fn zero(_cx: &BufferSnapshot) -> Self {
Default::default()
}
fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
if other.max_depth > self.max_depth {
self.max_depth = other.max_depth;