Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Return a `Fold` struct when querying the `FoldMap`
Antonio Scandurra3ff8c78b5867
diff --git a/crates/editor2/src/display_map.rs b/crates/editor2/src/display_map.rsindex d88daaccc12..e64d5e301ca 100644--- a/crates/editor2/src/display_map.rs+++ b/crates/editor2/src/display_map.rs@@ -31,7 +31,7 @@ pub use block_map::{BlockDisposition, BlockId, BlockProperties, BlockStyle, RenderBlock, TransformBlock,};-pub use self::fold_map::FoldPoint;+pub use self::fold_map::{Fold, FoldPoint};pub use self::inlay_map::{Inlay, InlayOffset, InlayPoint};#[derive(Copy, Clone, Debug, PartialEq, Eq)]@@ -124,7 +124,7 @@ impl DisplayMap {self.fold(other.folds_in_range(0..other.buffer_snapshot.len())- .map(|fold| fold.to_offset(&other.buffer_snapshot)),+ .map(|fold| fold.range.to_offset(&other.buffer_snapshot)),cx,);}@@ -723,7 +723,7 @@ impl DisplaySnapshot {DisplayPoint(point)}- pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Range<Anchor>>+ pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Fold>whereT: ToOffset,{diff --git a/crates/editor2/src/display_map/fold_map.rs b/crates/editor2/src/display_map/fold_map.rsindex 88cd202b080..4dad2d52aeb 100644--- a/crates/editor2/src/display_map/fold_map.rs+++ b/crates/editor2/src/display_map/fold_map.rs@@ -3,15 +3,16 @@ use super::{Highlights,};use crate::{Anchor, AnchorRangeExt, MultiBufferSnapshot, ToOffset};-use gpui::{HighlightStyle, Hsla};+use gpui::{ElementId, HighlightStyle, Hsla};use language::{Chunk, Edit, Point, TextSummary};use std::{any::TypeId,cmp::{self, Ordering},iter,- ops::{Add, AddAssign, Range, Sub},+ ops::{Add, AddAssign, Deref, DerefMut, Range, Sub},};use sum_tree::{Bias, Cursor, FilterCursor, SumTree};+use util::post_inc;#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]pub struct FoldPoint(pub Point);@@ -90,12 +91,16 @@ impl<'a> FoldMapWriter<'a> {}// For now, ignore any ranges that span an excerpt boundary.- let fold = Fold(buffer.anchor_after(range.start)..buffer.anchor_before(range.end));- if fold.0.start.excerpt_id != fold.0.end.excerpt_id {+ let fold_range =+ FoldRange(buffer.anchor_after(range.start)..buffer.anchor_before(range.end));+ if fold_range.0.start.excerpt_id != fold_range.0.end.excerpt_id {continue;}- folds.push(fold);+ folds.push(Fold {+ id: FoldId(post_inc(&mut self.0.next_fold_id.0)),+ range: fold_range,+ });let inlay_range =snapshot.to_inlay_offset(range.start)..snapshot.to_inlay_offset(range.end);@@ -106,13 +111,13 @@ impl<'a> FoldMapWriter<'a> {}let buffer = &snapshot.buffer;- folds.sort_unstable_by(|a, b| sum_tree::SeekTarget::cmp(a, b, buffer));+ folds.sort_unstable_by(|a, b| sum_tree::SeekTarget::cmp(&a.range, &b.range, buffer));self.0.snapshot.folds = {let mut new_tree = SumTree::new();- let mut cursor = self.0.snapshot.folds.cursor::<Fold>();+ let mut cursor = self.0.snapshot.folds.cursor::<FoldRange>();for fold in folds {- new_tree.append(cursor.slice(&fold, Bias::Right, buffer), buffer);+ new_tree.append(cursor.slice(&fold.range, Bias::Right, buffer), buffer);new_tree.push(fold, buffer);}new_tree.append(cursor.suffix(buffer), buffer);@@ -138,7 +143,8 @@ impl<'a> FoldMapWriter<'a> {let mut folds_cursor =intersecting_folds(&snapshot, &self.0.snapshot.folds, range, inclusive);while let Some(fold) = folds_cursor.item() {- let offset_range = fold.0.start.to_offset(buffer)..fold.0.end.to_offset(buffer);+ let offset_range =+ fold.range.start.to_offset(buffer)..fold.range.end.to_offset(buffer);if offset_range.end > offset_range.start {let inlay_range = snapshot.to_inlay_offset(offset_range.start)..snapshot.to_inlay_offset(offset_range.end);@@ -175,6 +181,7 @@ impl<'a> FoldMapWriter<'a> {pub struct FoldMap {snapshot: FoldSnapshot,ellipses_color: Option<Hsla>,+ next_fold_id: FoldId,}impl FoldMap {@@ -197,6 +204,7 @@ impl FoldMap {ellipses_color: None,},ellipses_color: None,+ next_fold_id: FoldId::default(),};let snapshot = this.snapshot.clone();(this, snapshot)@@ -242,8 +250,8 @@ impl FoldMap {while let Some(fold) = folds.next() {if let Some(next_fold) = folds.peek() {let comparison = fold- .0- .cmp(&next_fold.0, &self.snapshot.inlay_snapshot.buffer);+ .range+ .cmp(&next_fold.range, &self.snapshot.inlay_snapshot.buffer);assert!(comparison.is_le());}}@@ -304,9 +312,9 @@ impl FoldMap {let anchor = inlay_snapshot.buffer.anchor_before(inlay_snapshot.to_buffer_offset(edit.new.start));- let mut folds_cursor = self.snapshot.folds.cursor::<Fold>();+ let mut folds_cursor = self.snapshot.folds.cursor::<FoldRange>();folds_cursor.seek(- &Fold(anchor..Anchor::max()),+ &FoldRange(anchor..Anchor::max()),Bias::Left,&inlay_snapshot.buffer,);@@ -315,8 +323,8 @@ impl FoldMap {let inlay_snapshot = &inlay_snapshot;move || {let item = folds_cursor.item().map(|f| {- let buffer_start = f.0.start.to_offset(&inlay_snapshot.buffer);- let buffer_end = f.0.end.to_offset(&inlay_snapshot.buffer);+ let buffer_start = f.range.start.to_offset(&inlay_snapshot.buffer);+ let buffer_end = f.range.end.to_offset(&inlay_snapshot.buffer);inlay_snapshot.to_inlay_offset(buffer_start)..inlay_snapshot.to_inlay_offset(buffer_end)});@@ -596,13 +604,13 @@ impl FoldSnapshot {self.transforms.summary().output.longest_row}- pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Range<Anchor>>+ pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Fold>whereT: ToOffset,{let mut folds = intersecting_folds(&self.inlay_snapshot, &self.folds, range, false);iter::from_fn(move || {- let item = folds.item().map(|f| &f.0);+ let item = folds.item();folds.next(&self.inlay_snapshot.buffer);item})@@ -830,10 +838,39 @@ impl sum_tree::Summary for TransformSummary {}}-#[derive(Clone, Debug)]-struct Fold(Range<Anchor>);+#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]+pub struct FoldId(usize);++impl Into<ElementId> for FoldId {+ fn into(self) -> ElementId {+ ElementId::Integer(self.0)+ }+}++#[derive(Clone, Debug, Eq, PartialEq)]+pub struct Fold {+ pub id: FoldId,+ pub range: FoldRange,+}++#[derive(Clone, Debug, Eq, PartialEq)]+pub struct FoldRange(Range<Anchor>);++impl Deref for FoldRange {+ type Target = Range<Anchor>;++ fn deref(&self) -> &Self::Target {+ &self.0+ }+}++impl DerefMut for FoldRange {+ fn deref_mut(&mut self) -> &mut Self::Target {+ &mut self.0+ }+}-impl Default for Fold {+impl Default for FoldRange {fn default() -> Self {Self(Anchor::min()..Anchor::max())}@@ -844,17 +881,17 @@ impl sum_tree::Item for Fold {fn summary(&self) -> Self::Summary {FoldSummary {- start: self.0.start.clone(),- end: self.0.end.clone(),- min_start: self.0.start.clone(),- max_end: self.0.end.clone(),+ start: self.range.start.clone(),+ end: self.range.end.clone(),+ min_start: self.range.start.clone(),+ max_end: self.range.end.clone(),count: 1,}}}#[derive(Clone, Debug)]-struct FoldSummary {+pub struct FoldSummary {start: Anchor,end: Anchor,min_start: Anchor,@@ -900,14 +937,14 @@ impl sum_tree::Summary for FoldSummary {}}-impl<'a> sum_tree::Dimension<'a, FoldSummary> for Fold {+impl<'a> sum_tree::Dimension<'a, FoldSummary> for FoldRange {fn add_summary(&mut self, summary: &'a FoldSummary, _: &MultiBufferSnapshot) {self.0.start = summary.start.clone();self.0.end = summary.end.clone();}}-impl<'a> sum_tree::SeekTarget<'a, FoldSummary, Fold> for Fold {+impl<'a> sum_tree::SeekTarget<'a, FoldSummary, FoldRange> for FoldRange {fn cmp(&self, other: &Self, buffer: &MultiBufferSnapshot) -> Ordering {self.0.cmp(&other.0, buffer)}@@ -1321,7 +1358,10 @@ mod tests {let (snapshot, _) = map.read(inlay_snapshot.clone(), vec![]);let fold_ranges = snapshot.folds_in_range(Point::new(1, 0)..Point::new(1, 3))- .map(|fold| fold.start.to_point(&buffer_snapshot)..fold.end.to_point(&buffer_snapshot))+ .map(|fold| {+ fold.range.start.to_point(&buffer_snapshot)+ ..fold.range.end.to_point(&buffer_snapshot)+ }).collect::<Vec<_>>();assert_eq!(fold_ranges,@@ -1553,10 +1593,9 @@ mod tests {.filter(|fold| {let start = buffer_snapshot.anchor_before(start);let end = buffer_snapshot.anchor_after(end);- start.cmp(&fold.0.end, &buffer_snapshot) == Ordering::Less- && end.cmp(&fold.0.start, &buffer_snapshot) == Ordering::Greater+ start.cmp(&fold.range.end, &buffer_snapshot) == Ordering::Less+ && end.cmp(&fold.range.start, &buffer_snapshot) == Ordering::Greater})- .map(|fold| fold.0).collect::<Vec<_>>();assert_eq!(@@ -1639,10 +1678,10 @@ mod tests {let buffer = &inlay_snapshot.buffer;let mut folds = self.snapshot.folds.items(buffer);// Ensure sorting doesn't change how folds get merged and displayed.- folds.sort_by(|a, b| a.0.cmp(&b.0, buffer));+ folds.sort_by(|a, b| a.range.cmp(&b.range, buffer));let mut fold_ranges = folds.iter()- .map(|fold| fold.0.start.to_offset(buffer)..fold.0.end.to_offset(buffer))+ .map(|fold| fold.range.start.to_offset(buffer)..fold.range.end.to_offset(buffer)).peekable();let mut merged_ranges = Vec::new();diff --git a/crates/editor2/src/editor.rs b/crates/editor2/src/editor.rsindex d2ba9567f04..41757b4dc27 100644--- a/crates/editor2/src/editor.rs+++ b/crates/editor2/src/editor.rs@@ -5303,8 +5303,8 @@ impl Editor {buffer.anchor_before(range_to_move.start)..buffer.anchor_after(range_to_move.end),) {- let mut start = fold.start.to_point(&buffer);- let mut end = fold.end.to_point(&buffer);+ let mut start = fold.range.start.to_point(&buffer);+ let mut end = fold.range.end.to_point(&buffer);start.row -= row_delta;end.row -= row_delta;refold_ranges.push(start..end);@@ -5394,8 +5394,8 @@ impl Editor {buffer.anchor_before(range_to_move.start)..buffer.anchor_after(range_to_move.end),) {- let mut start = fold.start.to_point(&buffer);- let mut end = fold.end.to_point(&buffer);+ let mut start = fold.range.start.to_point(&buffer);+ let mut end = fold.range.end.to_point(&buffer);start.row += row_delta;end.row += row_delta;refold_ranges.push(start..end);diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rsindex 06f25c88b6f..0aee6c301bd 100644--- a/crates/editor2/src/element.rs+++ b/crates/editor2/src/element.rs@@ -1581,11 +1581,11 @@ impl EditorElement {snapshot.folds_in_range(start_anchor..end_anchor).map(|anchor| {- let start = anchor.start.to_point(&snapshot.buffer_snapshot);+ let start = anchor.range.start.to_point(&snapshot.buffer_snapshot);(start.row,start.to_display_point(&snapshot.display_snapshot)- ..anchor.end.to_display_point(&snapshot),+ ..anchor.range.end.to_display_point(&snapshot),)}),);diff --git a/crates/editor2/src/git.rs b/crates/editor2/src/git.rsindex e04372f0a7b..6e408cd3a01 100644--- a/crates/editor2/src/git.rs+++ b/crates/editor2/src/git.rs@@ -60,8 +60,8 @@ pub fn diff_hunk_to_display(hunk: DiffHunk<u32>, snapshot: &DisplaySnapshot) ->let folds_end = Point::new(hunk.buffer_range.end + 2, 0);let folds_range = folds_start..folds_end;- let containing_fold = snapshot.folds_in_range(folds_range).find(|fold_range| {- let fold_point_range = fold_range.to_point(&snapshot.buffer_snapshot);+ let containing_fold = snapshot.folds_in_range(folds_range).find(|fold| {+ let fold_point_range = fold.range.to_point(&snapshot.buffer_snapshot);let fold_point_range = fold_point_range.start..=fold_point_range.end;let folded_start = fold_point_range.contains(&hunk_start_point);@@ -72,7 +72,7 @@ pub fn diff_hunk_to_display(hunk: DiffHunk<u32>, snapshot: &DisplaySnapshot) ->});if let Some(fold) = containing_fold {- let row = fold.start.to_display_point(snapshot).row();+ let row = fold.range.start.to_display_point(snapshot).row();DisplayDiffHunk::Folded { display_row: row }} else {let start = hunk_start_point.to_display_point(snapshot).row();diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rsindex c177ffc8c21..9fb9f513148 100644--- a/crates/gpui2/src/window.rs+++ b/crates/gpui2/src/window.rs@@ -2471,7 +2471,7 @@ impl From<SmallVec<[u32; 16]>> for StackingOrder {#[derive(Clone, Debug, Eq, PartialEq, Hash)]pub enum ElementId {View(EntityId),- Number(usize),+ Integer(usize),Name(SharedString),FocusHandle(FocusId),}@@ -2484,13 +2484,13 @@ impl From<EntityId> for ElementId {impl From<usize> for ElementId {fn from(id: usize) -> Self {- ElementId::Number(id)+ ElementId::Integer(id)}}impl From<i32> for ElementId {fn from(id: i32) -> Self {- Self::Number(id as usize)+ Self::Integer(id as usize)}}