Consolidate Edit types in editor crate

This commit is contained in:
Max Brunsfeld 2021-12-08 09:33:55 -08:00
parent fa379885f1
commit 6a44a7448e
4 changed files with 83 additions and 116 deletions

View file

@ -1,4 +1,4 @@
use super::wrap_map::{self, Edit as WrapEdit, WrapPoint, WrapSnapshot};
use super::wrap_map::{self, WrapEdit, WrapPoint, WrapSnapshot};
use gpui::{AppContext, ElementBox, ModelHandle};
use language::{Buffer, Chunk};
use parking_lot::Mutex;

View file

@ -5,7 +5,7 @@ use parking_lot::Mutex;
use std::{
cmp::{self, Ordering},
iter,
ops::Range,
ops::{Range, Sub},
sync::atomic::{AtomicUsize, Ordering::SeqCst},
};
use sum_tree::{Bias, Cursor, FilterCursor, SumTree};
@ -456,8 +456,8 @@ impl FoldMap {
new_transforms.start().1 .0 + (edit.new.end - new_transforms.start().0);
fold_edits.push(FoldEdit {
old_bytes: FoldOffset(old_start)..FoldOffset(old_end),
new_bytes: FoldOffset(new_start)..FoldOffset(new_end),
old: FoldOffset(old_start)..FoldOffset(old_end),
new: FoldOffset(new_start)..FoldOffset(new_end),
});
}
@ -749,20 +749,20 @@ fn consolidate_buffer_edits(edits: &mut Vec<text::Edit<usize>>) {
fn consolidate_fold_edits(edits: &mut Vec<FoldEdit>) {
edits.sort_unstable_by(|a, b| {
a.old_bytes
a.old
.start
.cmp(&b.old_bytes.start)
.then_with(|| b.old_bytes.end.cmp(&a.old_bytes.end))
.cmp(&b.old.start)
.then_with(|| b.old.end.cmp(&a.old.end))
});
let mut i = 1;
while i < edits.len() {
let edit = edits[i].clone();
let prev_edit = &mut edits[i - 1];
if prev_edit.old_bytes.end >= edit.old_bytes.start {
prev_edit.old_bytes.end = prev_edit.old_bytes.end.max(edit.old_bytes.end);
prev_edit.new_bytes.start = prev_edit.new_bytes.start.min(edit.new_bytes.start);
prev_edit.new_bytes.end = prev_edit.new_bytes.end.max(edit.new_bytes.end);
if prev_edit.old.end >= edit.old.start {
prev_edit.old.end = prev_edit.old.end.max(edit.old.end);
prev_edit.new.start = prev_edit.new.start.min(edit.new.start);
prev_edit.new.end = prev_edit.new.end.max(edit.new.end);
edits.remove(i);
continue;
}
@ -1021,6 +1021,14 @@ impl FoldOffset {
}
}
impl Sub for FoldOffset {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl<'a> sum_tree::Dimension<'a, TransformSummary> for FoldOffset {
fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
self.0 += &summary.output.bytes;
@ -1039,26 +1047,7 @@ impl<'a> sum_tree::Dimension<'a, TransformSummary> for usize {
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FoldEdit {
pub old_bytes: Range<FoldOffset>,
pub new_bytes: Range<FoldOffset>,
}
#[cfg(test)]
impl FoldEdit {
pub fn delta(&self) -> isize {
self.inserted_bytes() as isize - self.deleted_bytes() as isize
}
pub fn deleted_bytes(&self) -> usize {
self.old_bytes.end.0 - self.old_bytes.start.0
}
pub fn inserted_bytes(&self) -> usize {
self.new_bytes.end.0 - self.new_bytes.start.0
}
}
pub type FoldEdit = Edit<FoldOffset>;
#[cfg(test)]
mod tests {
@ -1087,12 +1076,12 @@ mod tests {
edits,
&[
FoldEdit {
old_bytes: FoldOffset(2)..FoldOffset(16),
new_bytes: FoldOffset(2)..FoldOffset(5),
old: FoldOffset(2)..FoldOffset(16),
new: FoldOffset(2)..FoldOffset(5),
},
FoldEdit {
old_bytes: FoldOffset(18)..FoldOffset(29),
new_bytes: FoldOffset(7)..FoldOffset(10)
old: FoldOffset(18)..FoldOffset(29),
new: FoldOffset(7)..FoldOffset(10)
},
]
);
@ -1115,12 +1104,12 @@ mod tests {
edits,
&[
FoldEdit {
old_bytes: FoldOffset(0)..FoldOffset(1),
new_bytes: FoldOffset(0)..FoldOffset(3),
old: FoldOffset(0)..FoldOffset(1),
new: FoldOffset(0)..FoldOffset(3),
},
FoldEdit {
old_bytes: FoldOffset(6)..FoldOffset(6),
new_bytes: FoldOffset(8)..FoldOffset(11),
old: FoldOffset(6)..FoldOffset(6),
new: FoldOffset(8)..FoldOffset(11),
},
]
);
@ -1454,12 +1443,9 @@ mod tests {
let mut text = initial_snapshot.text();
for (snapshot, edits) in snapshot_edits.drain(..) {
let new_text = snapshot.text();
let mut delta = 0isize;
for edit in edits {
let old_bytes = ((edit.old_bytes.start.0 as isize) + delta) as usize
..((edit.old_bytes.end.0 as isize) + delta) as usize;
let new_bytes = edit.new_bytes.start.0..edit.new_bytes.end.0;
delta += edit.delta();
let old_bytes = edit.new.start.0..edit.new.start.0 + edit.old_len().0;
let new_bytes = edit.new.start.0..edit.new.end.0;
text.replace_range(old_bytes, &new_text[new_bytes]);
}

View file

@ -21,7 +21,7 @@ impl TabMap {
&self,
fold_snapshot: FoldSnapshot,
mut fold_edits: Vec<FoldEdit>,
) -> (TabSnapshot, Vec<Edit>) {
) -> (TabSnapshot, Vec<TabEdit>) {
let mut old_snapshot = self.0.lock();
let max_offset = old_snapshot.fold_snapshot.len();
let new_snapshot = TabSnapshot {
@ -34,13 +34,13 @@ impl TabMap {
let mut delta = 0;
for chunk in old_snapshot
.fold_snapshot
.chunks(fold_edit.old_bytes.end..max_offset, None)
.chunks(fold_edit.old.end..max_offset, None)
{
let patterns: &[_] = &['\t', '\n'];
if let Some(ix) = chunk.text.find(patterns) {
if &chunk.text[ix..ix + 1] == "\t" {
fold_edit.old_bytes.end.0 += delta + ix + 1;
fold_edit.new_bytes.end.0 += delta + ix + 1;
fold_edit.old.end.0 += delta + ix + 1;
fold_edit.new.end.0 += delta + ix + 1;
}
break;
@ -55,9 +55,9 @@ impl TabMap {
let (prev_edits, next_edits) = fold_edits.split_at_mut(ix);
let prev_edit = prev_edits.last_mut().unwrap();
let edit = &next_edits[0];
if prev_edit.old_bytes.end >= edit.old_bytes.start {
prev_edit.old_bytes.end = edit.old_bytes.end;
prev_edit.new_bytes.end = edit.new_bytes.end;
if prev_edit.old.end >= edit.old.start {
prev_edit.old.end = edit.old.end;
prev_edit.new.end = edit.new.end;
fold_edits.remove(ix);
} else {
ix += 1;
@ -65,25 +65,13 @@ impl TabMap {
}
for fold_edit in fold_edits {
let old_start = fold_edit
.old_bytes
.start
.to_point(&old_snapshot.fold_snapshot);
let old_end = fold_edit
.old_bytes
.end
.to_point(&old_snapshot.fold_snapshot);
let new_start = fold_edit
.new_bytes
.start
.to_point(&new_snapshot.fold_snapshot);
let new_end = fold_edit
.new_bytes
.end
.to_point(&new_snapshot.fold_snapshot);
tab_edits.push(Edit {
old_lines: old_snapshot.to_tab_point(old_start)..old_snapshot.to_tab_point(old_end),
new_lines: new_snapshot.to_tab_point(new_start)..new_snapshot.to_tab_point(new_end),
let old_start = fold_edit.old.start.to_point(&old_snapshot.fold_snapshot);
let old_end = fold_edit.old.end.to_point(&old_snapshot.fold_snapshot);
let new_start = fold_edit.new.start.to_point(&new_snapshot.fold_snapshot);
let new_end = fold_edit.new.end.to_point(&new_snapshot.fold_snapshot);
tab_edits.push(TabEdit {
old: old_snapshot.to_tab_point(old_start)..old_snapshot.to_tab_point(old_end),
new: new_snapshot.to_tab_point(new_start)..new_snapshot.to_tab_point(new_end),
});
}
@ -322,11 +310,7 @@ impl From<super::Point> for TabPoint {
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Edit {
pub old_lines: Range<TabPoint>,
pub new_lines: Range<TabPoint>,
}
pub type TabEdit = text::Edit<TabPoint>;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct TextSummary {

View file

@ -1,6 +1,6 @@
use super::{
fold_map,
tab_map::{self, Edit as TabEdit, TabPoint, TabSnapshot},
tab_map::{self, TabEdit, TabPoint, TabSnapshot},
};
use gpui::{
fonts::FontId, text_layout::LineWrapper, Entity, ModelContext, ModelHandle, MutableAppContext,
@ -15,7 +15,7 @@ use text::Patch;
use theme::SyntaxTheme;
pub use super::tab_map::TextSummary;
pub type Edit = text::Edit<u32>;
pub type WrapEdit = text::Edit<u32>;
pub struct WrapMap {
snapshot: WrapSnapshot,
@ -106,7 +106,7 @@ impl WrapMap {
tab_snapshot: TabSnapshot,
edits: Vec<TabEdit>,
cx: &mut ModelContext<Self>,
) -> (WrapSnapshot, Vec<Edit>) {
) -> (WrapSnapshot, Vec<WrapEdit>) {
if self.wrap_width.is_some() {
self.pending_edits.push_back((tab_snapshot, edits));
self.flush_edits(cx);
@ -157,8 +157,8 @@ impl WrapMap {
.update(
tab_snapshot,
&[TabEdit {
old_lines: range.clone(),
new_lines: range.clone(),
old: range.clone(),
new: range.clone(),
}],
wrap_width,
&mut line_wrapper,
@ -203,7 +203,7 @@ impl WrapMap {
}
let new_rows = self.snapshot.transforms.summary().output.lines.row + 1;
self.snapshot.interpolated = false;
self.edits_since_sync = self.edits_since_sync.compose(&Patch::new(vec![Edit {
self.edits_since_sync = self.edits_since_sync.compose(&Patch::new(vec![WrapEdit {
old: 0..old_rows,
new: 0..new_rows,
}]));
@ -313,47 +313,44 @@ impl WrapSnapshot {
let mut old_cursor = self.transforms.cursor::<TabPoint>();
let mut tab_edits_iter = tab_edits.iter().peekable();
new_transforms = old_cursor.slice(
&tab_edits_iter.peek().unwrap().old_lines.start,
Bias::Right,
&(),
);
new_transforms =
old_cursor.slice(&tab_edits_iter.peek().unwrap().old.start, Bias::Right, &());
while let Some(edit) = tab_edits_iter.next() {
if edit.new_lines.start > TabPoint::from(new_transforms.summary().input.lines) {
if edit.new.start > TabPoint::from(new_transforms.summary().input.lines) {
let summary = new_tab_snapshot.text_summary_for_range(
TabPoint::from(new_transforms.summary().input.lines)..edit.new_lines.start,
TabPoint::from(new_transforms.summary().input.lines)..edit.new.start,
);
new_transforms.push_or_extend(Transform::isomorphic(summary));
}
if !edit.new_lines.is_empty() {
if !edit.new.is_empty() {
new_transforms.push_or_extend(Transform::isomorphic(
new_tab_snapshot.text_summary_for_range(edit.new_lines.clone()),
new_tab_snapshot.text_summary_for_range(edit.new.clone()),
));
}
old_cursor.seek_forward(&edit.old_lines.end, Bias::Right, &());
old_cursor.seek_forward(&edit.old.end, Bias::Right, &());
if let Some(next_edit) = tab_edits_iter.peek() {
if next_edit.old_lines.start > old_cursor.end(&()) {
if old_cursor.end(&()) > edit.old_lines.end {
if next_edit.old.start > old_cursor.end(&()) {
if old_cursor.end(&()) > edit.old.end {
let summary = self
.tab_snapshot
.text_summary_for_range(edit.old_lines.end..old_cursor.end(&()));
.text_summary_for_range(edit.old.end..old_cursor.end(&()));
new_transforms.push_or_extend(Transform::isomorphic(summary));
}
old_cursor.next(&());
new_transforms.push_tree(
old_cursor.slice(&next_edit.old_lines.start, Bias::Right, &()),
old_cursor.slice(&next_edit.old.start, Bias::Right, &()),
&(),
);
}
} else {
if old_cursor.end(&()) > edit.old_lines.end {
if old_cursor.end(&()) > edit.old.end {
let summary = self
.tab_snapshot
.text_summary_for_range(edit.old_lines.end..old_cursor.end(&()));
.text_summary_for_range(edit.old.end..old_cursor.end(&()));
new_transforms.push_or_extend(Transform::isomorphic(summary));
}
old_cursor.next(&());
@ -391,14 +388,14 @@ impl WrapSnapshot {
let mut row_edits = Vec::new();
while let Some(edit) = tab_edits_iter.next() {
let mut row_edit = RowEdit {
old_rows: edit.old_lines.start.row()..edit.old_lines.end.row() + 1,
new_rows: edit.new_lines.start.row()..edit.new_lines.end.row() + 1,
old_rows: edit.old.start.row()..edit.old.end.row() + 1,
new_rows: edit.new.start.row()..edit.new.end.row() + 1,
};
while let Some(next_edit) = tab_edits_iter.peek() {
if next_edit.old_lines.start.row() <= row_edit.old_rows.end {
row_edit.old_rows.end = next_edit.old_lines.end.row() + 1;
row_edit.new_rows.end = next_edit.new_lines.end.row() + 1;
if next_edit.old.start.row() <= row_edit.old_rows.end {
row_edit.old_rows.end = next_edit.old.end.row() + 1;
row_edit.new_rows.end = next_edit.new.end.row() + 1;
tab_edits_iter.next();
} else {
break;
@ -528,28 +525,28 @@ impl WrapSnapshot {
let mut old_cursor = self.transforms.cursor::<TransformSummary>();
let mut new_cursor = new_snapshot.transforms.cursor::<TransformSummary>();
for mut tab_edit in tab_edits.iter().cloned() {
tab_edit.old_lines.start.0.column = 0;
tab_edit.old_lines.end.0 += Point::new(1, 0);
tab_edit.new_lines.start.0.column = 0;
tab_edit.new_lines.end.0 += Point::new(1, 0);
tab_edit.old.start.0.column = 0;
tab_edit.old.end.0 += Point::new(1, 0);
tab_edit.new.start.0.column = 0;
tab_edit.new.end.0 += Point::new(1, 0);
old_cursor.seek(&tab_edit.old_lines.start, Bias::Right, &());
old_cursor.seek(&tab_edit.old.start, Bias::Right, &());
let mut old_start = old_cursor.start().output.lines;
old_start += tab_edit.old_lines.start.0 - old_cursor.start().input.lines;
old_start += tab_edit.old.start.0 - old_cursor.start().input.lines;
old_cursor.seek(&tab_edit.old_lines.end, Bias::Right, &());
old_cursor.seek(&tab_edit.old.end, Bias::Right, &());
let mut old_end = old_cursor.start().output.lines;
old_end += tab_edit.old_lines.end.0 - old_cursor.start().input.lines;
old_end += tab_edit.old.end.0 - old_cursor.start().input.lines;
new_cursor.seek(&tab_edit.new_lines.start, Bias::Right, &());
new_cursor.seek(&tab_edit.new.start, Bias::Right, &());
let mut new_start = new_cursor.start().output.lines;
new_start += tab_edit.new_lines.start.0 - new_cursor.start().input.lines;
new_start += tab_edit.new.start.0 - new_cursor.start().input.lines;
new_cursor.seek(&tab_edit.new_lines.end, Bias::Right, &());
new_cursor.seek(&tab_edit.new.end, Bias::Right, &());
let mut new_end = new_cursor.start().output.lines;
new_end += tab_edit.new_lines.end.0 - new_cursor.start().input.lines;
new_end += tab_edit.new.end.0 - new_cursor.start().input.lines;
wrap_edits.push(Edit {
wrap_edits.push(WrapEdit {
old: old_start.row..old_end.row,
new: new_start.row..new_end.row,
});
@ -955,7 +952,7 @@ impl<'a> sum_tree::Dimension<'a, TransformSummary> for WrapPoint {
}
}
fn consolidate_wrap_edits(edits: &mut Vec<Edit>) {
fn consolidate_wrap_edits(edits: &mut Vec<WrapEdit>) {
let mut i = 1;
while i < edits.len() {
let edit = edits[i].clone();