From dda9c6898b2c4c1e41e47e3ff5637c12c2b835dc Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 1 Jun 2021 12:28:04 -0700 Subject: [PATCH] Remove count field from FragmentSummary Sort anchors according to their 'full offset' (deleted + visible) --- zed/src/editor/buffer.rs | 33 +++++++++++++-------------------- zed/src/editor/buffer/anchor.rs | 4 ++-- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/zed/src/editor/buffer.rs b/zed/src/editor/buffer.rs index cffe2a5f9ba9e697d2446e073ff60977a339dc3c..7bf58d27a74cb9d096b987c25cef51486b266a29 100644 --- a/zed/src/editor/buffer.rs +++ b/zed/src/editor/buffer.rs @@ -353,7 +353,6 @@ pub struct FragmentSummary { max_version: time::Global, min_insertion_version: time::Global, max_insertion_version: time::Global, - count: usize, } #[derive(Default, Clone, Debug, PartialEq, Eq)] @@ -2030,13 +2029,10 @@ impl Buffer { } } - fn fragment_ix_for_anchor(&self, anchor: &Anchor) -> (usize, usize) { + fn full_offset_for_anchor(&self, anchor: &Anchor) -> usize { match anchor { - Anchor::Start => (0, 0), - Anchor::End => ( - self.fragments.extent::(&None).0, - self.fragments.last().map_or(0, |f| f.visible_len()), - ), + Anchor::Start => 0, + Anchor::End => self.fragments.extent::(&None).0, Anchor::Middle { offset, bias, @@ -2044,14 +2040,15 @@ impl Buffer { } => { let mut cursor = self .fragments - .cursor::(); + .cursor::(); cursor.seek( &VersionedOffset::Offset(*offset), bias.to_seek_bias(), &Some(version.clone()), ); - let count = cursor.start().1; - (count.0, offset - cursor.start().0.offset()) + let full_offset = cursor.start().1; + let visible_offset = cursor.start().0.offset(); + full_offset.0 + offset - visible_offset } } } @@ -2530,7 +2527,6 @@ impl sum_tree::Item for Fragment { max_version, min_insertion_version, max_insertion_version, - count: 1, } } else { FragmentSummary { @@ -2542,7 +2538,6 @@ impl sum_tree::Item for Fragment { max_version, min_insertion_version, max_insertion_version, - count: 1, } } } @@ -2561,7 +2556,6 @@ impl sum_tree::Summary for FragmentSummary { .meet(&other.min_insertion_version); self.max_insertion_version .join(&other.max_insertion_version); - self.count += other.count; } } @@ -2573,7 +2567,6 @@ impl Default for FragmentSummary { max_version: time::Global::new(), min_insertion_version: time::Global::new(), max_insertion_version: time::Global::new(), - count: 0, } } } @@ -2672,19 +2665,19 @@ impl<'a> sum_tree::Dimension<'a, FragmentSummary> for (VersionedOffset, usize) { } } -impl<'a> sum_tree::Dimension<'a, FragmentSummary> for (VersionedOffset, FragmentCount) { +impl<'a> sum_tree::Dimension<'a, FragmentSummary> for (VersionedOffset, FullOffset) { fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option) { self.0.add_summary(summary, cx); - self.1 .0 += summary.count; + self.1 .0 += summary.text.visible + summary.text.deleted; } } -#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Ord, PartialOrd)] -struct FragmentCount(usize); +#[derive(Clone, Copy, Debug, Default)] +struct FullOffset(usize); -impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FragmentCount { +impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FullOffset { fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option) { - self.0 += summary.count; + self.0 += summary.text.visible + summary.text.deleted; } } diff --git a/zed/src/editor/buffer/anchor.rs b/zed/src/editor/buffer/anchor.rs index 518d4afe8f3b8bcb1c69e06bcd3984b827e8c703..1f44eb65f0fc81d17f6a5e08614660a768ffa269 100644 --- a/zed/src/editor/buffer/anchor.rs +++ b/zed/src/editor/buffer/anchor.rs @@ -67,8 +67,8 @@ impl Anchor { bias: other_bias, .. }, ) => buffer - .fragment_ix_for_anchor(self) - .cmp(&buffer.fragment_ix_for_anchor(other)) + .full_offset_for_anchor(self) + .cmp(&buffer.full_offset_for_anchor(other)) .then_with(|| self_bias.cmp(&other_bias)), }) }