Pass Summary::Context to Item::summarize (#18510)

We are going to use this in the multi-buffer to produce a summary for an
`Excerpt` that contains a `Range<Anchor>`.

Release Notes:

- N/A

Co-authored-by: Nathan <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2024-09-29 10:30:48 -06:00 committed by GitHub
parent 8aeab4800c
commit 84ce81caf1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 31 additions and 31 deletions

View File

@ -808,7 +808,7 @@ pub fn mentions_to_proto(mentions: &[(Range<usize>, UserId)]) -> Vec<proto::Chat
impl sum_tree::Item for ChannelMessage { impl sum_tree::Item for ChannelMessage {
type Summary = ChannelMessageSummary; type Summary = ChannelMessageSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
ChannelMessageSummary { ChannelMessageSummary {
max_id: self.id, max_id: self.id,
count: 1, count: 1,

View File

@ -1360,7 +1360,7 @@ impl<'a> Iterator for BlockBufferRows<'a> {
impl sum_tree::Item for Transform { impl sum_tree::Item for Transform {
type Summary = TransformSummary; type Summary = TransformSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
self.summary.clone() self.summary.clone()
} }
} }

View File

@ -291,7 +291,7 @@ impl sum_tree::Summary for ItemSummary {
impl sum_tree::Item for CreaseItem { impl sum_tree::Item for CreaseItem {
type Summary = ItemSummary; type Summary = ItemSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &MultiBufferSnapshot) -> Self::Summary {
ItemSummary { ItemSummary {
range: self.crease.range.clone(), range: self.crease.range.clone(),
} }

View File

@ -944,7 +944,7 @@ struct TransformSummary {
impl sum_tree::Item for Transform { impl sum_tree::Item for Transform {
type Summary = TransformSummary; type Summary = TransformSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
self.summary.clone() self.summary.clone()
} }
} }
@ -1004,7 +1004,7 @@ impl Default for FoldRange {
impl sum_tree::Item for Fold { impl sum_tree::Item for Fold {
type Summary = FoldSummary; type Summary = FoldSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &MultiBufferSnapshot) -> Self::Summary {
FoldSummary { FoldSummary {
start: self.range.start, start: self.range.start,
end: self.range.end, end: self.range.end,

View File

@ -74,7 +74,7 @@ impl Inlay {
impl sum_tree::Item for Transform { impl sum_tree::Item for Transform {
type Summary = TransformSummary; type Summary = TransformSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
match self { match self {
Transform::Isomorphic(summary) => TransformSummary { Transform::Isomorphic(summary) => TransformSummary {
input: summary.clone(), input: summary.clone(),

View File

@ -917,7 +917,7 @@ impl Transform {
impl sum_tree::Item for Transform { impl sum_tree::Item for Transform {
type Summary = TransformSummary; type Summary = TransformSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
self.summary.clone() self.summary.clone()
} }
} }

View File

@ -29,7 +29,7 @@ pub struct GitBlameEntrySummary {
impl sum_tree::Item for GitBlameEntry { impl sum_tree::Item for GitBlameEntry {
type Summary = GitBlameEntrySummary; type Summary = GitBlameEntrySummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
GitBlameEntrySummary { rows: self.rows } GitBlameEntrySummary { rows: self.rows }
} }
} }

View File

@ -34,7 +34,7 @@ struct InternalDiffHunk {
impl sum_tree::Item for InternalDiffHunk { impl sum_tree::Item for InternalDiffHunk {
type Summary = DiffHunkSummary; type Summary = DiffHunkSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &text::BufferSnapshot) -> Self::Summary {
DiffHunkSummary { DiffHunkSummary {
buffer_range: self.buffer_range.clone(), buffer_range: self.buffer_range.clone(),
} }

View File

@ -858,7 +858,7 @@ impl Styled for List {
impl sum_tree::Item for ListItem { impl sum_tree::Item for ListItem {
type Summary = ListItemSummary; type Summary = ListItemSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _: &()) -> Self::Summary {
match self { match self {
ListItem::Unmeasured { focus_handle } => ListItemSummary { ListItem::Unmeasured { focus_handle } => ListItemSummary {
count: 1, count: 1,

View File

@ -224,7 +224,7 @@ impl DiagnosticSet {
impl sum_tree::Item for DiagnosticEntry<Anchor> { impl sum_tree::Item for DiagnosticEntry<Anchor> {
type Summary = Summary; type Summary = Summary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &text::BufferSnapshot) -> Self::Summary {
Summary { Summary {
start: self.range.start, start: self.range.start,
end: self.range.end, end: self.range.end,

View File

@ -1739,7 +1739,7 @@ impl<'a> SeekTarget<'a, SyntaxLayerSummary, SyntaxLayerSummary>
impl sum_tree::Item for SyntaxLayerEntry { impl sum_tree::Item for SyntaxLayerEntry {
type Summary = SyntaxLayerSummary; type Summary = SyntaxLayerSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &BufferSnapshot) -> Self::Summary {
SyntaxLayerSummary { SyntaxLayerSummary {
min_depth: self.depth, min_depth: self.depth,
max_depth: self.depth, max_depth: self.depth,

View File

@ -4596,7 +4596,7 @@ impl fmt::Debug for Excerpt {
impl sum_tree::Item for Excerpt { impl sum_tree::Item for Excerpt {
type Summary = ExcerptSummary; type Summary = ExcerptSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
let mut text = self.text_summary.clone(); let mut text = self.text_summary.clone();
if self.has_trailing_newline { if self.has_trailing_newline {
text += TextSummary::from("\n"); text += TextSummary::from("\n");
@ -4613,7 +4613,7 @@ impl sum_tree::Item for Excerpt {
impl sum_tree::Item for ExcerptIdMapping { impl sum_tree::Item for ExcerptIdMapping {
type Summary = ExcerptId; type Summary = ExcerptId;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
self.id self.id
} }
} }

View File

@ -455,7 +455,7 @@ impl EventEmitter<NotificationEvent> for NotificationStore {}
impl sum_tree::Item for NotificationEntry { impl sum_tree::Item for NotificationEntry {
type Summary = NotificationSummary; type Summary = NotificationSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
NotificationSummary { NotificationSummary {
max_id: self.id, max_id: self.id,
count: 1, count: 1,

View File

@ -1159,7 +1159,7 @@ impl Chunk {
impl sum_tree::Item for Chunk { impl sum_tree::Item for Chunk {
type Summary = ChunkSummary; type Summary = ChunkSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
ChunkSummary::from(self.0.as_str()) ChunkSummary::from(self.0.as_str())
} }
} }

View File

@ -20,7 +20,7 @@ pub const TREE_BASE: usize = 6;
pub trait Item: Clone { pub trait Item: Clone {
type Summary: Summary; type Summary: Summary;
fn summary(&self) -> Self::Summary; fn summary(&self, cx: &<Self::Summary as Summary>::Context) -> Self::Summary;
} }
/// An [`Item`] whose summary has a specific key that can be used to identify it /// An [`Item`] whose summary has a specific key that can be used to identify it
@ -211,7 +211,7 @@ impl<T: Item> SumTree<T> {
while iter.peek().is_some() { while iter.peek().is_some() {
let items: ArrayVec<T, { 2 * TREE_BASE }> = iter.by_ref().take(2 * TREE_BASE).collect(); let items: ArrayVec<T, { 2 * TREE_BASE }> = iter.by_ref().take(2 * TREE_BASE).collect();
let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> = let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> =
items.iter().map(|item| item.summary()).collect(); items.iter().map(|item| item.summary(cx)).collect();
let mut summary = item_summaries[0].clone(); let mut summary = item_summaries[0].clone();
for item_summary in &item_summaries[1..] { for item_summary in &item_summaries[1..] {
@ -281,7 +281,7 @@ impl<T: Item> SumTree<T> {
.map(|items| { .map(|items| {
let items: ArrayVec<T, { 2 * TREE_BASE }> = items.into_iter().collect(); let items: ArrayVec<T, { 2 * TREE_BASE }> = items.into_iter().collect();
let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> = let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> =
items.iter().map(|item| item.summary()).collect(); items.iter().map(|item| item.summary(cx)).collect();
let mut summary = item_summaries[0].clone(); let mut summary = item_summaries[0].clone();
for item_summary in &item_summaries[1..] { for item_summary in &item_summaries[1..] {
<T::Summary as Summary>::add_summary(&mut summary, item_summary, cx); <T::Summary as Summary>::add_summary(&mut summary, item_summary, cx);
@ -405,7 +405,7 @@ impl<T: Item> SumTree<T> {
if let Some((item, item_summary)) = items.last_mut().zip(item_summaries.last_mut()) if let Some((item, item_summary)) = items.last_mut().zip(item_summaries.last_mut())
{ {
(f)(item); (f)(item);
*item_summary = item.summary(); *item_summary = item.summary(cx);
*summary = sum(item_summaries.iter(), cx); *summary = sum(item_summaries.iter(), cx);
Some(summary.clone()) Some(summary.clone())
} else { } else {
@ -461,7 +461,7 @@ impl<T: Item> SumTree<T> {
} }
pub fn push(&mut self, item: T, cx: &<T::Summary as Summary>::Context) { pub fn push(&mut self, item: T, cx: &<T::Summary as Summary>::Context) {
let summary = item.summary(); let summary = item.summary(cx);
self.append( self.append(
SumTree(Arc::new(Node::Leaf { SumTree(Arc::new(Node::Leaf {
summary: summary.clone(), summary: summary.clone(),
@ -1352,7 +1352,7 @@ mod tests {
impl Item for u8 { impl Item for u8 {
type Summary = IntegersSummary; type Summary = IntegersSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
IntegersSummary { IntegersSummary {
count: 1, count: 1,
sum: *self as usize, sum: *self as usize,

View File

@ -224,7 +224,7 @@ where
{ {
type Summary = MapKey<K>; type Summary = MapKey<K>;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
self.key() self.key()
} }
} }

View File

@ -69,7 +69,7 @@ impl Default for Locator {
impl sum_tree::Item for Locator { impl sum_tree::Item for Locator {
type Summary = Locator; type Summary = Locator;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
self.clone() self.clone()
} }
} }

View File

@ -107,7 +107,7 @@ impl<'a> Dimension<'a, OperationSummary> for OperationKey {
impl<T: Operation> Item for OperationItem<T> { impl<T: Operation> Item for OperationItem<T> {
type Summary = OperationSummary; type Summary = OperationSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
OperationSummary { OperationSummary {
key: OperationKey::new(self.0.lamport_timestamp()), key: OperationKey::new(self.0.lamport_timestamp()),
len: 1, len: 1,

View File

@ -2617,7 +2617,7 @@ impl Fragment {
impl sum_tree::Item for Fragment { impl sum_tree::Item for Fragment {
type Summary = FragmentSummary; type Summary = FragmentSummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &Option<clock::Global>) -> Self::Summary {
let mut max_version = clock::Global::new(); let mut max_version = clock::Global::new();
max_version.observe(self.timestamp); max_version.observe(self.timestamp);
for deletion in &self.deletions { for deletion in &self.deletions {
@ -2688,7 +2688,7 @@ impl Default for FragmentSummary {
impl sum_tree::Item for InsertionFragment { impl sum_tree::Item for InsertionFragment {
type Summary = InsertionFragmentKey; type Summary = InsertionFragmentKey;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
InsertionFragmentKey { InsertionFragmentKey {
timestamp: self.timestamp, timestamp: self.timestamp,
split_offset: self.split_offset, split_offset: self.split_offset,
@ -2700,7 +2700,7 @@ impl sum_tree::KeyedItem for InsertionFragment {
type Key = InsertionFragmentKey; type Key = InsertionFragmentKey;
fn key(&self) -> Self::Key { fn key(&self) -> Self::Key {
sum_tree::Item::summary(self) sum_tree::Item::summary(self, &())
} }
} }

View File

@ -11,7 +11,7 @@ struct UndoMapEntry {
impl sum_tree::Item for UndoMapEntry { impl sum_tree::Item for UndoMapEntry {
type Summary = UndoMapKey; type Summary = UndoMapKey;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
self.key self.key
} }
} }

View File

@ -3339,7 +3339,7 @@ impl EntryKind {
impl sum_tree::Item for Entry { impl sum_tree::Item for Entry {
type Summary = EntrySummary; type Summary = EntrySummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
let non_ignored_count = if self.is_ignored || self.is_external { let non_ignored_count = if self.is_ignored || self.is_external {
0 0
} else { } else {
@ -3434,7 +3434,7 @@ struct PathEntry {
impl sum_tree::Item for PathEntry { impl sum_tree::Item for PathEntry {
type Summary = PathEntrySummary; type Summary = PathEntrySummary;
fn summary(&self) -> Self::Summary { fn summary(&self, _cx: &()) -> Self::Summary {
PathEntrySummary { max_id: self.id } PathEntrySummary { max_id: self.id }
} }
} }