Don't return Results from position methods in FoldMap and DisplayMap

This commit is contained in:
Max Brunsfeld
2021-05-20 12:47:08 +02:00
committed by Antonio Scandurra
parent 72b98ad688
commit b3d2a70834
6 changed files with 207 additions and 403 deletions
+40 -65
View File
@@ -7,7 +7,6 @@ use crate::{
sum_tree::{self, Cursor, FilterCursor, SeekBias, SumTree},
time,
};
use anyhow::{anyhow, Result};
use gpui::{AppContext, ModelHandle};
use parking_lot::{Mutex, MutexGuard};
use std::{
@@ -55,17 +54,14 @@ impl FoldMap {
self.sync(ctx).summary().display.bytes
}
pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
let line_start = self.to_display_offset(DisplayPoint::new(row, 0), ctx)?.0;
pub fn line_len(&self, row: u32, ctx: &AppContext) -> u32 {
let line_start = self.to_display_offset(DisplayPoint::new(row, 0), ctx).0;
let line_end = if row >= self.max_point(ctx).row() {
self.len(ctx)
} else {
self.to_display_offset(DisplayPoint::new(row + 1, 0), ctx)?
.0
- 1
self.to_display_offset(DisplayPoint::new(row + 1, 0), ctx).0 - 1
};
Ok((line_end - line_start) as u32)
(line_end - line_start) as u32
}
pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint {
@@ -80,18 +76,18 @@ impl FoldMap {
&'a self,
range: Range<T>,
ctx: &'a AppContext,
) -> Result<impl Iterator<Item = &'a Range<Anchor>>>
) -> impl Iterator<Item = &'a Range<Anchor>>
where
T: ToOffset,
{
Ok(self.intersecting_folds(range, ctx)?.map(|f| &f.0))
self.intersecting_folds(range, ctx).map(|f| &f.0)
}
pub fn fold<T: ToOffset>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &AppContext,
) -> Result<()> {
) {
let _ = self.sync(ctx);
let mut edits = Vec::new();
@@ -128,14 +124,13 @@ impl FoldMap {
new_tree
};
self.apply_edits(edits, ctx);
Ok(())
}
pub fn unfold<T: ToOffset>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &AppContext,
) -> Result<()> {
) {
let _ = self.sync(ctx);
let buffer = self.buffer.read(ctx);
@@ -144,7 +139,7 @@ impl FoldMap {
let mut fold_ixs_to_delete = Vec::new();
for range in ranges.into_iter() {
// Remove intersecting folds and add their ranges to edits that are passed to apply_edits.
let mut folds_cursor = self.intersecting_folds(range, ctx)?;
let mut folds_cursor = self.intersecting_folds(range, ctx);
while let Some(fold) = folds_cursor.item() {
let offset_range = fold.0.start.to_offset(buffer)..fold.0.end.to_offset(buffer);
edits.push(Edit {
@@ -176,24 +171,23 @@ impl FoldMap {
folds
};
self.apply_edits(edits, ctx);
Ok(())
}
fn intersecting_folds<'a, T>(
&self,
range: Range<T>,
ctx: &'a AppContext,
) -> Result<FilterCursor<impl 'a + Fn(&FoldSummary) -> bool, Fold, usize>>
) -> FilterCursor<impl 'a + Fn(&FoldSummary) -> bool, Fold, usize>
where
T: ToOffset,
{
let buffer = self.buffer.read(ctx);
let start = buffer.anchor_before(range.start.to_offset(buffer));
let end = buffer.anchor_after(range.end.to_offset(buffer));
Ok(self.folds.filter::<_, usize>(move |summary| {
self.folds.filter::<_, usize>(move |summary| {
start.cmp(&summary.max_end, buffer).unwrap() == Ordering::Less
&& end.cmp(&summary.min_start, buffer).unwrap() == Ordering::Greater
}))
})
}
pub fn is_line_folded(&self, display_row: u32, ctx: &AppContext) -> bool {
@@ -221,11 +215,7 @@ impl FoldMap {
(cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx))
}
pub fn to_display_offset(
&self,
point: DisplayPoint,
ctx: &AppContext,
) -> Result<DisplayOffset> {
pub fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset {
self.snapshot(ctx).to_display_offset(point, ctx)
}
@@ -405,48 +395,46 @@ pub struct FoldMapSnapshot {
}
impl FoldMapSnapshot {
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
pub fn buffer_rows(&self, start_row: u32) -> BufferRows {
if start_row > self.transforms.summary().display.lines.row {
return Err(anyhow!("invalid display row {}", start_row));
panic!("invalid display row {}", start_row);
}
let display_point = Point::new(start_row, 0);
let mut cursor = self.transforms.cursor();
cursor.seek(&DisplayPoint(display_point), SeekBias::Left, &());
Ok(BufferRows {
BufferRows {
display_point,
cursor,
})
}
}
pub fn chars_at<'a>(&'a self, point: DisplayPoint, ctx: &'a AppContext) -> Result<Chars<'a>> {
let offset = self.to_display_offset(point, ctx)?;
pub fn chars_at<'a>(&'a self, point: DisplayPoint, ctx: &'a AppContext) -> Chars<'a> {
let offset = self.to_display_offset(point, ctx);
let mut cursor = self.transforms.cursor();
cursor.seek(&offset, SeekBias::Right, &());
Ok(Chars {
Chars {
cursor,
offset: offset.0,
buffer: self.buffer.read(ctx),
buffer_chars: None,
})
}
}
fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> Result<DisplayOffset> {
fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&point, SeekBias::Right, &());
let overshoot = point.0 - cursor.start().display.lines;
let mut offset = cursor.start().display.bytes;
if !overshoot.is_zero() {
let transform = cursor
.item()
.ok_or_else(|| anyhow!("display point {:?} is out of range", point))?;
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));
offset += end_buffer_offset - cursor.start().buffer.bytes;
}
Ok(DisplayOffset(offset))
DisplayOffset(offset)
}
}
@@ -680,8 +668,7 @@ mod tests {
Point::new(2, 4)..Point::new(4, 1),
],
app.as_ref(),
)
.unwrap();
);
assert_eq!(map.text(app.as_ref()), "aa…cc…eeeee");
buffer.update(app, |buffer, ctx| {
@@ -707,8 +694,7 @@ mod tests {
});
assert_eq!(map.text(app.as_ref()), "123a…c123456eee");
map.unfold(Some(Point::new(0, 4)..Point::new(0, 5)), app.as_ref())
.unwrap();
map.unfold(Some(Point::new(0, 4)..Point::new(0, 5)), app.as_ref());
assert_eq!(map.text(app.as_ref()), "123aaaaa\nbbbbbb\nccc123456eee");
}
@@ -719,17 +705,17 @@ mod tests {
{
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
map.fold(vec![5..8], app.as_ref()).unwrap();
map.fold(vec![5..8], app.as_ref());
map.check_invariants(app.as_ref());
assert_eq!(map.text(app.as_ref()), "abcde…ijkl");
// Create an fold adjacent to the start of the first fold.
map.fold(vec![0..1, 2..5], app.as_ref()).unwrap();
map.fold(vec![0..1, 2..5], app.as_ref());
map.check_invariants(app.as_ref());
assert_eq!(map.text(app.as_ref()), "…b…ijkl");
// Create an fold adjacent to the end of the first fold.
map.fold(vec![11..11, 8..10], app.as_ref()).unwrap();
map.fold(vec![11..11, 8..10], app.as_ref());
map.check_invariants(app.as_ref());
assert_eq!(map.text(app.as_ref()), "…b…kl");
}
@@ -738,7 +724,7 @@ mod tests {
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
// Create two adjacent folds.
map.fold(vec![0..2, 2..5], app.as_ref()).unwrap();
map.fold(vec![0..2, 2..5], app.as_ref());
map.check_invariants(app.as_ref());
assert_eq!(map.text(app.as_ref()), "…fghijkl");
@@ -765,8 +751,7 @@ mod tests {
Point::new(3, 1)..Point::new(4, 1),
],
app.as_ref(),
)
.unwrap();
);
assert_eq!(map.text(app.as_ref()), "aa…eeeee");
}
@@ -781,8 +766,7 @@ mod tests {
Point::new(3, 1)..Point::new(4, 1),
],
app.as_ref(),
)
.unwrap();
);
assert_eq!(map.text(app.as_ref()), "aa…cccc\nd…eeeee");
buffer.update(app, |buffer, ctx| {
@@ -807,11 +791,9 @@ mod tests {
Point::new(3, 1)..Point::new(4, 1),
],
app.as_ref(),
)
.unwrap();
);
let fold_ranges = map
.folds_in_range(Point::new(1, 0)..Point::new(1, 3), app.as_ref())
.unwrap()
.map(|fold| fold.start.to_point(buffer)..fold.end.to_point(buffer))
.collect::<Vec<_>>();
assert_eq!(
@@ -866,7 +848,7 @@ mod tests {
to_fold.push(start..end);
}
log::info!("folding {:?}", to_fold);
map.fold(to_fold, app.as_ref()).unwrap();
map.fold(to_fold, app.as_ref());
}
35..=59 if !map.folds.is_empty() => {
let buffer = buffer.read(app);
@@ -877,7 +859,7 @@ mod tests {
to_unfold.push(start..end);
}
log::info!("unfolding {:?}", to_unfold);
map.unfold(to_unfold, app.as_ref()).unwrap();
map.unfold(to_unfold, app.as_ref());
}
_ => {
let edits = buffer.update(app, |buffer, ctx| {
@@ -909,7 +891,7 @@ mod tests {
assert_eq!(map.text(app.as_ref()), expected_text);
for (display_row, line) in expected_text.lines().enumerate() {
let line_len = map.line_len(display_row as u32, app.as_ref()).unwrap();
let line_len = map.line_len(display_row as u32, app.as_ref());
assert_eq!(line_len, line.chars().count() as u32);
}
@@ -928,7 +910,7 @@ mod tests {
buffer_offset
);
assert_eq!(
map.to_display_offset(display_point, app.as_ref()).unwrap(),
map.to_display_offset(display_point, app.as_ref()),
display_offset
);
@@ -949,14 +931,13 @@ mod tests {
for _ in 0..5 {
let row = rng.gen_range(0..=map.max_point(app.as_ref()).row());
let column = rng.gen_range(0..=map.line_len(row, app.as_ref()).unwrap());
let column = rng.gen_range(0..=map.line_len(row, app.as_ref()));
let point = DisplayPoint::new(row, column);
let offset = map.to_display_offset(point, app.as_ref()).unwrap().0;
let offset = map.to_display_offset(point, app.as_ref()).0;
let len = rng.gen_range(0..=map.len(app.as_ref()) - offset);
assert_eq!(
map.snapshot(app.as_ref())
.chars_at(point, app.as_ref())
.unwrap()
.take(len)
.collect::<String>(),
expected_text
@@ -974,7 +955,6 @@ mod tests {
assert_eq!(
map.snapshot(app.as_ref())
.buffer_rows(display_row)
.unwrap()
.collect::<Vec<_>>(),
expected_buffer_rows[idx..],
);
@@ -1004,7 +984,6 @@ mod tests {
assert_eq!(
map.folds_in_range(start..end, app.as_ref())
.unwrap()
.cloned()
.collect::<Vec<_>>(),
expected_folds
@@ -1027,21 +1006,18 @@ mod tests {
Point::new(3, 1)..Point::new(4, 1),
],
app.as_ref(),
)
.unwrap();
);
assert_eq!(map.text(app.as_ref()), "aa…cccc\nd…eeeee\nffffff\n");
assert_eq!(
map.snapshot(app.as_ref())
.buffer_rows(0)
.unwrap()
.collect::<Vec<_>>(),
vec![0, 3, 5, 6]
);
assert_eq!(
map.snapshot(app.as_ref())
.buffer_rows(3)
.unwrap()
.collect::<Vec<_>>(),
vec![6]
);
@@ -1051,7 +1027,6 @@ mod tests {
fn text(&self, app: &AppContext) -> String {
self.snapshot(app)
.chars_at(DisplayPoint(Point::zero()), app)
.unwrap()
.collect()
}
+44 -70
View File
@@ -1,7 +1,6 @@
mod fold_map;
use super::{buffer, Anchor, Buffer, Edit, Point, ToOffset, ToPoint};
use anyhow::Result;
pub use fold_map::BufferRows;
use fold_map::{FoldMap, FoldMapSnapshot};
use gpui::{AppContext, ModelHandle};
@@ -39,7 +38,7 @@ impl DisplayMap {
&'a self,
range: Range<T>,
app: &'a AppContext,
) -> Result<impl Iterator<Item = &'a Range<Anchor>>>
) -> impl Iterator<Item = &'a Range<Anchor>>
where
T: ToOffset,
{
@@ -50,7 +49,7 @@ impl DisplayMap {
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &AppContext,
) -> Result<()> {
) {
self.fold_map.fold(ranges, ctx)
}
@@ -58,7 +57,7 @@ impl DisplayMap {
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &AppContext,
) -> Result<()> {
) {
self.fold_map.unfold(ranges, ctx)
}
@@ -69,24 +68,22 @@ impl DisplayMap {
pub fn text(&self, ctx: &AppContext) -> String {
self.snapshot(ctx)
.chars_at(DisplayPoint::zero(), ctx)
.unwrap()
.collect()
}
pub fn line(&self, display_row: u32, ctx: &AppContext) -> Result<String> {
Ok(self
.snapshot(ctx)
.chars_at(DisplayPoint::new(display_row, 0), ctx)?
pub fn line(&self, display_row: u32, ctx: &AppContext) -> String {
self.snapshot(ctx)
.chars_at(DisplayPoint::new(display_row, 0), ctx)
.take_while(|c| *c != '\n')
.collect())
.collect()
}
pub fn line_indent(&self, display_row: u32, ctx: &AppContext) -> Result<(u32, bool)> {
pub fn line_indent(&self, display_row: u32, ctx: &AppContext) -> (u32, bool) {
let mut indent = 0;
let mut is_blank = true;
for c in self
.snapshot(ctx)
.chars_at(DisplayPoint::new(display_row, 0), ctx)?
.chars_at(DisplayPoint::new(display_row, 0), ctx)
{
if c == ' ' {
indent += 1;
@@ -95,45 +92,33 @@ impl DisplayMap {
break;
}
}
Ok((indent, is_blank))
(indent, is_blank)
}
pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
DisplayPoint::new(row, self.fold_map.line_len(row, ctx)?)
pub fn line_len(&self, row: u32, ctx: &AppContext) -> u32 {
DisplayPoint::new(row, self.fold_map.line_len(row, ctx))
.expand_tabs(self, ctx)
.map(|point| point.column())
.column()
}
pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint {
self.fold_map.max_point(ctx).expand_tabs(self, ctx).unwrap()
self.fold_map.max_point(ctx).expand_tabs(self, ctx)
}
pub fn rightmost_point(&self, ctx: &AppContext) -> DisplayPoint {
self.fold_map.rightmost_point(ctx)
}
pub fn anchor_before(
&self,
point: DisplayPoint,
bias: Bias,
app: &AppContext,
) -> Result<Anchor> {
Ok(self
.buffer
pub fn anchor_before(&self, point: DisplayPoint, bias: Bias, app: &AppContext) -> Anchor {
self.buffer
.read(app)
.anchor_before(point.to_buffer_point(self, bias, app)?))
.anchor_before(point.to_buffer_point(self, bias, app))
}
pub fn anchor_after(
&self,
point: DisplayPoint,
bias: Bias,
app: &AppContext,
) -> Result<Anchor> {
Ok(self
.buffer
pub fn anchor_after(&self, point: DisplayPoint, bias: Bias, app: &AppContext) -> Anchor {
self.buffer
.read(app)
.anchor_after(point.to_buffer_point(self, bias, app)?))
.anchor_after(point.to_buffer_point(self, bias, app))
}
}
@@ -143,33 +128,32 @@ pub struct DisplayMapSnapshot {
}
impl DisplayMapSnapshot {
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
pub fn buffer_rows(&self, start_row: u32) -> BufferRows {
self.folds_snapshot.buffer_rows(start_row)
}
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result<Chars<'a>> {
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Chars<'a> {
let column = point.column() as usize;
let (point, to_next_stop) = self.collapse_tabs(point, Bias::Left, app)?;
let mut fold_chars = self.folds_snapshot.chars_at(point, app)?;
let (point, to_next_stop) = self.collapse_tabs(point, Bias::Left, app);
let mut fold_chars = self.folds_snapshot.chars_at(point, app);
if to_next_stop > 0 {
fold_chars.next();
}
Ok(Chars {
Chars {
fold_chars,
column,
to_next_stop,
tab_size: self.tab_size,
})
}
}
fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> Result<DisplayPoint> {
fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> DisplayPoint {
let chars = self
.folds_snapshot
.chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx)?;
.chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx);
let expanded = expand_tabs(chars, point.column() as usize, self.tab_size);
*point.column_mut() = expanded as u32;
Ok(point)
point
}
fn collapse_tabs(
@@ -177,15 +161,15 @@ impl DisplayMapSnapshot {
mut point: DisplayPoint,
bias: Bias,
ctx: &AppContext,
) -> Result<(DisplayPoint, usize)> {
) -> (DisplayPoint, usize) {
let chars = self
.folds_snapshot
.chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx)?;
.chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx);
let expanded = point.column() as usize;
let (collapsed, to_next_stop) = collapse_tabs(chars, expanded, bias, self.tab_size);
*point.column_mut() = collapsed as u32;
Ok((point, to_next_stop))
(point, to_next_stop)
}
}
@@ -217,45 +201,38 @@ impl DisplayPoint {
&mut self.0.column
}
pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result<Point> {
Ok(map
.fold_map
.to_buffer_point(self.collapse_tabs(map, bias, ctx)?.0, ctx))
pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Point {
map.fold_map
.to_buffer_point(self.collapse_tabs(map, bias, ctx).0, ctx)
}
pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result<usize> {
Ok(map
.fold_map
.to_buffer_offset(self.collapse_tabs(&map, bias, ctx)?.0, ctx))
pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> usize {
map.fold_map
.to_buffer_offset(self.collapse_tabs(&map, bias, ctx).0, ctx)
}
fn expand_tabs(self, map: &DisplayMap, ctx: &AppContext) -> Result<Self> {
fn expand_tabs(self, map: &DisplayMap, ctx: &AppContext) -> Self {
map.snapshot(ctx).expand_tabs(self, ctx)
}
fn collapse_tabs(
self,
map: &DisplayMap,
bias: Bias,
ctx: &AppContext,
) -> Result<(Self, usize)> {
fn collapse_tabs(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> (Self, usize) {
map.snapshot(ctx).collapse_tabs(self, bias, ctx)
}
}
impl Point {
pub fn to_display_point(self, map: &DisplayMap, ctx: &AppContext) -> Result<DisplayPoint> {
pub fn to_display_point(self, map: &DisplayMap, ctx: &AppContext) -> DisplayPoint {
let mut display_point = map.fold_map.to_display_point(self, ctx);
let snapshot = map.fold_map.snapshot(ctx);
let chars = snapshot.chars_at(DisplayPoint::new(display_point.row(), 0), ctx)?;
let chars = snapshot.chars_at(DisplayPoint::new(display_point.row(), 0), ctx);
*display_point.column_mut() =
expand_tabs(chars, display_point.column() as usize, map.tab_size) as u32;
Ok(display_point)
display_point
}
}
impl Anchor {
pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> Result<DisplayPoint> {
pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> DisplayPoint {
self.to_point(map.buffer.read(app))
.to_display_point(map, app)
}
@@ -365,7 +342,6 @@ mod tests {
assert_eq!(
map.snapshot(app.as_ref())
.chars_at(DisplayPoint::new(1, 0), app.as_ref())
.unwrap()
.take(10)
.collect::<String>(),
" b bb"
@@ -373,7 +349,6 @@ mod tests {
assert_eq!(
map.snapshot(app.as_ref())
.chars_at(DisplayPoint::new(1, 2), app.as_ref())
.unwrap()
.take(10)
.collect::<String>(),
" b bbbb"
@@ -381,7 +356,6 @@ mod tests {
assert_eq!(
map.snapshot(app.as_ref())
.chars_at(DisplayPoint::new(1, 6), app.as_ref())
.unwrap()
.take(13)
.collect::<String>(),
" bbbbb\nc c"