Move highlighted_text_in_range from Buffer to buffer::Snapshot

This commit is contained in:
Antonio Scandurra
2021-05-25 11:51:29 +02:00
parent e54a31ead2
commit aeb68867da
6 changed files with 182 additions and 200 deletions
+26 -41
View File
@@ -46,7 +46,7 @@ impl FoldMap {
pub fn snapshot(&self, ctx: &AppContext) -> FoldMapSnapshot {
FoldMapSnapshot {
transforms: self.sync(ctx).clone(),
buffer: self.buffer.clone(),
buffer: self.buffer.read(ctx).snapshot(),
}
}
@@ -210,11 +210,11 @@ impl FoldMap {
}
pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> usize {
self.snapshot(ctx).to_buffer_offset(point, ctx)
self.snapshot(ctx).to_buffer_offset(point)
}
pub fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset {
self.snapshot(ctx).to_display_offset(point, ctx)
self.snapshot(ctx).to_display_offset(point)
}
pub fn to_buffer_point(&self, display_point: DisplayPoint, ctx: &AppContext) -> Point {
@@ -394,7 +394,7 @@ impl FoldMap {
pub struct FoldMapSnapshot {
transforms: SumTree<Transform>,
buffer: ModelHandle<Buffer>,
buffer: buffer::Snapshot,
}
impl FoldMapSnapshot {
@@ -413,13 +413,12 @@ impl FoldMapSnapshot {
}
}
pub fn chunks_at<'a>(&'a self, offset: DisplayOffset, ctx: &'a AppContext) -> Chunks<'a> {
pub fn chunks_at(&self, offset: DisplayOffset) -> Chunks {
let mut transform_cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
transform_cursor.seek(&offset, SeekBias::Right, &());
let overshoot = offset.0 - transform_cursor.start().display.bytes;
let buffer_offset = transform_cursor.start().buffer.bytes + overshoot;
let buffer = self.buffer.read(ctx);
let rope_cursor = buffer.text_for_range(buffer_offset..buffer.len());
let rope_cursor = self.buffer.text_for_range(buffer_offset..self.buffer.len());
Chunks {
transform_cursor,
buffer_offset,
@@ -427,34 +426,27 @@ impl FoldMapSnapshot {
}
}
pub fn highlighted_chunks_at<'a>(
&'a self,
offset: DisplayOffset,
ctx: &'a AppContext,
) -> HighlightedChunks<'a> {
pub fn highlighted_chunks_at(&mut self, offset: DisplayOffset) -> HighlightedChunks {
let mut transform_cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
transform_cursor.seek(&offset, SeekBias::Right, &());
let overshoot = offset.0 - transform_cursor.start().display.bytes;
let buffer_offset = transform_cursor.start().buffer.bytes + overshoot;
let buffer = self.buffer.read(ctx);
HighlightedChunks {
transform_cursor,
buffer_offset,
buffer_chunks: buffer.highlighted_text_for_range(buffer_offset..buffer.len()),
buffer_chunks: self
.buffer
.highlighted_text_for_range(buffer_offset..self.buffer.len()),
buffer_chunk: None,
}
}
pub fn chars_at<'a>(
&'a self,
point: DisplayPoint,
ctx: &'a AppContext,
) -> impl Iterator<Item = char> + 'a {
let offset = self.to_display_offset(point, ctx);
self.chunks_at(offset, ctx).flat_map(str::chars)
pub fn chars_at<'a>(&'a self, point: DisplayPoint) -> impl Iterator<Item = char> + 'a {
let offset = self.to_display_offset(point);
self.chunks_at(offset).flat_map(str::chars)
}
pub fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset {
pub fn to_display_offset(&self, point: DisplayPoint) -> DisplayOffset {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&point, SeekBias::Right, &());
let overshoot = point.0 - cursor.start().display.lines;
@@ -462,27 +454,24 @@ impl FoldMapSnapshot {
if !overshoot.is_zero() {
let transform = cursor.item().expect("display point out of range");
assert!(transform.display_text.is_none());
let end_buffer_offset =
(cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx));
let end_buffer_offset = self
.buffer
.to_offset(cursor.start().buffer.lines + overshoot);
offset += end_buffer_offset - cursor.start().buffer.bytes;
}
DisplayOffset(offset)
}
pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> usize {
pub fn to_buffer_offset(&self, point: DisplayPoint) -> usize {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&point, SeekBias::Right, &());
let overshoot = point.0 - cursor.start().display.lines;
(cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx))
self.buffer
.to_offset(cursor.start().buffer.lines + overshoot)
}
#[cfg(test)]
pub fn clip_offset(
&self,
offset: DisplayOffset,
bias: Bias,
ctx: &AppContext,
) -> DisplayOffset {
pub fn clip_offset(&self, offset: DisplayOffset, bias: Bias) -> DisplayOffset {
let mut cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
cursor.seek(&offset, SeekBias::Right, &());
if let Some(transform) = cursor.item() {
@@ -496,7 +485,7 @@ impl FoldMapSnapshot {
} else {
let overshoot = offset.0 - transform_start;
let buffer_offset = cursor.start().buffer.bytes + overshoot;
let clipped_buffer_offset = self.buffer.read(ctx).clip_offset(buffer_offset, bias);
let clipped_buffer_offset = self.buffer.clip_offset(buffer_offset, bias);
DisplayOffset(
(offset.0 as isize + (clipped_buffer_offset as isize - buffer_offset as isize))
as usize,
@@ -507,7 +496,7 @@ impl FoldMapSnapshot {
}
}
pub fn clip_point(&self, point: DisplayPoint, bias: Bias, ctx: &AppContext) -> DisplayPoint {
pub fn clip_point(&self, point: DisplayPoint, bias: Bias) -> DisplayPoint {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&point, SeekBias::Right, &());
if let Some(transform) = cursor.item() {
@@ -521,8 +510,7 @@ impl FoldMapSnapshot {
} else {
let overshoot = point.0 - transform_start;
let buffer_position = cursor.start().buffer.lines + overshoot;
let clipped_buffer_position =
self.buffer.read(ctx).clip_point(buffer_position, bias);
let clipped_buffer_position = self.buffer.clip_point(buffer_position, bias);
DisplayPoint::new(
point.row(),
((point.column() as i32) + clipped_buffer_position.column as i32
@@ -1131,11 +1119,10 @@ mod tests {
let offset = map.snapshot(app.as_ref()).clip_offset(
DisplayOffset(rng.gen_range(0..=map.len(app.as_ref()))),
Bias::Right,
app.as_ref(),
);
assert_eq!(
map.snapshot(app.as_ref())
.chunks_at(offset, app.as_ref())
.chunks_at(offset)
.collect::<String>(),
&expected_text[offset.0..],
);
@@ -1218,9 +1205,7 @@ mod tests {
impl FoldMap {
fn text(&self, app: &AppContext) -> String {
self.snapshot(app)
.chunks_at(DisplayOffset(0), app)
.collect()
self.snapshot(app).chunks_at(DisplayOffset(0)).collect()
}
fn merged_fold_ranges(&self, app: &AppContext) -> Vec<Range<usize>> {