Fix first FoldMap methods after the map move

Co-Authored-By: Antonio Scandurra <antonio@zed.dev>
This commit is contained in:
Kirill Bulatov 2023-06-16 12:37:25 +03:00
parent 9ae611fa89
commit 29bb6c67b0
2 changed files with 239 additions and 246 deletions

View file

@ -1,5 +1,5 @@
use super::{ use super::{
inlay_map::{InlayEdit, InlayOffset, InlayPoint, InlaySnapshot}, inlay_map::{InlayBufferRows, InlayEdit, InlayOffset, InlayPoint, InlaySnapshot},
TextHighlights, TextHighlights,
}; };
use crate::{ use crate::{
@ -15,7 +15,7 @@ use std::{
cmp::{self, Ordering}, cmp::{self, Ordering},
iter::{self, Peekable}, iter::{self, Peekable},
ops::{Add, AddAssign, Range, Sub}, ops::{Add, AddAssign, Range, Sub},
sync::atomic::{AtomicUsize, Ordering::SeqCst}, sync::atomic::Ordering::SeqCst,
vec, vec,
}; };
use sum_tree::{Bias, Cursor, FilterCursor, SumTree}; use sum_tree::{Bias, Cursor, FilterCursor, SumTree};
@ -65,25 +65,17 @@ impl FoldPoint {
.transforms .transforms
.cursor::<(FoldPoint, TransformSummary)>(); .cursor::<(FoldPoint, TransformSummary)>();
cursor.seek(&self, Bias::Right, &()); cursor.seek(&self, Bias::Right, &());
let inlay_snapshot = &snapshot.inlay_snapshot;
let to_inlay_offset = |buffer_offset: usize| {
let buffer_point = inlay_snapshot.buffer.offset_to_point(buffer_offset);
inlay_snapshot.to_offset(inlay_snapshot.to_inlay_point(buffer_point))
};
let mut inlay_offset = to_inlay_offset(cursor.start().1.output.len);
let overshoot = self.0 - cursor.start().1.output.lines; let overshoot = self.0 - cursor.start().1.output.lines;
let mut offset = cursor.start().1.output.len;
if !overshoot.is_zero() { if !overshoot.is_zero() {
let transform = cursor.item().expect("display point out of range"); let transform = cursor.item().expect("display point out of range");
assert!(transform.output_text.is_none()); assert!(transform.output_text.is_none());
let end_snapshot_offset = snapshot let end_inlay_offset = snapshot
.inlay_snapshot .inlay_snapshot
.to_offset(InlayPoint(cursor.start().1.input.lines + overshoot)); .to_offset(InlayPoint(cursor.start().1.input.lines + overshoot));
inlay_offset += end_snapshot_offset - to_inlay_offset(cursor.start().1.input.len); offset += end_inlay_offset.0 - cursor.start().1.input.len;
} }
FoldOffset(offset)
snapshot
.to_fold_point(inlay_snapshot.to_point(inlay_offset), Bias::Right)
.to_offset(snapshot)
} }
} }
@ -148,7 +140,7 @@ impl<'a> FoldMapWriter<'a> {
transforms: self.0.transforms.lock().clone(), transforms: self.0.transforms.lock().clone(),
folds: self.0.folds.clone(), folds: self.0.folds.clone(),
inlay_snapshot: snapshot, inlay_snapshot: snapshot,
version: self.0.version.load(SeqCst), version: self.0.version,
ellipses_color: self.0.ellipses_color, ellipses_color: self.0.ellipses_color,
}; };
(snapshot, edits) (snapshot, edits)
@ -201,7 +193,7 @@ impl<'a> FoldMapWriter<'a> {
transforms: self.0.transforms.lock().clone(), transforms: self.0.transforms.lock().clone(),
folds: self.0.folds.clone(), folds: self.0.folds.clone(),
inlay_snapshot: snapshot, inlay_snapshot: snapshot,
version: self.0.version.load(SeqCst), version: self.0.version,
ellipses_color: self.0.ellipses_color, ellipses_color: self.0.ellipses_color,
}; };
(snapshot, edits) (snapshot, edits)
@ -212,7 +204,7 @@ pub struct FoldMap {
inlay_snapshot: Mutex<InlaySnapshot>, inlay_snapshot: Mutex<InlaySnapshot>,
transforms: Mutex<SumTree<Transform>>, transforms: Mutex<SumTree<Transform>>,
folds: SumTree<Fold>, folds: SumTree<Fold>,
version: AtomicUsize, version: usize,
ellipses_color: Option<Color>, ellipses_color: Option<Color>,
} }
@ -232,14 +224,14 @@ impl FoldMap {
&(), &(),
)), )),
ellipses_color: None, ellipses_color: None,
version: Default::default(), version: 0,
}; };
let snapshot = FoldSnapshot { let snapshot = FoldSnapshot {
transforms: this.transforms.lock().clone(), transforms: this.transforms.lock().clone(),
folds: this.folds.clone(), folds: this.folds.clone(),
inlay_snapshot: inlay_snapshot.clone(), inlay_snapshot: inlay_snapshot.clone(),
version: this.version.load(SeqCst), version: this.version,
ellipses_color: None, ellipses_color: None,
}; };
(this, snapshot) (this, snapshot)
@ -256,7 +248,7 @@ impl FoldMap {
transforms: self.transforms.lock().clone(), transforms: self.transforms.lock().clone(),
folds: self.folds.clone(), folds: self.folds.clone(),
inlay_snapshot: self.inlay_snapshot.lock().clone(), inlay_snapshot: self.inlay_snapshot.lock().clone(),
version: self.version.load(SeqCst), version: self.version,
ellipses_color: self.ellipses_color, ellipses_color: self.ellipses_color,
}; };
(snapshot, edits) (snapshot, edits)
@ -299,54 +291,44 @@ impl FoldMap {
} }
} }
fn sync(&self, inlay_snapshot: InlaySnapshot, inlay_edits: Vec<InlayEdit>) -> Vec<FoldEdit> { fn sync(
let buffer = &inlay_snapshot.buffer; &mut self,
let snapshot = self.inlay_snapshot.lock(); inlay_snapshot: InlaySnapshot,
inlay_edits: Vec<InlayEdit>,
let mut new_snapshot = snapshot.clone(); ) -> Vec<FoldEdit> {
if new_snapshot.version != inlay_snapshot.version { if inlay_edits.is_empty() {
new_snapshot.version += 1; if self.inlay_snapshot.lock().version != inlay_snapshot.version {
self.version += 1;
} }
*self.inlay_snapshot.lock() = inlay_snapshot;
Vec::new()
} else {
let mut inlay_edits_iter = inlay_edits.iter().cloned().peekable(); let mut inlay_edits_iter = inlay_edits.iter().cloned().peekable();
let mut new_transforms = SumTree::new(); let mut new_transforms = SumTree::new();
let mut transforms = self.transforms.lock(); let mut transforms = self.transforms.lock();
let mut cursor = transforms.cursor::<usize>(); let mut cursor = transforms.cursor::<InlayOffset>();
cursor.seek(&0, Bias::Right, &()); cursor.seek(&InlayOffset(0), Bias::Right, &());
while let Some(inlay_edit) = inlay_edits_iter.next() { while let Some(mut edit) = inlay_edits_iter.next() {
// TODO kb is this right?
let mut edit = Edit {
old: inlay_snapshot.to_buffer_offset(inlay_edit.old.start)
..inlay_snapshot.to_buffer_offset(inlay_edit.old.end),
new: inlay_snapshot.to_buffer_offset(inlay_edit.new.start)
..inlay_snapshot.to_buffer_offset(inlay_edit.new.end),
};
new_transforms.append(cursor.slice(&edit.old.start, Bias::Left, &()), &()); new_transforms.append(cursor.slice(&edit.old.start, Bias::Left, &()), &());
edit.new.start -= edit.old.start - cursor.start(); edit.new.start -= edit.old.start - *cursor.start();
edit.old.start = *cursor.start(); edit.old.start = *cursor.start();
cursor.seek(&edit.old.end, Bias::Right, &()); cursor.seek(&edit.old.end, Bias::Right, &());
cursor.next(&()); cursor.next(&());
let mut delta = edit.new.len() as isize - edit.old.len() as isize; let mut delta = edit.new_len().0 as isize - edit.old_len().0 as isize;
loop { loop {
edit.old.end = *cursor.start(); edit.old.end = *cursor.start();
if let Some(next_inlay_edit) = inlay_edits_iter.peek() { if let Some(next_edit) = inlay_edits_iter.peek() {
if next_inlay_edit.old.start > inlay_snapshot.to_inlay_offset(edit.old.end) { if next_edit.old.start > edit.old.end {
break; break;
} }
let next_inlay_edit = inlay_edits_iter.next().unwrap(); let next_edit = inlay_edits_iter.next().unwrap();
let next_edit = Edit { delta += next_edit.new_len().0 as isize - next_edit.old_len().0 as isize;
old: inlay_snapshot.to_buffer_offset(next_inlay_edit.old.start)
..inlay_snapshot.to_buffer_offset(next_inlay_edit.old.end),
new: inlay_snapshot.to_buffer_offset(next_inlay_edit.new.start)
..inlay_snapshot.to_buffer_offset(next_inlay_edit.new.end),
};
delta += next_edit.new.len() as isize - next_edit.old.len() as isize;
if next_edit.old.end >= edit.old.end { if next_edit.old.end >= edit.old.end {
edit.old.end = next_edit.old.end; edit.old.end = next_edit.old.end;
@ -358,34 +340,39 @@ impl FoldMap {
} }
} }
edit.new.end = ((edit.new.start + edit.old.len()) as isize + delta) as usize; edit.new.end =
InlayOffset(((edit.new.start + edit.old_len()).0 as isize + delta) as usize);
let anchor = buffer.anchor_before(edit.new.start); let anchor = inlay_snapshot
.buffer
.anchor_before(inlay_snapshot.to_buffer_offset(edit.new.start));
let mut folds_cursor = self.folds.cursor::<Fold>(); let mut folds_cursor = self.folds.cursor::<Fold>();
folds_cursor.seek(&Fold(anchor..Anchor::max()), Bias::Left, &buffer); folds_cursor.seek(
&Fold(anchor..Anchor::max()),
Bias::Left,
&inlay_snapshot.buffer,
);
let mut folds = iter::from_fn({ let mut folds = iter::from_fn({
|| { let inlay_snapshot = &inlay_snapshot;
move || {
let item = folds_cursor.item().map(|f| { let item = folds_cursor.item().map(|f| {
let fold_buffer_start = f.0.start.to_offset(buffer); let buffer_start = f.0.start.to_offset(&inlay_snapshot.buffer);
let fold_buffer_end = f.0.end.to_offset(buffer); let buffer_end = f.0.end.to_offset(&inlay_snapshot.buffer);
inlay_snapshot.to_inlay_offset(buffer_start)
inlay_snapshot.to_inlay_offset(fold_buffer_start) ..inlay_snapshot.to_inlay_offset(buffer_end)
..inlay_snapshot.to_inlay_offset(fold_buffer_end)
}); });
folds_cursor.next(buffer); folds_cursor.next(&inlay_snapshot.buffer);
item item
} }
}) })
.peekable(); .peekable();
while folds.peek().map_or(false, |fold| { while folds.peek().map_or(false, |fold| fold.start < edit.new.end) {
inlay_snapshot.to_buffer_offset(fold.start) < edit.new.end
}) {
let mut fold = folds.next().unwrap(); let mut fold = folds.next().unwrap();
let sum = new_transforms.summary(); let sum = new_transforms.summary();
assert!(inlay_snapshot.to_buffer_offset(fold.start) >= sum.input.len); assert!(fold.start.0 >= sum.input.len);
while folds while folds
.peek() .peek()
@ -397,10 +384,9 @@ impl FoldMap {
} }
} }
if inlay_snapshot.to_buffer_offset(fold.start) > sum.input.len { if fold.start.0 > sum.input.len {
let text_summary = buffer.text_summary_for_range::<TextSummary, _>( let text_summary = inlay_snapshot
sum.input.len..inlay_snapshot.to_buffer_offset(fold.start), .text_summary_for_range(InlayOffset(sum.input.len)..fold.start);
);
new_transforms.push( new_transforms.push(
Transform { Transform {
summary: TransformSummary { summary: TransformSummary {
@ -419,10 +405,8 @@ impl FoldMap {
Transform { Transform {
summary: TransformSummary { summary: TransformSummary {
output: TextSummary::from(output_text), output: TextSummary::from(output_text),
input: inlay_snapshot.text_summary_for_range( input: inlay_snapshot
inlay_snapshot.to_point(fold.start) .text_summary_for_range(fold.start..fold.end),
..inlay_snapshot.to_point(fold.end),
),
}, },
output_text: Some(output_text), output_text: Some(output_text),
}, },
@ -432,9 +416,9 @@ impl FoldMap {
} }
let sum = new_transforms.summary(); let sum = new_transforms.summary();
if sum.input.len < edit.new.end { if sum.input.len < edit.new.end.0 {
let text_summary: TextSummary = let text_summary = inlay_snapshot
buffer.text_summary_for_range(sum.input.len..edit.new.end); .text_summary_for_range(InlayOffset(sum.input.len)..edit.new.end);
new_transforms.push( new_transforms.push(
Transform { Transform {
summary: TransformSummary { summary: TransformSummary {
@ -467,22 +451,16 @@ impl FoldMap {
let mut fold_edits = Vec::with_capacity(inlay_edits.len()); let mut fold_edits = Vec::with_capacity(inlay_edits.len());
{ {
let mut old_transforms = transforms.cursor::<(usize, FoldOffset)>(); let mut old_transforms = transforms.cursor::<(InlayOffset, FoldOffset)>();
let mut new_transforms = new_transforms.cursor::<(usize, FoldOffset)>(); let mut new_transforms = new_transforms.cursor::<(InlayOffset, FoldOffset)>();
for inlay_edit in inlay_edits { for mut edit in inlay_edits {
let mut edit = Edit {
old: inlay_snapshot.to_buffer_offset(inlay_edit.old.start)
..inlay_snapshot.to_buffer_offset(inlay_edit.old.end),
new: inlay_snapshot.to_buffer_offset(inlay_edit.new.start)
..inlay_snapshot.to_buffer_offset(inlay_edit.new.end),
};
old_transforms.seek(&edit.old.start, Bias::Left, &()); old_transforms.seek(&edit.old.start, Bias::Left, &());
if old_transforms.item().map_or(false, |t| t.is_fold()) { if old_transforms.item().map_or(false, |t| t.is_fold()) {
edit.old.start = old_transforms.start().0; edit.old.start = old_transforms.start().0;
} }
let old_start = let old_start =
old_transforms.start().1 .0 + (edit.old.start - old_transforms.start().0); old_transforms.start().1 .0 + (edit.old.start - old_transforms.start().0).0;
old_transforms.seek_forward(&edit.old.end, Bias::Right, &()); old_transforms.seek_forward(&edit.old.end, Bias::Right, &());
if old_transforms.item().map_or(false, |t| t.is_fold()) { if old_transforms.item().map_or(false, |t| t.is_fold()) {
@ -490,14 +468,14 @@ impl FoldMap {
edit.old.end = old_transforms.start().0; edit.old.end = old_transforms.start().0;
} }
let old_end = let old_end =
old_transforms.start().1 .0 + (edit.old.end - old_transforms.start().0); old_transforms.start().1 .0 + (edit.old.end - old_transforms.start().0).0;
new_transforms.seek(&edit.new.start, Bias::Left, &()); new_transforms.seek(&edit.new.start, Bias::Left, &());
if new_transforms.item().map_or(false, |t| t.is_fold()) { if new_transforms.item().map_or(false, |t| t.is_fold()) {
edit.new.start = new_transforms.start().0; edit.new.start = new_transforms.start().0;
} }
let new_start = let new_start =
new_transforms.start().1 .0 + (edit.new.start - new_transforms.start().0); new_transforms.start().1 .0 + (edit.new.start - new_transforms.start().0).0;
new_transforms.seek_forward(&edit.new.end, Bias::Right, &()); new_transforms.seek_forward(&edit.new.end, Bias::Right, &());
if new_transforms.item().map_or(false, |t| t.is_fold()) { if new_transforms.item().map_or(false, |t| t.is_fold()) {
@ -505,7 +483,7 @@ impl FoldMap {
edit.new.end = new_transforms.start().0; edit.new.end = new_transforms.start().0;
} }
let new_end = let new_end =
new_transforms.start().1 .0 + (edit.new.end - new_transforms.start().0); new_transforms.start().1 .0 + (edit.new.end - new_transforms.start().0).0;
fold_edits.push(FoldEdit { fold_edits.push(FoldEdit {
old: FoldOffset(old_start)..FoldOffset(old_end), old: FoldOffset(old_start)..FoldOffset(old_end),
@ -518,10 +496,11 @@ impl FoldMap {
*transforms = new_transforms; *transforms = new_transforms;
*self.inlay_snapshot.lock() = inlay_snapshot; *self.inlay_snapshot.lock() = inlay_snapshot;
self.version.fetch_add(1, SeqCst); self.version += 1;
fold_edits fold_edits
} }
} }
}
#[derive(Clone)] #[derive(Clone)]
pub struct FoldSnapshot { pub struct FoldSnapshot {
@ -552,7 +531,7 @@ impl FoldSnapshot {
pub fn text_summary_for_range(&self, range: Range<FoldPoint>) -> TextSummary { pub fn text_summary_for_range(&self, range: Range<FoldPoint>) -> TextSummary {
let mut summary = TextSummary::default(); let mut summary = TextSummary::default();
let mut cursor = self.transforms.cursor::<(FoldPoint, Point)>(); let mut cursor = self.transforms.cursor::<(FoldPoint, InlayPoint)>();
cursor.seek(&range.start, Bias::Right, &()); cursor.seek(&range.start, Bias::Right, &());
if let Some(transform) = cursor.item() { if let Some(transform) = cursor.item() {
let start_in_transform = range.start.0 - cursor.start().0 .0; let start_in_transform = range.start.0 - cursor.start().0 .0;
@ -563,12 +542,15 @@ impl FoldSnapshot {
[start_in_transform.column as usize..end_in_transform.column as usize], [start_in_transform.column as usize..end_in_transform.column as usize],
); );
} else { } else {
let buffer_start = cursor.start().1 + start_in_transform; let inlay_start = self
let buffer_end = cursor.start().1 + end_in_transform; .inlay_snapshot
.to_offset(InlayPoint(cursor.start().1 .0 + start_in_transform));
let inlay_end = self
.inlay_snapshot
.to_offset(InlayPoint(cursor.start().1 .0 + end_in_transform));
summary = self summary = self
.inlay_snapshot .inlay_snapshot
.buffer .text_summary_for_range(inlay_start..inlay_end);
.text_summary_for_range(buffer_start..buffer_end);
} }
} }
@ -582,12 +564,13 @@ impl FoldSnapshot {
if let Some(output_text) = transform.output_text { if let Some(output_text) = transform.output_text {
summary += TextSummary::from(&output_text[..end_in_transform.column as usize]); summary += TextSummary::from(&output_text[..end_in_transform.column as usize]);
} else { } else {
let buffer_start = cursor.start().1; let inlay_start = self.inlay_snapshot.to_offset(cursor.start().1);
let buffer_end = cursor.start().1 + end_in_transform; let inlay_end = self
.inlay_snapshot
.to_offset(InlayPoint(cursor.start().1 .0 + end_in_transform));
summary += self summary += self
.inlay_snapshot .inlay_snapshot
.buffer .text_summary_for_range(inlay_start..inlay_end);
.text_summary_for_range::<TextSummary, _>(buffer_start..buffer_end);
} }
} }
} }
@ -605,12 +588,11 @@ impl FoldSnapshot {
cursor.end(&()).1 cursor.end(&()).1
} }
} else { } else {
let overshoot = InlayPoint(point.0 - cursor.start().0 .0); let overshoot = point.0 - cursor.start().0 .0;
// TODO kb is this right? FoldPoint(cmp::min(
cmp::min( cursor.start().1 .0 + overshoot,
FoldPoint(cursor.start().1 .0 + overshoot.0), cursor.end(&()).1 .0,
cursor.end(&()).1, ))
)
} }
} }
@ -634,12 +616,12 @@ impl FoldSnapshot {
} }
let fold_point = FoldPoint::new(start_row, 0); let fold_point = FoldPoint::new(start_row, 0);
let mut cursor = self.transforms.cursor::<(FoldPoint, Point)>(); let mut cursor = self.transforms.cursor::<(FoldPoint, InlayPoint)>();
cursor.seek(&fold_point, Bias::Left, &()); cursor.seek(&fold_point, Bias::Left, &());
let overshoot = fold_point.0 - cursor.start().0 .0; let overshoot = fold_point.0 - cursor.start().0 .0;
let buffer_point = cursor.start().1 + overshoot; let inlay_point = InlayPoint(cursor.start().1 .0 + overshoot);
let input_buffer_rows = self.inlay_snapshot.buffer.buffer_rows(buffer_point.row); let input_buffer_rows = self.inlay_snapshot.buffer_rows(inlay_point.row());
FoldBufferRows { FoldBufferRows {
fold_point, fold_point,
@ -1053,8 +1035,8 @@ impl<'a> sum_tree::Dimension<'a, FoldSummary> for usize {
#[derive(Clone)] #[derive(Clone)]
pub struct FoldBufferRows<'a> { pub struct FoldBufferRows<'a> {
cursor: Cursor<'a, Transform, (FoldPoint, Point)>, cursor: Cursor<'a, Transform, (FoldPoint, InlayPoint)>,
input_buffer_rows: MultiBufferRows<'a>, input_buffer_rows: InlayBufferRows<'a>,
fold_point: FoldPoint, fold_point: FoldPoint,
} }
@ -1073,7 +1055,7 @@ impl<'a> Iterator for FoldBufferRows<'a> {
if self.cursor.item().is_some() { if self.cursor.item().is_some() {
if traversed_fold { if traversed_fold {
self.input_buffer_rows.seek(self.cursor.start().1.row); self.input_buffer_rows.seek(self.cursor.start().1.row());
self.input_buffer_rows.next(); self.input_buffer_rows.next();
} }
*self.fold_point.row_mut() += 1; *self.fold_point.row_mut() += 1;
@ -1216,16 +1198,12 @@ impl FoldOffset {
.transforms .transforms
.cursor::<(FoldOffset, TransformSummary)>(); .cursor::<(FoldOffset, TransformSummary)>();
cursor.seek(&self, Bias::Right, &()); cursor.seek(&self, Bias::Right, &());
// TODO kb seems wrong to use buffer points?
let overshoot = if cursor.item().map_or(true, |t| t.is_fold()) { let overshoot = if cursor.item().map_or(true, |t| t.is_fold()) {
Point::new(0, (self.0 - cursor.start().0 .0) as u32) Point::new(0, (self.0 - cursor.start().0 .0) as u32)
} else { } else {
let buffer_offset = cursor.start().1.input.len + self.0 - cursor.start().0 .0; let inlay_offset = cursor.start().1.input.len + self.0 - cursor.start().0 .0;
let buffer_point = snapshot let inlay_point = snapshot.inlay_snapshot.to_point(InlayOffset(inlay_offset));
.inlay_snapshot inlay_point.0 - cursor.start().1.input.lines
.buffer
.offset_to_point(buffer_offset);
buffer_point - cursor.start().1.input.lines
}; };
FoldPoint(cursor.start().1.output.lines + overshoot) FoldPoint(cursor.start().1.output.lines + overshoot)
} }
@ -1271,18 +1249,6 @@ impl<'a> sum_tree::Dimension<'a, TransformSummary> for InlayOffset {
} }
} }
impl<'a> sum_tree::Dimension<'a, TransformSummary> for Point {
fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
*self += &summary.input.lines;
}
}
impl<'a> sum_tree::Dimension<'a, TransformSummary> for usize {
fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
*self += &summary.input.len;
}
}
pub type FoldEdit = Edit<FoldOffset>; pub type FoldEdit = Edit<FoldOffset>;
#[cfg(test)] #[cfg(test)]

View file

@ -9,7 +9,7 @@ use language::{Chunk, Edit, Point, Rope, TextSummary};
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{ use std::{
cmp, cmp,
ops::{Add, AddAssign, Range, Sub}, ops::{Add, AddAssign, Range, Sub, SubAssign},
}; };
use sum_tree::{Bias, Cursor, SumTree}; use sum_tree::{Bias, Cursor, SumTree};
use text::Patch; use text::Patch;
@ -93,6 +93,12 @@ impl AddAssign for InlayOffset {
} }
} }
impl SubAssign for InlayOffset {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
}
}
impl<'a> sum_tree::Dimension<'a, TransformSummary> for InlayOffset { impl<'a> sum_tree::Dimension<'a, TransformSummary> for InlayOffset {
fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) { fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
self.0 += &summary.output.len; self.0 += &summary.output.len;
@ -125,6 +131,7 @@ pub struct InlayBufferRows<'a> {
transforms: Cursor<'a, Transform, (InlayPoint, Point)>, transforms: Cursor<'a, Transform, (InlayPoint, Point)>,
buffer_rows: MultiBufferRows<'a>, buffer_rows: MultiBufferRows<'a>,
inlay_row: u32, inlay_row: u32,
max_buffer_row: u32,
} }
pub struct InlayChunks<'a> { pub struct InlayChunks<'a> {
@ -193,6 +200,28 @@ impl<'a> Iterator for InlayChunks<'a> {
} }
} }
impl<'a> InlayBufferRows<'a> {
pub fn seek(&mut self, row: u32) {
let inlay_point = InlayPoint::new(row, 0);
self.transforms.seek(&inlay_point, Bias::Left, &());
let mut buffer_point = self.transforms.start().1;
let buffer_row = if row == 0 {
0
} else {
match self.transforms.item() {
Some(Transform::Isomorphic(_)) => {
buffer_point += inlay_point.0 - self.transforms.start().0 .0;
buffer_point.row
}
_ => cmp::min(buffer_point.row + 1, self.max_buffer_row),
}
};
self.inlay_row = inlay_point.row();
self.buffer_rows.seek(buffer_row);
}
}
impl<'a> Iterator for InlayBufferRows<'a> { impl<'a> Iterator for InlayBufferRows<'a> {
type Item = Option<u32>; type Item = Option<u32>;
@ -632,10 +661,10 @@ impl InlaySnapshot {
self.transforms.summary().output.clone() self.transforms.summary().output.clone()
} }
pub fn text_summary_for_range(&self, range: Range<InlayPoint>) -> TextSummary { pub fn text_summary_for_range(&self, range: Range<InlayOffset>) -> TextSummary {
let mut summary = TextSummary::default(); let mut summary = TextSummary::default();
let mut cursor = self.transforms.cursor::<(InlayPoint, Point)>(); let mut cursor = self.transforms.cursor::<(InlayOffset, usize)>();
cursor.seek(&range.start, Bias::Right, &()); cursor.seek(&range.start, Bias::Right, &());
let overshoot = range.start.0 - cursor.start().0 .0; let overshoot = range.start.0 - cursor.start().0 .0;
@ -649,10 +678,8 @@ impl InlaySnapshot {
cursor.next(&()); cursor.next(&());
} }
Some(Transform::Inlay(inlay)) => { Some(Transform::Inlay(inlay)) => {
let suffix_start = inlay.text.point_to_offset(overshoot); let suffix_start = overshoot;
let suffix_end = inlay.text.point_to_offset( let suffix_end = cmp::min(cursor.end(&()).0, range.end).0 - cursor.start().0 .0;
cmp::min(cursor.end(&()).0, range.end).0 - cursor.start().0 .0,
);
summary = inlay.text.cursor(suffix_start).summary(suffix_end); summary = inlay.text.cursor(suffix_start).summary(suffix_end);
cursor.next(&()); cursor.next(&());
} }
@ -671,10 +698,10 @@ impl InlaySnapshot {
let prefix_end = prefix_start + overshoot; let prefix_end = prefix_start + overshoot;
summary += self summary += self
.buffer .buffer
.text_summary_for_range::<TextSummary, Point>(prefix_start..prefix_end); .text_summary_for_range::<TextSummary, _>(prefix_start..prefix_end);
} }
Some(Transform::Inlay(inlay)) => { Some(Transform::Inlay(inlay)) => {
let prefix_end = inlay.text.point_to_offset(overshoot); let prefix_end = overshoot;
summary += inlay.text.cursor(0).summary::<TextSummary>(prefix_end); summary += inlay.text.cursor(0).summary::<TextSummary>(prefix_end);
} }
None => {} None => {}
@ -689,6 +716,7 @@ impl InlaySnapshot {
let inlay_point = InlayPoint::new(row, 0); let inlay_point = InlayPoint::new(row, 0);
cursor.seek(&inlay_point, Bias::Left, &()); cursor.seek(&inlay_point, Bias::Left, &());
let max_buffer_row = self.buffer.max_point().row;
let mut buffer_point = cursor.start().1; let mut buffer_point = cursor.start().1;
let buffer_row = if row == 0 { let buffer_row = if row == 0 {
0 0
@ -698,7 +726,7 @@ impl InlaySnapshot {
buffer_point += inlay_point.0 - cursor.start().0 .0; buffer_point += inlay_point.0 - cursor.start().0 .0;
buffer_point.row buffer_point.row
} }
_ => cmp::min(buffer_point.row + 1, self.buffer.max_point().row), _ => cmp::min(buffer_point.row + 1, max_buffer_row),
} }
}; };
@ -706,6 +734,7 @@ impl InlaySnapshot {
transforms: cursor, transforms: cursor,
inlay_row: inlay_point.row(), inlay_row: inlay_point.row(),
buffer_rows: self.buffer.buffer_rows(buffer_row), buffer_rows: self.buffer.buffer_rows(buffer_row),
max_buffer_row,
} }
} }
@ -1049,10 +1078,8 @@ mod tests {
start..end start..end
); );
let start_point = InlayPoint(expected_text.offset_to_point(start));
let end_point = InlayPoint(expected_text.offset_to_point(end));
assert_eq!( assert_eq!(
inlay_snapshot.text_summary_for_range(start_point..end_point), inlay_snapshot.text_summary_for_range(start..end),
expected_text.slice(start..end).summary() expected_text.slice(start..end).summary()
); );
} }