sum_tree: Do not implement Dimension on tuples, use new Dimensions wrapper instead (#35482)

This is a bit of a readability improvement IMHO; I often find myself
confused when dealing when dimension pairs, as there's no easy way to
jump to the implementation of a dimension for tuples to remind myself
for the n-th time how exactly that impl works. Now it should be possible
to jump directly to that impl.

Another bonus is that Dimension supports 3-ary tuples as well - by using
a () as a default value of a 3rd dimension.


Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2025-08-05 02:37:22 +02:00 committed by GitHub
parent be2f54b233
commit 07e3d53d58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 215 additions and 141 deletions

View file

@ -3,7 +3,7 @@ use crate::{
locator::Locator,
};
use std::{cmp::Ordering, fmt::Debug, ops::Range};
use sum_tree::Bias;
use sum_tree::{Bias, Dimensions};
/// A timestamped position in a buffer
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, Default)]
@ -102,7 +102,9 @@ impl Anchor {
let Some(fragment_id) = buffer.try_fragment_id_for_anchor(self) else {
return false;
};
let mut fragment_cursor = buffer.fragments.cursor::<(Option<&Locator>, usize)>(&None);
let mut fragment_cursor = buffer
.fragments
.cursor::<Dimensions<Option<&Locator>, usize>>(&None);
fragment_cursor.seek(&Some(fragment_id), Bias::Left);
fragment_cursor
.item()

View file

@ -37,7 +37,7 @@ use std::{
};
pub use subscription::*;
pub use sum_tree::Bias;
use sum_tree::{FilterCursor, SumTree, TreeMap, TreeSet};
use sum_tree::{Dimensions, FilterCursor, SumTree, TreeMap, TreeSet};
use undo_map::UndoMap;
#[cfg(any(test, feature = "test-support"))]
@ -1071,7 +1071,9 @@ impl Buffer {
let mut insertion_offset = 0;
let mut new_ropes =
RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
let mut old_fragments = self.fragments.cursor::<(VersionedFullOffset, usize)>(&cx);
let mut old_fragments = self
.fragments
.cursor::<Dimensions<VersionedFullOffset, usize>>(&cx);
let mut new_fragments =
old_fragments.slice(&VersionedFullOffset::Offset(ranges[0].start), Bias::Left);
new_ropes.append(new_fragments.summary().text);
@ -1298,7 +1300,9 @@ impl Buffer {
self.snapshot.undo_map.insert(undo);
let mut edits = Patch::default();
let mut old_fragments = self.fragments.cursor::<(Option<&Locator>, usize)>(&None);
let mut old_fragments = self
.fragments
.cursor::<Dimensions<Option<&Locator>, usize>>(&None);
let mut new_fragments = SumTree::new(&None);
let mut new_ropes =
RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
@ -1561,7 +1565,9 @@ impl Buffer {
D: TextDimension,
{
// get fragment ranges
let mut cursor = self.fragments.cursor::<(Option<&Locator>, usize)>(&None);
let mut cursor = self
.fragments
.cursor::<Dimensions<Option<&Locator>, usize>>(&None);
let offset_ranges = self
.fragment_ids_for_edits(edit_ids.into_iter())
.into_iter()
@ -2232,7 +2238,9 @@ impl BufferSnapshot {
{
let anchors = anchors.into_iter();
let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>(&());
let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>(&None);
let mut fragment_cursor = self
.fragments
.cursor::<Dimensions<Option<&Locator>, usize>>(&None);
let mut text_cursor = self.visible_text.cursor(0);
let mut position = D::zero(&());
@ -2318,7 +2326,9 @@ impl BufferSnapshot {
);
};
let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>(&None);
let mut fragment_cursor = self
.fragments
.cursor::<Dimensions<Option<&Locator>, usize>>(&None);
fragment_cursor.seek(&Some(&insertion.fragment_id), Bias::Left);
let fragment = fragment_cursor.item().unwrap();
let mut fragment_offset = fragment_cursor.start().1;
@ -2476,7 +2486,7 @@ impl BufferSnapshot {
};
let mut cursor = self
.fragments
.cursor::<(Option<&Locator>, FragmentTextSummary)>(&None);
.cursor::<Dimensions<Option<&Locator>, FragmentTextSummary>>(&None);
let start_fragment_id = self.fragment_id_for_anchor(&range.start);
cursor.seek(&Some(start_fragment_id), Bias::Left);