Merge branch 'master' into new-file
This commit is contained in:
@@ -678,9 +678,8 @@ impl Buffer {
|
||||
if let Some(ctx) = ctx {
|
||||
ctx.notify();
|
||||
|
||||
let changes = self.edits_since(since).collect::<Vec<_>>();
|
||||
if !changes.is_empty() {
|
||||
self.did_edit(changes, was_dirty, ctx);
|
||||
if self.edits_since(since).next().is_some() {
|
||||
self.did_edit(was_dirty, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -741,8 +740,8 @@ impl Buffer {
|
||||
Ok(ops)
|
||||
}
|
||||
|
||||
fn did_edit(&self, changes: Vec<Edit>, was_dirty: bool, ctx: &mut ModelContext<Self>) {
|
||||
ctx.emit(Event::Edited(changes));
|
||||
fn did_edit(&self, was_dirty: bool, ctx: &mut ModelContext<Self>) {
|
||||
ctx.emit(Event::Edited);
|
||||
if !was_dirty {
|
||||
ctx.emit(Event::Dirtied);
|
||||
}
|
||||
@@ -891,9 +890,8 @@ impl Buffer {
|
||||
|
||||
if let Some(ctx) = ctx {
|
||||
ctx.notify();
|
||||
let changes = self.edits_since(old_version).collect::<Vec<_>>();
|
||||
if !changes.is_empty() {
|
||||
self.did_edit(changes, was_dirty, ctx);
|
||||
if self.edits_since(old_version).next().is_some() {
|
||||
self.did_edit(was_dirty, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1085,9 +1083,8 @@ impl Buffer {
|
||||
|
||||
if let Some(ctx) = ctx {
|
||||
ctx.notify();
|
||||
let changes = self.edits_since(old_version).collect::<Vec<_>>();
|
||||
if !changes.is_empty() {
|
||||
self.did_edit(changes, was_dirty, ctx);
|
||||
if self.edits_since(old_version).next().is_some() {
|
||||
self.did_edit(was_dirty, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1112,9 +1109,8 @@ impl Buffer {
|
||||
|
||||
if let Some(ctx) = ctx {
|
||||
ctx.notify();
|
||||
let changes = self.edits_since(old_version).collect::<Vec<_>>();
|
||||
if !changes.is_empty() {
|
||||
self.did_edit(changes, was_dirty, ctx);
|
||||
if self.edits_since(old_version).next().is_some() {
|
||||
self.did_edit(was_dirty, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1793,7 +1789,7 @@ impl Snapshot {
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum Event {
|
||||
Edited(Vec<Edit>),
|
||||
Edited,
|
||||
Dirtied,
|
||||
Saved,
|
||||
FileHandleChanged,
|
||||
@@ -2402,34 +2398,11 @@ mod tests {
|
||||
let buffer_1_events = buffer_1_events.borrow();
|
||||
assert_eq!(
|
||||
*buffer_1_events,
|
||||
vec![
|
||||
Event::Edited(vec![Edit {
|
||||
old_range: 2..4,
|
||||
new_range: 2..5
|
||||
}]),
|
||||
Event::Dirtied,
|
||||
Event::Edited(vec![Edit {
|
||||
old_range: 5..5,
|
||||
new_range: 5..7
|
||||
}]),
|
||||
Event::Edited(vec![Edit {
|
||||
old_range: 5..7,
|
||||
new_range: 5..5
|
||||
}]),
|
||||
]
|
||||
vec![Event::Edited, Event::Dirtied, Event::Edited, Event::Edited]
|
||||
);
|
||||
|
||||
let buffer_2_events = buffer_2_events.borrow();
|
||||
assert_eq!(
|
||||
*buffer_2_events,
|
||||
vec![
|
||||
Event::Edited(vec![Edit {
|
||||
old_range: 2..4,
|
||||
new_range: 2..5
|
||||
},]),
|
||||
Event::Dirtied
|
||||
]
|
||||
);
|
||||
assert_eq!(*buffer_2_events, vec![Event::Edited, Event::Dirtied]);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2969,16 +2942,7 @@ mod tests {
|
||||
model.update(app, |buffer, ctx| {
|
||||
assert!(buffer.text() == "ac");
|
||||
assert!(buffer.is_dirty());
|
||||
assert_eq!(
|
||||
*events.borrow(),
|
||||
&[
|
||||
Event::Edited(vec![Edit {
|
||||
old_range: 1..2,
|
||||
new_range: 1..1
|
||||
}]),
|
||||
Event::Dirtied
|
||||
]
|
||||
);
|
||||
assert_eq!(*events.borrow(), &[Event::Edited, Event::Dirtied]);
|
||||
events.borrow_mut().clear();
|
||||
|
||||
buffer.did_save(buffer.version(), None, ctx);
|
||||
@@ -3000,17 +2964,7 @@ mod tests {
|
||||
assert!(buffer.is_dirty());
|
||||
assert_eq!(
|
||||
*events.borrow(),
|
||||
&[
|
||||
Event::Edited(vec![Edit {
|
||||
old_range: 1..1,
|
||||
new_range: 1..2
|
||||
}]),
|
||||
Event::Dirtied,
|
||||
Event::Edited(vec![Edit {
|
||||
old_range: 2..2,
|
||||
new_range: 2..3
|
||||
}]),
|
||||
],
|
||||
&[Event::Edited, Event::Dirtied, Event::Edited],
|
||||
);
|
||||
events.borrow_mut().clear();
|
||||
|
||||
@@ -3022,13 +2976,7 @@ mod tests {
|
||||
});
|
||||
|
||||
model.update(app, |_, _| {
|
||||
assert_eq!(
|
||||
*events.borrow(),
|
||||
&[Event::Edited(vec![Edit {
|
||||
old_range: 1..3,
|
||||
new_range: 1..1
|
||||
},])]
|
||||
);
|
||||
assert_eq!(*events.borrow(), &[Event::Edited]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,10 +3,12 @@ use super::{
|
||||
};
|
||||
use crate::{
|
||||
sum_tree::{self, Cursor, SumTree},
|
||||
time,
|
||||
util::find_insertion_index,
|
||||
};
|
||||
use anyhow::{anyhow, Result};
|
||||
use gpui::{AppContext, ModelHandle};
|
||||
use parking_lot::{Mutex, MutexGuard};
|
||||
use std::{
|
||||
cmp::{self, Ordering},
|
||||
iter::Take,
|
||||
@@ -16,49 +18,44 @@ use sum_tree::{Dimension, SeekBias};
|
||||
|
||||
pub struct FoldMap {
|
||||
buffer: ModelHandle<Buffer>,
|
||||
transforms: SumTree<Transform>,
|
||||
transforms: Mutex<SumTree<Transform>>,
|
||||
folds: Vec<Range<Anchor>>,
|
||||
last_sync: Mutex<time::Global>,
|
||||
}
|
||||
|
||||
impl FoldMap {
|
||||
pub fn new(buffer: ModelHandle<Buffer>, app: &AppContext) -> Self {
|
||||
let text_summary = buffer.read(app).text_summary();
|
||||
pub fn new(buffer_handle: ModelHandle<Buffer>, ctx: &AppContext) -> Self {
|
||||
let buffer = buffer_handle.read(ctx);
|
||||
let text_summary = buffer.text_summary();
|
||||
Self {
|
||||
buffer,
|
||||
buffer: buffer_handle,
|
||||
folds: Vec::new(),
|
||||
transforms: SumTree::from_item(Transform {
|
||||
transforms: Mutex::new(SumTree::from_item(Transform {
|
||||
summary: TransformSummary {
|
||||
buffer: text_summary.clone(),
|
||||
display: text_summary,
|
||||
},
|
||||
display_text: None,
|
||||
}),
|
||||
})),
|
||||
last_sync: Mutex::new(buffer.version()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
|
||||
if start_row > self.transforms.summary().display.lines.row {
|
||||
return Err(anyhow!("invalid display row {}", start_row));
|
||||
pub fn snapshot(&self, ctx: &AppContext) -> FoldMapSnapshot {
|
||||
FoldMapSnapshot {
|
||||
transforms: self.sync(ctx).clone(),
|
||||
buffer: self.buffer.clone(),
|
||||
}
|
||||
|
||||
let display_point = Point::new(start_row, 0);
|
||||
let mut cursor = self.transforms.cursor();
|
||||
cursor.seek(&DisplayPoint(display_point), SeekBias::Left);
|
||||
|
||||
Ok(BufferRows {
|
||||
display_point,
|
||||
cursor,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.transforms.summary().display.chars
|
||||
pub fn len(&self, ctx: &AppContext) -> usize {
|
||||
self.sync(ctx).summary().display.chars
|
||||
}
|
||||
|
||||
pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
|
||||
let line_start = self.to_display_offset(DisplayPoint::new(row, 0), ctx)?.0;
|
||||
let line_end = if row >= self.max_point().row() {
|
||||
self.len()
|
||||
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
|
||||
@@ -68,61 +65,39 @@ impl FoldMap {
|
||||
Ok((line_end - line_start) as u32)
|
||||
}
|
||||
|
||||
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result<Chars<'a>> {
|
||||
let offset = self.to_display_offset(point, app)?;
|
||||
let mut cursor = self.transforms.cursor();
|
||||
cursor.seek(&offset, SeekBias::Right);
|
||||
let buffer = self.buffer.read(app);
|
||||
Ok(Chars {
|
||||
cursor,
|
||||
offset: offset.0,
|
||||
buffer,
|
||||
buffer_chars: None,
|
||||
})
|
||||
pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint {
|
||||
DisplayPoint(self.sync(ctx).summary().display.lines)
|
||||
}
|
||||
|
||||
pub fn max_point(&self) -> DisplayPoint {
|
||||
DisplayPoint(self.transforms.summary().display.lines)
|
||||
pub fn rightmost_point(&self, ctx: &AppContext) -> DisplayPoint {
|
||||
DisplayPoint(self.sync(ctx).summary().display.rightmost_point)
|
||||
}
|
||||
|
||||
pub fn rightmost_point(&self) -> DisplayPoint {
|
||||
DisplayPoint(self.transforms.summary().display.rightmost_point)
|
||||
}
|
||||
|
||||
pub fn folds_in_range<T>(&self, range: Range<T>, app: &AppContext) -> Result<&[Range<Anchor>]>
|
||||
pub fn folds_in_range<'a, T>(
|
||||
&'a self,
|
||||
range: Range<T>,
|
||||
app: &'a AppContext,
|
||||
) -> Result<impl Iterator<Item = &'a Range<Anchor>>>
|
||||
where
|
||||
T: ToOffset,
|
||||
{
|
||||
let buffer = self.buffer.read(app);
|
||||
let range = buffer.anchor_before(range.start)?..buffer.anchor_before(range.end)?;
|
||||
let mut start_ix = find_insertion_index(&self.folds, |probe| probe.cmp(&range, buffer))?;
|
||||
let mut end_ix = start_ix;
|
||||
|
||||
for fold in self.folds[..start_ix].iter().rev() {
|
||||
if fold.end.cmp(&range.start, buffer)? == Ordering::Greater {
|
||||
start_ix -= 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for fold in &self.folds[end_ix..] {
|
||||
if range.end.cmp(&fold.start, buffer)? == Ordering::Greater {
|
||||
end_ix += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(&self.folds[start_ix..end_ix])
|
||||
Ok(self.folds.iter().filter(move |fold| {
|
||||
range.start.cmp(&fold.end, buffer).unwrap() == Ordering::Less
|
||||
&& range.end.cmp(&fold.start, buffer).unwrap() == Ordering::Greater
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn fold<T: ToOffset>(
|
||||
&mut self,
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
app: &AppContext,
|
||||
ctx: &AppContext,
|
||||
) -> Result<()> {
|
||||
let _ = self.sync(ctx);
|
||||
|
||||
let mut edits = Vec::new();
|
||||
let buffer = self.buffer.read(app);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
for range in ranges.into_iter() {
|
||||
let start = range.start.to_offset(buffer)?;
|
||||
let end = range.end.to_offset(buffer)?;
|
||||
@@ -142,16 +117,18 @@ impl FoldMap {
|
||||
.then_with(|| b.old_range.end.cmp(&a.old_range.end))
|
||||
});
|
||||
|
||||
self.apply_edits(&edits, app)?;
|
||||
self.apply_edits(edits, ctx);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unfold<T: ToOffset>(
|
||||
&mut self,
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
app: &AppContext,
|
||||
ctx: &AppContext,
|
||||
) -> Result<()> {
|
||||
let buffer = self.buffer.read(app);
|
||||
let _ = self.sync(ctx);
|
||||
|
||||
let buffer = self.buffer.read(ctx);
|
||||
|
||||
let mut edits = Vec::new();
|
||||
for range in ranges.into_iter() {
|
||||
@@ -176,12 +153,13 @@ impl FoldMap {
|
||||
});
|
||||
}
|
||||
|
||||
self.apply_edits(&edits, app)?;
|
||||
self.apply_edits(edits, ctx);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_line_folded(&self, display_row: u32) -> bool {
|
||||
let mut cursor = self.transforms.cursor::<DisplayPoint, DisplayPoint>();
|
||||
pub fn is_line_folded(&self, display_row: u32, ctx: &AppContext) -> bool {
|
||||
let transforms = self.sync(ctx);
|
||||
let mut cursor = transforms.cursor::<DisplayPoint, DisplayPoint>();
|
||||
cursor.seek(&DisplayPoint::new(display_row, 0), SeekBias::Right);
|
||||
while let Some(transform) = cursor.item() {
|
||||
if transform.display_text.is_some() {
|
||||
@@ -196,43 +174,33 @@ impl FoldMap {
|
||||
false
|
||||
}
|
||||
|
||||
pub fn to_buffer_offset(&self, point: DisplayPoint, app: &AppContext) -> Result<usize> {
|
||||
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
|
||||
pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> Result<usize> {
|
||||
let transforms = self.sync(ctx);
|
||||
let mut cursor = 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(app))
|
||||
(cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx))
|
||||
}
|
||||
|
||||
pub fn to_display_offset(
|
||||
&self,
|
||||
point: DisplayPoint,
|
||||
app: &AppContext,
|
||||
ctx: &AppContext,
|
||||
) -> Result<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.chars;
|
||||
if !overshoot.is_zero() {
|
||||
let transform = cursor
|
||||
.item()
|
||||
.ok_or_else(|| anyhow!("display point {:?} is out of range", point))?;
|
||||
assert!(transform.display_text.is_none());
|
||||
let end_buffer_offset =
|
||||
(cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(app))?;
|
||||
offset += end_buffer_offset - cursor.start().buffer.chars;
|
||||
}
|
||||
Ok(DisplayOffset(offset))
|
||||
self.snapshot(ctx).to_display_offset(point, ctx)
|
||||
}
|
||||
|
||||
pub fn to_buffer_point(&self, display_point: DisplayPoint) -> Point {
|
||||
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
|
||||
pub fn to_buffer_point(&self, display_point: DisplayPoint, ctx: &AppContext) -> Point {
|
||||
let transforms = self.sync(ctx);
|
||||
let mut cursor = transforms.cursor::<DisplayPoint, TransformSummary>();
|
||||
cursor.seek(&display_point, SeekBias::Right);
|
||||
let overshoot = display_point.0 - cursor.start().display.lines;
|
||||
cursor.start().buffer.lines + overshoot
|
||||
}
|
||||
|
||||
pub fn to_display_point(&self, point: Point) -> DisplayPoint {
|
||||
let mut cursor = self.transforms.cursor::<Point, TransformSummary>();
|
||||
pub fn to_display_point(&self, point: Point, ctx: &AppContext) -> DisplayPoint {
|
||||
let transforms = self.sync(ctx);
|
||||
let mut cursor = transforms.cursor::<Point, TransformSummary>();
|
||||
cursor.seek(&point, SeekBias::Right);
|
||||
let overshoot = point - cursor.start().buffer.lines;
|
||||
DisplayPoint(cmp::min(
|
||||
@@ -241,12 +209,23 @@ impl FoldMap {
|
||||
))
|
||||
}
|
||||
|
||||
pub fn apply_edits(&mut self, edits: &[Edit], app: &AppContext) -> Result<()> {
|
||||
let buffer = self.buffer.read(app);
|
||||
let mut edits = edits.iter().cloned().peekable();
|
||||
fn sync(&self, ctx: &AppContext) -> MutexGuard<SumTree<Transform>> {
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let mut edits = buffer.edits_since(self.last_sync.lock().clone()).peekable();
|
||||
if edits.peek().is_some() {
|
||||
self.apply_edits(edits, ctx);
|
||||
}
|
||||
*self.last_sync.lock() = buffer.version();
|
||||
self.transforms.lock()
|
||||
}
|
||||
|
||||
fn apply_edits(&self, edits: impl IntoIterator<Item = Edit>, ctx: &AppContext) {
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let mut edits = edits.into_iter().peekable();
|
||||
|
||||
let mut new_transforms = SumTree::new();
|
||||
let mut cursor = self.transforms.cursor::<usize, usize>();
|
||||
let mut transforms = self.transforms.lock();
|
||||
let mut cursor = transforms.cursor::<usize, usize>();
|
||||
cursor.seek(&0, SeekBias::Right);
|
||||
|
||||
while let Some(mut edit) = edits.next() {
|
||||
@@ -282,9 +261,10 @@ impl FoldMap {
|
||||
edit.new_range.end =
|
||||
((edit.new_range.start + edit.old_extent()) as isize + delta) as usize;
|
||||
|
||||
let anchor = buffer.anchor_before(edit.new_range.start)?;
|
||||
let anchor = buffer.anchor_before(edit.new_range.start).unwrap();
|
||||
let folds_start =
|
||||
find_insertion_index(&self.folds, |probe| probe.start.cmp(&anchor, buffer))?;
|
||||
find_insertion_index(&self.folds, |probe| probe.start.cmp(&anchor, buffer))
|
||||
.unwrap();
|
||||
let mut folds = self.folds[folds_start..]
|
||||
.iter()
|
||||
.map(|fold| {
|
||||
@@ -366,9 +346,58 @@ impl FoldMap {
|
||||
}
|
||||
|
||||
drop(cursor);
|
||||
self.transforms = new_transforms;
|
||||
*transforms = new_transforms;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
pub struct FoldMapSnapshot {
|
||||
transforms: SumTree<Transform>,
|
||||
buffer: ModelHandle<Buffer>,
|
||||
}
|
||||
|
||||
impl FoldMapSnapshot {
|
||||
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
|
||||
if start_row > self.transforms.summary().display.lines.row {
|
||||
return Err(anyhow!("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 {
|
||||
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)?;
|
||||
let mut cursor = self.transforms.cursor();
|
||||
cursor.seek(&offset, SeekBias::Right);
|
||||
Ok(Chars {
|
||||
cursor,
|
||||
offset: offset.0,
|
||||
buffer: self.buffer.read(ctx),
|
||||
buffer_chars: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> Result<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.chars;
|
||||
if !overshoot.is_zero() {
|
||||
let transform = cursor
|
||||
.item()
|
||||
.ok_or_else(|| anyhow!("display point {:?} is out of range", point))?;
|
||||
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.chars;
|
||||
}
|
||||
Ok(DisplayOffset(offset))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -500,6 +529,7 @@ impl<'a> Dimension<'a, TransformSummary> for usize {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test::sample_text;
|
||||
use buffer::ToPoint;
|
||||
use gpui::App;
|
||||
|
||||
#[test]
|
||||
@@ -518,8 +548,7 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(map.text(app.as_ref()), "aa…cc…eeeee");
|
||||
|
||||
let edits = buffer.update(app, |buffer, ctx| {
|
||||
let start_version = buffer.version.clone();
|
||||
buffer.update(app, |buffer, ctx| {
|
||||
buffer
|
||||
.edit(
|
||||
vec![
|
||||
@@ -530,21 +559,16 @@ mod tests {
|
||||
Some(ctx),
|
||||
)
|
||||
.unwrap();
|
||||
buffer.edits_since(start_version).collect::<Vec<_>>()
|
||||
});
|
||||
|
||||
map.apply_edits(&edits, app.as_ref()).unwrap();
|
||||
assert_eq!(map.text(app.as_ref()), "123a…c123c…eeeee");
|
||||
|
||||
let edits = buffer.update(app, |buffer, ctx| {
|
||||
buffer.update(app, |buffer, ctx| {
|
||||
let start_version = buffer.version.clone();
|
||||
buffer
|
||||
.edit(Some(Point::new(2, 6)..Point::new(4, 3)), "456", Some(ctx))
|
||||
.unwrap();
|
||||
buffer.edits_since(start_version).collect::<Vec<_>>()
|
||||
});
|
||||
|
||||
map.apply_edits(&edits, app.as_ref()).unwrap();
|
||||
assert_eq!(map.text(app.as_ref()), "123a…c123456eee");
|
||||
|
||||
map.unfold(Some(Point::new(0, 4)..Point::new(0, 4)), app.as_ref())
|
||||
@@ -585,12 +609,11 @@ mod tests {
|
||||
assert_eq!(map.text(app.as_ref()), "…fghijkl");
|
||||
|
||||
// Edit within one of the folds.
|
||||
let edits = buffer.update(app, |buffer, ctx| {
|
||||
buffer.update(app, |buffer, ctx| {
|
||||
let version = buffer.version();
|
||||
buffer.edit(vec![0..1], "12345", Some(ctx)).unwrap();
|
||||
buffer.edits_since(version).collect::<Vec<_>>()
|
||||
});
|
||||
map.apply_edits(edits.as_slice(), app.as_ref()).unwrap();
|
||||
map.check_invariants(app.as_ref());
|
||||
assert_eq!(map.text(app.as_ref()), "12345…fghijkl");
|
||||
}
|
||||
@@ -632,19 +655,49 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(map.text(app.as_ref()), "aa…cccc\nd…eeeee");
|
||||
|
||||
let edits = buffer.update(app, |buffer, ctx| {
|
||||
let start_version = buffer.version.clone();
|
||||
buffer.update(app, |buffer, ctx| {
|
||||
buffer
|
||||
.edit(Some(Point::new(2, 2)..Point::new(3, 1)), "", Some(ctx))
|
||||
.unwrap();
|
||||
buffer.edits_since(start_version).collect::<Vec<_>>()
|
||||
});
|
||||
|
||||
map.apply_edits(&edits, app.as_ref()).unwrap();
|
||||
assert_eq!(map.text(app.as_ref()), "aa…eeeee");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_folds_in_range() {
|
||||
App::test((), |app| {
|
||||
let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(5, 6), ctx));
|
||||
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
|
||||
let buffer = buffer.read(app);
|
||||
|
||||
map.fold(
|
||||
vec![
|
||||
Point::new(0, 2)..Point::new(2, 2),
|
||||
Point::new(0, 4)..Point::new(1, 0),
|
||||
Point::new(1, 2)..Point::new(3, 2),
|
||||
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).unwrap()..fold.end.to_point(buffer).unwrap()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
fold_ranges,
|
||||
vec![
|
||||
Point::new(0, 2)..Point::new(2, 2),
|
||||
Point::new(1, 2)..Point::new(3, 2)
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_random_folds() {
|
||||
use crate::editor::ToPoint;
|
||||
@@ -679,10 +732,10 @@ mod tests {
|
||||
|
||||
for _ in 0..operations {
|
||||
log::info!("text: {:?}", buffer.read(app).text());
|
||||
{
|
||||
if rng.gen() {
|
||||
let buffer = buffer.read(app);
|
||||
|
||||
let fold_count = rng.gen_range(0..=2);
|
||||
let fold_count = rng.gen_range(1..=5);
|
||||
let mut fold_ranges: Vec<Range<usize>> = Vec::new();
|
||||
for _ in 0..fold_count {
|
||||
let end = rng.gen_range(0..buffer.len() + 1);
|
||||
@@ -691,29 +744,15 @@ mod tests {
|
||||
}
|
||||
log::info!("folding {:?}", fold_ranges);
|
||||
map.fold(fold_ranges.clone(), app.as_ref()).unwrap();
|
||||
map.check_invariants(app.as_ref());
|
||||
|
||||
let mut expected_text = buffer.text();
|
||||
for fold_range in map.merged_fold_ranges(app.as_ref()).into_iter().rev() {
|
||||
expected_text.replace_range(fold_range.start..fold_range.end, "…");
|
||||
}
|
||||
assert_eq!(map.text(app.as_ref()), expected_text);
|
||||
|
||||
for fold_range in map.merged_fold_ranges(app.as_ref()) {
|
||||
let display_point =
|
||||
map.to_display_point(fold_range.start.to_point(buffer).unwrap());
|
||||
assert!(map.is_line_folded(display_point.row()));
|
||||
}
|
||||
} else {
|
||||
let edits = buffer.update(app, |buffer, ctx| {
|
||||
let start_version = buffer.version.clone();
|
||||
let edit_count = rng.gen_range(1..=5);
|
||||
buffer.randomly_edit(&mut rng, edit_count, Some(ctx));
|
||||
buffer.edits_since(start_version).collect::<Vec<_>>()
|
||||
});
|
||||
log::info!("editing {:?}", edits);
|
||||
}
|
||||
|
||||
let edits = buffer.update(app, |buffer, ctx| {
|
||||
let start_version = buffer.version.clone();
|
||||
let edit_count = rng.gen_range(0..=2);
|
||||
buffer.randomly_edit(&mut rng, edit_count, Some(ctx));
|
||||
buffer.edits_since(start_version).collect::<Vec<_>>()
|
||||
});
|
||||
log::info!("editing {:?}", edits);
|
||||
map.apply_edits(&edits, app.as_ref()).unwrap();
|
||||
map.check_invariants(app.as_ref());
|
||||
|
||||
let buffer = map.buffer.read(app);
|
||||
@@ -733,13 +772,78 @@ mod tests {
|
||||
|
||||
assert_eq!(map.text(app.as_ref()), expected_text);
|
||||
|
||||
for (idx, buffer_row) in expected_buffer_rows.iter().enumerate() {
|
||||
let display_row = map.to_display_point(Point::new(*buffer_row, 0)).row();
|
||||
for (display_row, line) in expected_text.lines().enumerate() {
|
||||
let line_len = map.line_len(display_row as u32, app.as_ref()).unwrap();
|
||||
assert_eq!(line_len, line.chars().count() as u32);
|
||||
}
|
||||
|
||||
let mut display_point = DisplayPoint::new(0, 0);
|
||||
let mut display_offset = DisplayOffset(0);
|
||||
for c in expected_text.chars() {
|
||||
let buffer_point = map.to_buffer_point(display_point, app.as_ref());
|
||||
let buffer_offset = buffer_point.to_offset(buffer).unwrap();
|
||||
assert_eq!(
|
||||
map.buffer_rows(display_row).unwrap().collect::<Vec<_>>(),
|
||||
map.to_display_point(buffer_point, app.as_ref()),
|
||||
display_point
|
||||
);
|
||||
assert_eq!(
|
||||
map.to_buffer_offset(display_point, app.as_ref()).unwrap(),
|
||||
buffer_offset
|
||||
);
|
||||
assert_eq!(
|
||||
map.to_display_offset(display_point, app.as_ref()).unwrap(),
|
||||
display_offset
|
||||
);
|
||||
|
||||
if c == '\n' {
|
||||
*display_point.row_mut() += 1;
|
||||
*display_point.column_mut() = 0;
|
||||
} else {
|
||||
*display_point.column_mut() += 1;
|
||||
}
|
||||
display_offset.0 += 1;
|
||||
}
|
||||
|
||||
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 point = DisplayPoint::new(row, column);
|
||||
let offset = map.to_display_offset(point, app.as_ref()).unwrap().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
|
||||
.chars()
|
||||
.skip(offset)
|
||||
.take(len)
|
||||
.collect::<String>()
|
||||
);
|
||||
}
|
||||
|
||||
for (idx, buffer_row) in expected_buffer_rows.iter().enumerate() {
|
||||
let display_row = map
|
||||
.to_display_point(Point::new(*buffer_row, 0), app.as_ref())
|
||||
.row();
|
||||
assert_eq!(
|
||||
map.snapshot(app.as_ref())
|
||||
.buffer_rows(display_row)
|
||||
.unwrap()
|
||||
.collect::<Vec<_>>(),
|
||||
expected_buffer_rows[idx..],
|
||||
);
|
||||
}
|
||||
|
||||
for fold_range in map.merged_fold_ranges(app.as_ref()) {
|
||||
let display_point = map.to_display_point(
|
||||
fold_range.start.to_point(buffer).unwrap(),
|
||||
app.as_ref(),
|
||||
);
|
||||
assert!(map.is_line_folded(display_point.row(), app.as_ref()));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -764,24 +868,36 @@ mod tests {
|
||||
|
||||
assert_eq!(map.text(app.as_ref()), "aa…cccc\nd…eeeee\nffffff\n");
|
||||
assert_eq!(
|
||||
map.buffer_rows(0).unwrap().collect::<Vec<_>>(),
|
||||
map.snapshot(app.as_ref())
|
||||
.buffer_rows(0)
|
||||
.unwrap()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![0, 3, 5, 6]
|
||||
);
|
||||
assert_eq!(map.buffer_rows(3).unwrap().collect::<Vec<_>>(), vec![6]);
|
||||
assert_eq!(
|
||||
map.snapshot(app.as_ref())
|
||||
.buffer_rows(3)
|
||||
.unwrap()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![6]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
impl FoldMap {
|
||||
fn text(&self, app: &AppContext) -> String {
|
||||
self.chars_at(DisplayPoint(Point::zero()), app)
|
||||
self.snapshot(app)
|
||||
.chars_at(DisplayPoint(Point::zero()), app)
|
||||
.unwrap()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn merged_fold_ranges(&self, app: &AppContext) -> Vec<Range<usize>> {
|
||||
let buffer = self.buffer.read(app);
|
||||
let mut fold_ranges = self
|
||||
.folds
|
||||
let mut folds = self.folds.clone();
|
||||
// Ensure sorting doesn't change how folds get merged and displayed.
|
||||
folds.sort_by(|a, b| a.cmp(b, buffer).unwrap());
|
||||
let mut fold_ranges = folds
|
||||
.iter()
|
||||
.map(|fold| {
|
||||
fold.start.to_offset(buffer).unwrap()..fold.end.to_offset(buffer).unwrap()
|
||||
@@ -807,10 +923,12 @@ mod tests {
|
||||
merged_ranges
|
||||
}
|
||||
|
||||
fn check_invariants(&self, app: &AppContext) {
|
||||
fn check_invariants(&self, ctx: &AppContext) {
|
||||
let transforms = self.sync(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
assert_eq!(
|
||||
self.transforms.summary().buffer.chars,
|
||||
self.buffer.read(app).len(),
|
||||
transforms.summary().buffer.chars,
|
||||
buffer.len(),
|
||||
"transform tree does not match buffer's length"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ mod fold_map;
|
||||
use super::{buffer, Anchor, AnchorRangeExt, Buffer, Edit, Point, TextSummary, ToOffset, ToPoint};
|
||||
use anyhow::Result;
|
||||
pub use fold_map::BufferRows;
|
||||
use fold_map::FoldMap;
|
||||
use gpui::{AppContext, Entity, ModelContext, ModelHandle};
|
||||
use fold_map::{FoldMap, FoldMapSnapshot};
|
||||
use gpui::{AppContext, ModelHandle};
|
||||
use std::ops::Range;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
@@ -19,22 +19,27 @@ pub struct DisplayMap {
|
||||
tab_size: usize,
|
||||
}
|
||||
|
||||
impl Entity for DisplayMap {
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
impl DisplayMap {
|
||||
pub fn new(buffer: ModelHandle<Buffer>, tab_size: usize, ctx: &mut ModelContext<Self>) -> Self {
|
||||
ctx.subscribe(&buffer, Self::handle_buffer_event);
|
||||
|
||||
pub fn new(buffer: ModelHandle<Buffer>, tab_size: usize, ctx: &AppContext) -> Self {
|
||||
DisplayMap {
|
||||
buffer: buffer.clone(),
|
||||
fold_map: FoldMap::new(buffer, ctx.as_ref()),
|
||||
fold_map: FoldMap::new(buffer, ctx),
|
||||
tab_size,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn folds_in_range<T>(&self, range: Range<T>, app: &AppContext) -> Result<&[Range<Anchor>]>
|
||||
pub fn snapshot(&self, ctx: &AppContext) -> DisplayMapSnapshot {
|
||||
DisplayMapSnapshot {
|
||||
folds_snapshot: self.fold_map.snapshot(ctx),
|
||||
tab_size: self.tab_size,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn folds_in_range<'a, T>(
|
||||
&'a self,
|
||||
range: Range<T>,
|
||||
app: &'a AppContext,
|
||||
) -> Result<impl Iterator<Item = &'a Range<Anchor>>>
|
||||
where
|
||||
T: ToOffset,
|
||||
{
|
||||
@@ -44,40 +49,45 @@ impl DisplayMap {
|
||||
pub fn fold<T: ToOffset>(
|
||||
&mut self,
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
ctx: &mut ModelContext<Self>,
|
||||
ctx: &AppContext,
|
||||
) -> Result<()> {
|
||||
self.fold_map.fold(ranges, ctx.as_ref())?;
|
||||
ctx.notify();
|
||||
Ok(())
|
||||
self.fold_map.fold(ranges, ctx)
|
||||
}
|
||||
|
||||
pub fn unfold<T: ToOffset>(
|
||||
&mut self,
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
ctx: &mut ModelContext<Self>,
|
||||
ctx: &AppContext,
|
||||
) -> Result<()> {
|
||||
self.fold_map.unfold(ranges, ctx.as_ref())?;
|
||||
ctx.notify();
|
||||
Ok(())
|
||||
self.fold_map.unfold(ranges, ctx)
|
||||
}
|
||||
|
||||
pub fn is_line_folded(&self, display_row: u32) -> bool {
|
||||
self.fold_map.is_line_folded(display_row)
|
||||
pub fn is_line_folded(&self, display_row: u32, ctx: &AppContext) -> bool {
|
||||
self.fold_map.is_line_folded(display_row, ctx)
|
||||
}
|
||||
|
||||
pub fn text(&self, app: &AppContext) -> String {
|
||||
self.chars_at(DisplayPoint::zero(), app).unwrap().collect()
|
||||
pub fn text(&self, ctx: &AppContext) -> String {
|
||||
self.snapshot(ctx)
|
||||
.chars_at(DisplayPoint::zero(), ctx)
|
||||
.unwrap()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn line(&self, display_row: u32, app: &AppContext) -> Result<String> {
|
||||
let chars = self.chars_at(DisplayPoint::new(display_row, 0), app)?;
|
||||
Ok(chars.take_while(|c| *c != '\n').collect())
|
||||
pub fn line(&self, display_row: u32, ctx: &AppContext) -> Result<String> {
|
||||
Ok(self
|
||||
.snapshot(ctx)
|
||||
.chars_at(DisplayPoint::new(display_row, 0), ctx)?
|
||||
.take_while(|c| *c != '\n')
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn line_indent(&self, display_row: u32, app: &AppContext) -> Result<(u32, bool)> {
|
||||
pub fn line_indent(&self, display_row: u32, ctx: &AppContext) -> Result<(u32, bool)> {
|
||||
let mut indent = 0;
|
||||
let mut is_blank = true;
|
||||
for c in self.chars_at(DisplayPoint::new(display_row, 0), app)? {
|
||||
for c in self
|
||||
.snapshot(ctx)
|
||||
.chars_at(DisplayPoint::new(display_row, 0), ctx)?
|
||||
{
|
||||
if c == ' ' {
|
||||
indent += 1;
|
||||
} else {
|
||||
@@ -88,38 +98,18 @@ impl DisplayMap {
|
||||
Ok((indent, is_blank))
|
||||
}
|
||||
|
||||
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result<Chars<'a>> {
|
||||
let column = point.column() as usize;
|
||||
let (point, to_next_stop) = point.collapse_tabs(self, Bias::Left, app)?;
|
||||
let mut fold_chars = self.fold_map.chars_at(point, app)?;
|
||||
if to_next_stop > 0 {
|
||||
fold_chars.next();
|
||||
}
|
||||
|
||||
Ok(Chars {
|
||||
fold_chars,
|
||||
column,
|
||||
to_next_stop,
|
||||
tab_size: self.tab_size,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
|
||||
self.fold_map.buffer_rows(start_row)
|
||||
}
|
||||
|
||||
pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
|
||||
DisplayPoint::new(row, self.fold_map.line_len(row, ctx)?)
|
||||
.expand_tabs(self, ctx)
|
||||
.map(|point| point.column())
|
||||
}
|
||||
|
||||
pub fn max_point(&self, app: &AppContext) -> DisplayPoint {
|
||||
self.fold_map.max_point().expand_tabs(self, app).unwrap()
|
||||
pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint {
|
||||
self.fold_map.max_point(ctx).expand_tabs(self, ctx).unwrap()
|
||||
}
|
||||
|
||||
pub fn rightmost_point(&self) -> DisplayPoint {
|
||||
self.fold_map.rightmost_point()
|
||||
pub fn rightmost_point(&self, ctx: &AppContext) -> DisplayPoint {
|
||||
self.fold_map.rightmost_point(ctx)
|
||||
}
|
||||
|
||||
pub fn anchor_before(
|
||||
@@ -143,12 +133,57 @@ impl DisplayMap {
|
||||
.read(app)
|
||||
.anchor_after(point.to_buffer_point(self, bias, app)?)
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_buffer_event(&mut self, event: &buffer::Event, ctx: &mut ModelContext<Self>) {
|
||||
match event {
|
||||
buffer::Event::Edited(edits) => self.fold_map.apply_edits(edits, ctx.as_ref()).unwrap(),
|
||||
_ => {}
|
||||
pub struct DisplayMapSnapshot {
|
||||
folds_snapshot: FoldMapSnapshot,
|
||||
tab_size: usize,
|
||||
}
|
||||
|
||||
impl DisplayMapSnapshot {
|
||||
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
|
||||
self.folds_snapshot.buffer_rows(start_row)
|
||||
}
|
||||
|
||||
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result<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)?;
|
||||
if to_next_stop > 0 {
|
||||
fold_chars.next();
|
||||
}
|
||||
|
||||
Ok(Chars {
|
||||
fold_chars,
|
||||
column,
|
||||
to_next_stop,
|
||||
tab_size: self.tab_size,
|
||||
})
|
||||
}
|
||||
|
||||
fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> Result<DisplayPoint> {
|
||||
let chars = self
|
||||
.folds_snapshot
|
||||
.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)
|
||||
}
|
||||
|
||||
fn collapse_tabs(
|
||||
&self,
|
||||
mut point: DisplayPoint,
|
||||
bias: Bias,
|
||||
ctx: &AppContext,
|
||||
) -> Result<(DisplayPoint, usize)> {
|
||||
let chars = self
|
||||
.folds_snapshot
|
||||
.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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,50 +215,36 @@ impl DisplayPoint {
|
||||
&mut self.0.column
|
||||
}
|
||||
|
||||
pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, app: &AppContext) -> Result<Point> {
|
||||
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, app)?.0))
|
||||
.to_buffer_point(self.collapse_tabs(map, bias, ctx)?.0, ctx))
|
||||
}
|
||||
|
||||
pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, app: &AppContext) -> Result<usize> {
|
||||
pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result<usize> {
|
||||
map.fold_map
|
||||
.to_buffer_offset(self.collapse_tabs(map, bias, app)?.0, app)
|
||||
.to_buffer_offset(self.collapse_tabs(&map, bias, ctx)?.0, ctx)
|
||||
}
|
||||
|
||||
fn expand_tabs(mut self, map: &DisplayMap, app: &AppContext) -> Result<Self> {
|
||||
let chars = map
|
||||
.fold_map
|
||||
.chars_at(DisplayPoint(Point::new(self.row(), 0)), app)?;
|
||||
let expanded = expand_tabs(chars, self.column() as usize, map.tab_size);
|
||||
*self.column_mut() = expanded as u32;
|
||||
|
||||
Ok(self)
|
||||
fn expand_tabs(self, map: &DisplayMap, ctx: &AppContext) -> Result<Self> {
|
||||
map.snapshot(ctx).expand_tabs(self, ctx)
|
||||
}
|
||||
|
||||
fn collapse_tabs(
|
||||
mut self,
|
||||
self,
|
||||
map: &DisplayMap,
|
||||
bias: Bias,
|
||||
app: &AppContext,
|
||||
ctx: &AppContext,
|
||||
) -> Result<(Self, usize)> {
|
||||
let chars = map
|
||||
.fold_map
|
||||
.chars_at(DisplayPoint(Point::new(self.0.row, 0)), app)?;
|
||||
let expanded = self.column() as usize;
|
||||
let (collapsed, to_next_stop) = collapse_tabs(chars, expanded, bias, map.tab_size);
|
||||
*self.column_mut() = collapsed as u32;
|
||||
|
||||
Ok((self, to_next_stop))
|
||||
map.snapshot(ctx).collapse_tabs(self, bias, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Point {
|
||||
pub fn to_display_point(self, map: &DisplayMap, app: &AppContext) -> Result<DisplayPoint> {
|
||||
let mut display_point = map.fold_map.to_display_point(self);
|
||||
let chars = map
|
||||
.fold_map
|
||||
.chars_at(DisplayPoint::new(display_point.row(), 0), app)?;
|
||||
pub fn to_display_point(self, map: &DisplayMap, ctx: &AppContext) -> Result<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)?;
|
||||
*display_point.column_mut() =
|
||||
expand_tabs(chars, display_point.column() as usize, map.tab_size) as u32;
|
||||
Ok(display_point)
|
||||
@@ -325,7 +346,7 @@ mod tests {
|
||||
App::test((), |app| {
|
||||
let text = sample_text(6, 6);
|
||||
let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx));
|
||||
let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx));
|
||||
let map = DisplayMap::new(buffer.clone(), 4, app.as_ref());
|
||||
buffer
|
||||
.update(app, |buffer, ctx| {
|
||||
buffer.edit(
|
||||
@@ -340,23 +361,25 @@ mod tests {
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let map = map.read(app);
|
||||
assert_eq!(
|
||||
map.chars_at(DisplayPoint::new(1, 0), app.as_ref())
|
||||
map.snapshot(app.as_ref())
|
||||
.chars_at(DisplayPoint::new(1, 0), app.as_ref())
|
||||
.unwrap()
|
||||
.take(10)
|
||||
.collect::<String>(),
|
||||
" b bb"
|
||||
);
|
||||
assert_eq!(
|
||||
map.chars_at(DisplayPoint::new(1, 2), app.as_ref())
|
||||
map.snapshot(app.as_ref())
|
||||
.chars_at(DisplayPoint::new(1, 2), app.as_ref())
|
||||
.unwrap()
|
||||
.take(10)
|
||||
.collect::<String>(),
|
||||
" b bbbb"
|
||||
);
|
||||
assert_eq!(
|
||||
map.chars_at(DisplayPoint::new(1, 6), app.as_ref())
|
||||
map.snapshot(app.as_ref())
|
||||
.chars_at(DisplayPoint::new(1, 6), app.as_ref())
|
||||
.unwrap()
|
||||
.take(13)
|
||||
.collect::<String>(),
|
||||
@@ -392,11 +415,8 @@ mod tests {
|
||||
fn test_max_point() {
|
||||
App::test((), |app| {
|
||||
let buffer = app.add_model(|ctx| Buffer::new(0, "aaa\n\t\tbbb", ctx));
|
||||
let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx));
|
||||
assert_eq!(
|
||||
map.read(app).max_point(app.as_ref()),
|
||||
DisplayPoint::new(1, 11)
|
||||
)
|
||||
let map = DisplayMap::new(buffer.clone(), 4, app.as_ref());
|
||||
assert_eq!(map.max_point(app.as_ref()), DisplayPoint::new(1, 11))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ pub fn prev_word_boundary(
|
||||
let mut boundary = DisplayPoint::new(point.row(), 0);
|
||||
let mut column = 0;
|
||||
let mut prev_c = None;
|
||||
for c in map.chars_at(boundary, app)? {
|
||||
for c in map.snapshot(app).chars_at(boundary, app)? {
|
||||
if column >= point.column() {
|
||||
break;
|
||||
}
|
||||
@@ -118,7 +118,7 @@ pub fn next_word_boundary(
|
||||
app: &AppContext,
|
||||
) -> Result<DisplayPoint> {
|
||||
let mut prev_c = None;
|
||||
for c in map.chars_at(point, app)? {
|
||||
for c in map.snapshot(app).chars_at(point, app)? {
|
||||
if prev_c.is_some() && (c == '\n' || char_kind(prev_c.unwrap()) != char_kind(c)) {
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user