Give more specific names to all snapshot and text iterator types

This commit is contained in:
Max Brunsfeld
2021-12-08 09:24:00 -08:00
parent ad33111a22
commit fa379885f1
13 changed files with 229 additions and 207 deletions
+27 -17
View File
@@ -1,4 +1,4 @@
use crate::{rope::TextDimension, Snapshot};
use crate::{rope::TextDimension, BufferSnapshot};
use super::{Buffer, FromAnchor, FullOffset, Point, ToOffset};
use anyhow::Result;
@@ -83,7 +83,7 @@ impl Anchor {
}
}
pub fn cmp<'a>(&self, other: &Anchor, buffer: &Snapshot) -> Result<Ordering> {
pub fn cmp<'a>(&self, other: &Anchor, buffer: &BufferSnapshot) -> Result<Ordering> {
if self == other {
return Ok(Ordering::Equal);
}
@@ -115,7 +115,7 @@ impl Anchor {
}
}
pub fn summary<'a, D>(&self, content: &'a Snapshot) -> D
pub fn summary<'a, D>(&self, content: &'a BufferSnapshot) -> D
where
D: TextDimension<'a>,
{
@@ -132,7 +132,10 @@ impl<T> AnchorMap<T> {
self.entries.len()
}
pub fn iter<'a, D>(&'a self, snapshot: &'a Snapshot) -> impl Iterator<Item = (D, &'a T)> + 'a
pub fn iter<'a, D>(
&'a self,
snapshot: &'a BufferSnapshot,
) -> impl Iterator<Item = (D, &'a T)> + 'a
where
D: 'a + TextDimension<'a>,
{
@@ -155,7 +158,7 @@ impl AnchorSet {
self.0.len()
}
pub fn iter<'a, D>(&'a self, content: &'a Snapshot) -> impl Iterator<Item = D> + 'a
pub fn iter<'a, D>(&'a self, content: &'a BufferSnapshot) -> impl Iterator<Item = D> + 'a
where
D: 'a + TextDimension<'a>,
{
@@ -188,7 +191,7 @@ impl<T> AnchorRangeMap<T> {
pub fn ranges<'a, D>(
&'a self,
content: &'a Snapshot,
content: &'a BufferSnapshot,
) -> impl Iterator<Item = (Range<D>, &'a T)> + 'a
where
D: 'a + TextDimension<'a>,
@@ -206,7 +209,7 @@ impl<T> AnchorRangeMap<T> {
pub fn intersecting_ranges<'a, D, I>(
&'a self,
range: Range<(I, Bias)>,
content: &'a Snapshot,
content: &'a BufferSnapshot,
) -> impl Iterator<Item = (Range<D>, &'a T)> + 'a
where
D: 'a + TextDimension<'a>,
@@ -243,7 +246,7 @@ impl<T> AnchorRangeMap<T> {
pub fn min_by_key<'a, D, F, K>(
&self,
content: &'a Snapshot,
content: &'a BufferSnapshot,
mut extract_key: F,
) -> Option<(Range<D>, &T)>
where
@@ -259,7 +262,7 @@ impl<T> AnchorRangeMap<T> {
pub fn max_by_key<'a, D, F, K>(
&self,
content: &'a Snapshot,
content: &'a BufferSnapshot,
mut extract_key: F,
) -> Option<(Range<D>, &T)>
where
@@ -273,7 +276,11 @@ impl<T> AnchorRangeMap<T> {
.map(|(range, value)| (self.resolve_range(range, &content), value))
}
fn resolve_range<'a, D>(&self, range: &Range<FullOffset>, content: &'a Snapshot) -> Range<D>
fn resolve_range<'a, D>(
&self,
range: &Range<FullOffset>,
content: &'a BufferSnapshot,
) -> Range<D>
where
D: 'a + TextDimension<'a>,
{
@@ -330,7 +337,10 @@ impl AnchorRangeSet {
self.0.version()
}
pub fn ranges<'a, D>(&'a self, content: &'a Snapshot) -> impl 'a + Iterator<Item = Range<Point>>
pub fn ranges<'a, D>(
&'a self,
content: &'a BufferSnapshot,
) -> impl 'a + Iterator<Item = Range<Point>>
where
D: 'a + TextDimension<'a>,
{
@@ -357,7 +367,7 @@ impl<T: Clone> AnchorRangeMultimap<T> {
pub fn intersecting_ranges<'a, I, O>(
&'a self,
range: Range<I>,
content: &'a Snapshot,
content: &'a BufferSnapshot,
inclusive: bool,
) -> impl Iterator<Item = (usize, Range<O>, &T)> + 'a
where
@@ -451,7 +461,7 @@ impl<T: Clone> AnchorRangeMultimap<T> {
pub fn filter<'a, O, F>(
&'a self,
content: &'a Snapshot,
content: &'a BufferSnapshot,
mut f: F,
) -> impl 'a + Iterator<Item = (usize, Range<O>, &T)>
where
@@ -560,19 +570,19 @@ impl<'a> sum_tree::SeekTarget<'a, AnchorRangeMultimapSummary, FullOffsetRange> f
}
pub trait AnchorRangeExt {
fn cmp(&self, b: &Range<Anchor>, buffer: &Snapshot) -> Result<Ordering>;
fn to_offset(&self, content: &Snapshot) -> Range<usize>;
fn cmp(&self, b: &Range<Anchor>, buffer: &BufferSnapshot) -> Result<Ordering>;
fn to_offset(&self, content: &BufferSnapshot) -> Range<usize>;
}
impl AnchorRangeExt for Range<Anchor> {
fn cmp(&self, other: &Range<Anchor>, buffer: &Snapshot) -> Result<Ordering> {
fn cmp(&self, other: &Range<Anchor>, buffer: &BufferSnapshot) -> Result<Ordering> {
Ok(match self.start.cmp(&other.start, buffer)? {
Ordering::Equal => other.end.cmp(&self.end, buffer)?,
ord @ _ => ord,
})
}
fn to_offset(&self, content: &Snapshot) -> Range<usize> {
fn to_offset(&self, content: &BufferSnapshot) -> Range<usize> {
self.start.to_offset(&content)..self.end.to_offset(&content)
}
}
+5 -5
View File
@@ -1,6 +1,6 @@
use sum_tree::Bias;
use crate::{rope::TextDimension, Snapshot};
use crate::{rope::TextDimension, BufferSnapshot};
use super::{AnchorRangeMap, Buffer, Point, ToOffset, ToPoint};
use std::{cmp::Ordering, ops::Range, sync::Arc};
@@ -105,7 +105,7 @@ impl SelectionSet {
pub fn selections<'a, D>(
&'a self,
content: &'a Snapshot,
content: &'a BufferSnapshot,
) -> impl 'a + Iterator<Item = Selection<D>>
where
D: 'a + TextDimension<'a>,
@@ -124,7 +124,7 @@ impl SelectionSet {
pub fn intersecting_selections<'a, D, I>(
&'a self,
range: Range<(I, Bias)>,
content: &'a Snapshot,
content: &'a BufferSnapshot,
) -> impl 'a + Iterator<Item = Selection<D>>
where
D: 'a + TextDimension<'a>,
@@ -141,7 +141,7 @@ impl SelectionSet {
})
}
pub fn oldest_selection<'a, D>(&'a self, content: &'a Snapshot) -> Option<Selection<D>>
pub fn oldest_selection<'a, D>(&'a self, content: &'a BufferSnapshot) -> Option<Selection<D>>
where
D: 'a + TextDimension<'a>,
{
@@ -156,7 +156,7 @@ impl SelectionSet {
})
}
pub fn newest_selection<'a, D>(&'a self, content: &'a Snapshot) -> Option<Selection<D>>
pub fn newest_selection<'a, D>(&'a self, content: &'a BufferSnapshot) -> Option<Selection<D>>
where
D: 'a + TextDimension<'a>,
{
+22 -22
View File
@@ -37,7 +37,7 @@ pub use sum_tree::Bias;
use sum_tree::{FilterCursor, SumTree};
pub struct Buffer {
snapshot: Snapshot,
snapshot: BufferSnapshot,
last_edit: clock::Local,
history: History,
selections: HashMap<SelectionSetId, SelectionSet>,
@@ -51,7 +51,7 @@ pub struct Buffer {
}
#[derive(Clone, Debug)]
pub struct Snapshot {
pub struct BufferSnapshot {
visible_text: Rope,
deleted_text: Rope,
undo_map: UndoMap,
@@ -473,7 +473,7 @@ impl Buffer {
}
Buffer {
snapshot: Snapshot {
snapshot: BufferSnapshot {
visible_text,
deleted_text: Rope::new(),
fragments,
@@ -497,8 +497,8 @@ impl Buffer {
self.version.clone()
}
pub fn snapshot(&self) -> Snapshot {
Snapshot {
pub fn snapshot(&self) -> BufferSnapshot {
BufferSnapshot {
visible_text: self.visible_text.clone(),
deleted_text: self.deleted_text.clone(),
undo_map: self.undo_map.clone(),
@@ -1476,14 +1476,14 @@ impl Buffer {
}
impl Deref for Buffer {
type Target = Snapshot;
type Target = BufferSnapshot;
fn deref(&self) -> &Self::Target {
&self.snapshot
}
}
impl Snapshot {
impl BufferSnapshot {
pub fn as_rope(&self) -> &Rope {
&self.visible_text
}
@@ -2254,9 +2254,9 @@ impl Operation {
}
pub trait ToOffset {
fn to_offset<'a>(&self, content: &Snapshot) -> usize;
fn to_offset<'a>(&self, content: &BufferSnapshot) -> usize;
fn to_full_offset<'a>(&self, content: &Snapshot, bias: Bias) -> FullOffset {
fn to_full_offset<'a>(&self, content: &BufferSnapshot, bias: Bias) -> FullOffset {
let offset = self.to_offset(&content);
let mut cursor = content.fragments.cursor::<FragmentTextSummary>();
cursor.seek(&offset, bias, &None);
@@ -2265,30 +2265,30 @@ pub trait ToOffset {
}
impl ToOffset for Point {
fn to_offset<'a>(&self, content: &Snapshot) -> usize {
fn to_offset<'a>(&self, content: &BufferSnapshot) -> usize {
content.visible_text.point_to_offset(*self)
}
}
impl ToOffset for PointUtf16 {
fn to_offset<'a>(&self, content: &Snapshot) -> usize {
fn to_offset<'a>(&self, content: &BufferSnapshot) -> usize {
content.visible_text.point_utf16_to_offset(*self)
}
}
impl ToOffset for usize {
fn to_offset<'a>(&self, content: &Snapshot) -> usize {
fn to_offset<'a>(&self, content: &BufferSnapshot) -> usize {
assert!(*self <= content.len(), "offset is out of range");
*self
}
}
impl ToOffset for Anchor {
fn to_offset<'a>(&self, content: &Snapshot) -> usize {
fn to_offset<'a>(&self, content: &BufferSnapshot) -> usize {
content.summary_for_anchor(self)
}
fn to_full_offset<'a>(&self, content: &Snapshot, bias: Bias) -> FullOffset {
fn to_full_offset<'a>(&self, content: &BufferSnapshot, bias: Bias) -> FullOffset {
if content.version == self.version {
self.full_offset
} else {
@@ -2312,45 +2312,45 @@ impl ToOffset for Anchor {
}
impl<'a> ToOffset for &'a Anchor {
fn to_offset(&self, content: &Snapshot) -> usize {
fn to_offset(&self, content: &BufferSnapshot) -> usize {
content.summary_for_anchor(self)
}
}
pub trait ToPoint {
fn to_point<'a>(&self, content: &Snapshot) -> Point;
fn to_point<'a>(&self, content: &BufferSnapshot) -> Point;
}
impl ToPoint for Anchor {
fn to_point<'a>(&self, content: &Snapshot) -> Point {
fn to_point<'a>(&self, content: &BufferSnapshot) -> Point {
content.summary_for_anchor(self)
}
}
impl ToPoint for usize {
fn to_point<'a>(&self, content: &Snapshot) -> Point {
fn to_point<'a>(&self, content: &BufferSnapshot) -> Point {
content.visible_text.offset_to_point(*self)
}
}
impl ToPoint for Point {
fn to_point<'a>(&self, _: &Snapshot) -> Point {
fn to_point<'a>(&self, _: &BufferSnapshot) -> Point {
*self
}
}
pub trait FromAnchor {
fn from_anchor(anchor: &Anchor, content: &Snapshot) -> Self;
fn from_anchor(anchor: &Anchor, content: &BufferSnapshot) -> Self;
}
impl FromAnchor for Point {
fn from_anchor(anchor: &Anchor, content: &Snapshot) -> Self {
fn from_anchor(anchor: &Anchor, content: &BufferSnapshot) -> Self {
anchor.to_point(content)
}
}
impl FromAnchor for usize {
fn from_anchor(anchor: &Anchor, content: &Snapshot) -> Self {
fn from_anchor(anchor: &Anchor, content: &BufferSnapshot) -> Self {
anchor.to_offset(content)
}
}