Building, but failing test WIP
This commit is contained in:
parent
065734e1de
commit
04fc1d5982
14 changed files with 492 additions and 419 deletions
|
@ -843,7 +843,7 @@ pub mod tests {
|
|||
|
||||
let ix = snapshot.buffer_snapshot.text().find("seven").unwrap();
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(vec![ix..ix], "and ", cx);
|
||||
buffer.edit(ix..ix, "and ", cx);
|
||||
});
|
||||
|
||||
let snapshot = map.update(cx, |map, cx| map.snapshot(cx));
|
||||
|
@ -876,13 +876,12 @@ pub mod tests {
|
|||
let map =
|
||||
cx.add_model(|cx| DisplayMap::new(buffer.clone(), font_id, font_size, None, 1, 1, cx));
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
buffer.edit_batched(
|
||||
vec![
|
||||
Point::new(1, 0)..Point::new(1, 0),
|
||||
Point::new(1, 1)..Point::new(1, 1),
|
||||
Point::new(2, 1)..Point::new(2, 1),
|
||||
(Point::new(1, 0)..Point::new(1, 0), "\t"),
|
||||
(Point::new(1, 1)..Point::new(1, 1), "\t"),
|
||||
(Point::new(2, 1)..Point::new(2, 1), "\t"),
|
||||
],
|
||||
"\t",
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
|
|
@ -1154,7 +1154,7 @@ mod tests {
|
|||
|
||||
// Insert a line break, separating two block decorations into separate lines.
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit([Point::new(1, 1)..Point::new(1, 1)], "!!!\n", cx);
|
||||
buffer.edit(Point::new(1, 1)..Point::new(1, 1), "!!!\n", cx);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
|
||||
|
|
|
@ -1246,12 +1246,11 @@ mod tests {
|
|||
);
|
||||
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
buffer.edit_batched(
|
||||
vec![
|
||||
Point::new(0, 0)..Point::new(0, 1),
|
||||
Point::new(2, 3)..Point::new(2, 3),
|
||||
(Point::new(0, 0)..Point::new(0, 1), "123"),
|
||||
(Point::new(2, 3)..Point::new(2, 3), "123"),
|
||||
],
|
||||
"123",
|
||||
cx,
|
||||
);
|
||||
buffer.snapshot(cx)
|
||||
|
@ -1274,7 +1273,7 @@ mod tests {
|
|||
);
|
||||
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(vec![Point::new(2, 6)..Point::new(4, 3)], "456", cx);
|
||||
buffer.edit(Point::new(2, 6)..Point::new(4, 3), "456", cx);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot4, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner());
|
||||
|
@ -1330,7 +1329,7 @@ mod tests {
|
|||
|
||||
// Edit within one of the folds.
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(vec![0..1], "12345", cx);
|
||||
buffer.edit(0..1, "12345", cx);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot, _) =
|
||||
|
@ -1372,7 +1371,7 @@ mod tests {
|
|||
assert_eq!(snapshot.text(), "aa…cccc\nd…eeeee");
|
||||
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(Some(Point::new(2, 2)..Point::new(3, 1)), "", cx);
|
||||
buffer.edit(Point::new(2, 2)..Point::new(3, 1), "", cx);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner());
|
||||
|
|
|
@ -549,7 +549,7 @@ struct SnippetState {
|
|||
|
||||
pub struct RenameState {
|
||||
pub range: Range<Anchor>,
|
||||
pub old_name: String,
|
||||
pub old_name: Arc<str>,
|
||||
pub editor: ViewHandle<Editor>,
|
||||
block_id: BlockId,
|
||||
}
|
||||
|
@ -1912,46 +1912,19 @@ impl Editor {
|
|||
}
|
||||
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
let mut delta = 0_isize;
|
||||
let mut pending_edit: Option<PendingEdit> = None;
|
||||
for (_, _, range, indent, insert_extra_newline) in &old_selections {
|
||||
if pending_edit.as_ref().map_or(false, |pending| {
|
||||
pending.indent != *indent
|
||||
|| pending.insert_extra_newline != *insert_extra_newline
|
||||
}) {
|
||||
let pending = pending_edit.take().unwrap();
|
||||
let mut new_text = String::with_capacity(1 + pending.indent as usize);
|
||||
new_text.push('\n');
|
||||
new_text.extend(iter::repeat(' ').take(pending.indent as usize));
|
||||
if pending.insert_extra_newline {
|
||||
new_text = new_text.repeat(2);
|
||||
}
|
||||
buffer.edit_with_autoindent(pending.ranges, new_text, cx);
|
||||
delta += pending.delta;
|
||||
}
|
||||
|
||||
let start = (range.start as isize + delta) as usize;
|
||||
let end = (range.end as isize + delta) as usize;
|
||||
let mut text_len = *indent as usize + 1;
|
||||
if *insert_extra_newline {
|
||||
text_len *= 2;
|
||||
}
|
||||
|
||||
let pending = pending_edit.get_or_insert_with(Default::default);
|
||||
pending.delta += text_len as isize - (end - start) as isize;
|
||||
pending.indent = *indent;
|
||||
pending.insert_extra_newline = *insert_extra_newline;
|
||||
pending.ranges.push(start..end);
|
||||
}
|
||||
|
||||
let pending = pending_edit.unwrap();
|
||||
let mut new_text = String::with_capacity(1 + pending.indent as usize);
|
||||
new_text.push('\n');
|
||||
new_text.extend(iter::repeat(' ').take(pending.indent as usize));
|
||||
if pending.insert_extra_newline {
|
||||
new_text = new_text.repeat(2);
|
||||
}
|
||||
buffer.edit_with_autoindent(pending.ranges, new_text, cx);
|
||||
let edits =
|
||||
old_selections
|
||||
.iter()
|
||||
.map(|(_, _, range, indent, insert_extra_newline)| {
|
||||
let mut new_text = String::with_capacity(1 + *indent as usize);
|
||||
new_text.push('\n');
|
||||
new_text.extend(iter::repeat(' ').take(*indent as usize));
|
||||
if *insert_extra_newline {
|
||||
new_text = new_text.repeat(2);
|
||||
}
|
||||
(range.clone(), new_text)
|
||||
});
|
||||
buffer.edit_with_autoindent_batched(edits, cx);
|
||||
|
||||
let buffer = buffer.read(cx);
|
||||
this.selections = this
|
||||
|
@ -1977,14 +1950,6 @@ impl Editor {
|
|||
|
||||
this.request_autoscroll(Autoscroll::Fit, cx);
|
||||
});
|
||||
|
||||
#[derive(Default)]
|
||||
struct PendingEdit {
|
||||
indent: u32,
|
||||
insert_extra_newline: bool,
|
||||
delta: isize,
|
||||
ranges: SmallVec<[Range<usize>; 32]>,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, text: &str, cx: &mut ViewContext<Self>) {
|
||||
|
@ -1998,8 +1963,10 @@ impl Editor {
|
|||
.map(|s| (s.id, s.goal, snapshot.anchor_after(s.end)))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let edit_ranges = old_selections.iter().map(|s| s.start..s.end);
|
||||
buffer.edit_with_autoindent(edit_ranges, text, cx);
|
||||
buffer.edit_with_autoindent_batched(
|
||||
old_selections.iter().map(|s| (s.start..s.end, text)),
|
||||
cx,
|
||||
);
|
||||
anchors
|
||||
});
|
||||
|
||||
|
@ -2057,14 +2024,18 @@ impl Editor {
|
|||
drop(snapshot);
|
||||
|
||||
self.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
selections.iter().map(|s| s.start.clone()..s.start.clone()),
|
||||
&pair.start,
|
||||
let pair_start: Arc<str> = pair.start.clone().into();
|
||||
buffer.edit_batched(
|
||||
selections
|
||||
.iter()
|
||||
.map(|s| (s.start.clone()..s.start.clone(), pair_start.clone())),
|
||||
cx,
|
||||
);
|
||||
buffer.edit(
|
||||
selections.iter().map(|s| s.end.clone()..s.end.clone()),
|
||||
&pair.end,
|
||||
let pair_end: Arc<str> = pair.end.clone().into();
|
||||
buffer.edit_batched(
|
||||
selections
|
||||
.iter()
|
||||
.map(|s| (s.end.clone()..s.end.clone(), pair_end.clone())),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
|
@ -2141,7 +2112,13 @@ impl Editor {
|
|||
})
|
||||
.collect::<SmallVec<[_; 32]>>();
|
||||
|
||||
buffer.edit(selection_ranges, &pair.end, cx);
|
||||
let pair_end: Arc<str> = pair.end.clone().into();
|
||||
buffer.edit_batched(
|
||||
selection_ranges
|
||||
.iter()
|
||||
.map(|range| (range.clone(), pair_end.clone())),
|
||||
cx,
|
||||
);
|
||||
snapshot = buffer.snapshot(cx);
|
||||
|
||||
new_selections = Some(
|
||||
|
@ -2400,7 +2377,10 @@ impl Editor {
|
|||
this.insert_snippet(&ranges, snippet, cx).log_err();
|
||||
} else {
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit_with_autoindent(ranges, text, cx);
|
||||
buffer.edit_with_autoindent_batched(
|
||||
ranges.iter().map(|range| (range.clone(), text)),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@ -2752,7 +2732,14 @@ impl Editor {
|
|||
cx: &mut ViewContext<Self>,
|
||||
) -> Result<()> {
|
||||
let tabstops = self.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit_with_autoindent(insertion_ranges.iter().cloned(), &snippet.text, cx);
|
||||
let snippet_text: Arc<str> = snippet.text.clone().into();
|
||||
buffer.edit_with_autoindent_batched(
|
||||
insertion_ranges
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|range| (range, snippet_text.clone())),
|
||||
cx,
|
||||
);
|
||||
|
||||
let snapshot = &*buffer.read(cx);
|
||||
let snippet = &snippet;
|
||||
|
@ -2933,7 +2920,7 @@ impl Editor {
|
|||
.count();
|
||||
let chars_to_next_tab_stop = tab_size - (char_column as u32 % tab_size);
|
||||
buffer.edit(
|
||||
[selection.start..selection.start],
|
||||
selection.start..selection.start,
|
||||
" ".repeat(chars_to_next_tab_stop as usize),
|
||||
cx,
|
||||
);
|
||||
|
@ -2984,7 +2971,7 @@ impl Editor {
|
|||
let columns_to_next_tab_stop = tab_size - (indent_column % tab_size);
|
||||
let row_start = Point::new(row, 0);
|
||||
buffer.edit(
|
||||
[row_start..row_start],
|
||||
row_start..row_start,
|
||||
" ".repeat(columns_to_next_tab_stop as usize),
|
||||
cx,
|
||||
);
|
||||
|
@ -3043,7 +3030,7 @@ impl Editor {
|
|||
|
||||
self.transact(cx, |this, cx| {
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(deletion_ranges, "", cx);
|
||||
buffer.edit_batched(deletion_ranges.into_iter().map(|range| (range, "")), cx);
|
||||
});
|
||||
this.update_selections(
|
||||
this.local_selections::<usize>(cx),
|
||||
|
@ -3105,7 +3092,7 @@ impl Editor {
|
|||
|
||||
self.transact(cx, |this, cx| {
|
||||
let buffer = this.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(edit_ranges, "", cx);
|
||||
buffer.edit_batched(edit_ranges.into_iter().map(|range| (range, "")), cx);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let new_selections = new_cursors
|
||||
|
@ -3159,7 +3146,7 @@ impl Editor {
|
|||
self.transact(cx, |this, cx| {
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
for (point, text, _) in edits.into_iter().rev() {
|
||||
buffer.edit(Some(point..point), text, cx);
|
||||
buffer.edit(point..point, text, cx);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -3269,7 +3256,7 @@ impl Editor {
|
|||
this.unfold_ranges(unfold_ranges, true, cx);
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
for (range, text) in edits {
|
||||
buffer.edit([range], text, cx);
|
||||
buffer.edit(range, text, cx);
|
||||
}
|
||||
});
|
||||
this.fold_ranges(refold_ranges, cx);
|
||||
|
@ -3372,7 +3359,7 @@ impl Editor {
|
|||
this.unfold_ranges(unfold_ranges, true, cx);
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
for (range, text) in edits {
|
||||
buffer.edit([range], text, cx);
|
||||
buffer.edit(range, text, cx);
|
||||
}
|
||||
});
|
||||
this.fold_ranges(refold_ranges, cx);
|
||||
|
@ -3488,7 +3475,7 @@ impl Editor {
|
|||
};
|
||||
|
||||
delta += to_insert.len() as isize - range.len() as isize;
|
||||
buffer.edit([range], to_insert, cx);
|
||||
buffer.edit(range, to_insert, cx);
|
||||
selection.start += to_insert.len();
|
||||
selection.end = selection.start;
|
||||
});
|
||||
|
@ -4204,11 +4191,11 @@ impl Editor {
|
|||
for selection in &mut selections {
|
||||
// Get the line comment prefix. Split its trailing whitespace into a separate string,
|
||||
// as that portion won't be used for detecting if a line is a comment.
|
||||
let full_comment_prefix = if let Some(prefix) = buffer
|
||||
let full_comment_prefix: Arc<str> = if let Some(prefix) = buffer
|
||||
.language_at(selection.start, cx)
|
||||
.and_then(|l| l.line_comment_prefix())
|
||||
{
|
||||
prefix.to_string()
|
||||
prefix.into()
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
@ -4275,15 +4262,18 @@ impl Editor {
|
|||
|
||||
if !edit_ranges.is_empty() {
|
||||
if all_selection_lines_are_comments {
|
||||
buffer.edit(edit_ranges.iter().cloned(), "", cx);
|
||||
buffer.edit_batched(
|
||||
edit_ranges.iter().cloned().map(|range| (range, "")),
|
||||
cx,
|
||||
);
|
||||
} else {
|
||||
let min_column =
|
||||
edit_ranges.iter().map(|r| r.start.column).min().unwrap();
|
||||
let edit_ranges = edit_ranges.iter().map(|range| {
|
||||
let edits = edit_ranges.iter().map(|range| {
|
||||
let position = Point::new(range.start.row, min_column);
|
||||
position..position
|
||||
(position..position, full_comment_prefix.clone())
|
||||
});
|
||||
buffer.edit(edit_ranges, &full_comment_prefix, cx);
|
||||
buffer.edit_batched(edits, cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4664,7 +4654,7 @@ impl Editor {
|
|||
let rename_end = rename_start + rename_buffer_range.len();
|
||||
let range = buffer.anchor_before(rename_start)..buffer.anchor_after(rename_end);
|
||||
let mut old_highlight_id = None;
|
||||
let old_name = buffer
|
||||
let old_name: Arc<str> = buffer
|
||||
.chunks(rename_start..rename_end, true)
|
||||
.map(|chunk| {
|
||||
if old_highlight_id.is_none() {
|
||||
|
@ -4672,7 +4662,8 @@ impl Editor {
|
|||
}
|
||||
chunk.text
|
||||
})
|
||||
.collect();
|
||||
.collect::<String>()
|
||||
.into();
|
||||
|
||||
drop(buffer);
|
||||
|
||||
|
@ -4686,7 +4677,7 @@ impl Editor {
|
|||
}
|
||||
editor
|
||||
.buffer
|
||||
.update(cx, |buffer, cx| buffer.edit([0..0], &old_name, cx));
|
||||
.update(cx, |buffer, cx| buffer.edit(0..0, old_name.clone(), cx));
|
||||
editor.select_all(&SelectAll, cx);
|
||||
editor
|
||||
});
|
||||
|
@ -5608,7 +5599,7 @@ impl Editor {
|
|||
self.buffer.read(cx).read(cx).text()
|
||||
}
|
||||
|
||||
pub fn set_text(&mut self, text: impl Into<String>, cx: &mut ViewContext<Self>) {
|
||||
pub fn set_text(&mut self, text: impl Into<Arc<str>>, cx: &mut ViewContext<Self>) {
|
||||
self.transact(cx, |this, cx| {
|
||||
this.buffer
|
||||
.read(cx)
|
||||
|
@ -6613,8 +6604,8 @@ mod tests {
|
|||
// Simulate an edit in another editor
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.start_transaction_at(now, cx);
|
||||
buffer.edit([0..1], "a", cx);
|
||||
buffer.edit([1..1], "b", cx);
|
||||
buffer.edit(0..1, "a", cx);
|
||||
buffer.edit(1..1, "b", cx);
|
||||
buffer.end_transaction_at(now, cx);
|
||||
});
|
||||
|
||||
|
@ -6957,12 +6948,11 @@ mod tests {
|
|||
let (_, view) = cx.add_window(Default::default(), |cx| build_editor(buffer.clone(), cx));
|
||||
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
buffer.edit_batched(
|
||||
vec![
|
||||
Point::new(1, 0)..Point::new(1, 0),
|
||||
Point::new(1, 1)..Point::new(1, 1),
|
||||
(Point::new(1, 0)..Point::new(1, 0), "\t"),
|
||||
(Point::new(1, 1)..Point::new(1, 1), "\t"),
|
||||
],
|
||||
"\t",
|
||||
cx,
|
||||
);
|
||||
});
|
||||
|
@ -7595,12 +7585,11 @@ mod tests {
|
|||
|
||||
// Edit the buffer directly, deleting ranges surrounding the editor's selections
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
buffer.edit_batched(
|
||||
[
|
||||
Point::new(1, 2)..Point::new(3, 0),
|
||||
Point::new(4, 2)..Point::new(6, 0),
|
||||
(Point::new(1, 2)..Point::new(3, 0), ""),
|
||||
(Point::new(4, 2)..Point::new(6, 0), ""),
|
||||
],
|
||||
"",
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -7659,7 +7648,7 @@ mod tests {
|
|||
|
||||
// Edit the buffer directly, deleting ranges surrounding the editor's selections
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit([2..5, 10..13, 18..21], "", cx);
|
||||
buffer.edit_batched([(2..5, ""), (10..13, ""), (18..21, "")], cx);
|
||||
assert_eq!(buffer.read(cx).text(), "a(), b(), c()".unindent());
|
||||
});
|
||||
|
||||
|
|
|
@ -255,38 +255,41 @@ impl MultiBuffer {
|
|||
self.subscriptions.subscribe()
|
||||
}
|
||||
|
||||
pub fn edit<I, S, T>(&mut self, ranges: I, new_text: T, cx: &mut ModelContext<Self>)
|
||||
pub fn edit<S, T>(&mut self, range: Range<S>, new_text: T, cx: &mut ModelContext<Self>)
|
||||
where
|
||||
I: IntoIterator<Item = Range<S>>,
|
||||
S: ToOffset,
|
||||
T: Into<String>,
|
||||
T: Into<Arc<str>>,
|
||||
{
|
||||
self.edit_internal(ranges, new_text, false, cx)
|
||||
self.edit_internal([(range, new_text)], false, cx);
|
||||
}
|
||||
|
||||
pub fn edit_with_autoindent<I, S, T>(
|
||||
&mut self,
|
||||
ranges: I,
|
||||
new_text: T,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) where
|
||||
I: IntoIterator<Item = Range<S>>,
|
||||
pub fn edit_batched<I, S, T>(&mut self, edits: I, cx: &mut ModelContext<Self>)
|
||||
where
|
||||
I: IntoIterator<Item = (Range<S>, T)>,
|
||||
S: ToOffset,
|
||||
T: Into<String>,
|
||||
T: Into<Arc<str>>,
|
||||
{
|
||||
self.edit_internal(ranges, new_text, true, cx)
|
||||
self.edit_internal(edits, false, cx)
|
||||
}
|
||||
|
||||
pub fn edit_with_autoindent_batched<I, S, T>(&mut self, edits: I, cx: &mut ModelContext<Self>)
|
||||
where
|
||||
I: IntoIterator<Item = (Range<S>, T)>,
|
||||
S: ToOffset,
|
||||
T: Into<Arc<str>>,
|
||||
{
|
||||
self.edit_internal(edits, true, cx)
|
||||
}
|
||||
|
||||
pub fn edit_internal<I, S, T>(
|
||||
&mut self,
|
||||
ranges_iter: I,
|
||||
new_text: T,
|
||||
edits_iter: I,
|
||||
autoindent: bool,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) where
|
||||
I: IntoIterator<Item = Range<S>>,
|
||||
I: IntoIterator<Item = (Range<S>, T)>,
|
||||
S: ToOffset,
|
||||
T: Into<String>,
|
||||
T: Into<Arc<str>>,
|
||||
{
|
||||
if self.buffers.borrow().is_empty() {
|
||||
return;
|
||||
|
@ -294,24 +297,29 @@ impl MultiBuffer {
|
|||
|
||||
if let Some(buffer) = self.as_singleton() {
|
||||
let snapshot = self.read(cx);
|
||||
let ranges = ranges_iter
|
||||
.into_iter()
|
||||
.map(|range| range.start.to_offset(&snapshot)..range.end.to_offset(&snapshot));
|
||||
let edits = edits_iter.into_iter().map(|(range, new_text)| {
|
||||
(
|
||||
range.start.to_offset(&snapshot)..range.end.to_offset(&snapshot),
|
||||
new_text,
|
||||
)
|
||||
});
|
||||
return buffer.update(cx, |buffer, cx| {
|
||||
let language_name = buffer.language().map(|language| language.name());
|
||||
let indent_size = cx.global::<Settings>().tab_size(language_name.as_deref());
|
||||
if autoindent {
|
||||
buffer.edit_with_autoindent(ranges, new_text, indent_size, cx);
|
||||
buffer.edit_with_autoindent_batched(edits, indent_size, cx);
|
||||
} else {
|
||||
buffer.edit(ranges, new_text, cx);
|
||||
buffer.edit_batched(edits, cx);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let snapshot = self.read(cx);
|
||||
let mut buffer_edits: HashMap<usize, Vec<(Range<usize>, bool)>> = Default::default();
|
||||
let mut buffer_edits: HashMap<usize, Vec<(Range<usize>, Arc<str>, bool)>> =
|
||||
Default::default();
|
||||
let mut cursor = snapshot.excerpts.cursor::<usize>();
|
||||
for range in ranges_iter {
|
||||
for (range, new_text) in edits_iter {
|
||||
let new_text: Arc<str> = new_text.into();
|
||||
let start = range.start.to_offset(&snapshot);
|
||||
let end = range.end.to_offset(&snapshot);
|
||||
cursor.seek(&start, Bias::Right, &());
|
||||
|
@ -335,7 +343,7 @@ impl MultiBuffer {
|
|||
buffer_edits
|
||||
.entry(start_excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((buffer_start..buffer_end, true));
|
||||
.push((buffer_start..buffer_end, new_text, true));
|
||||
} else {
|
||||
let start_excerpt_range =
|
||||
buffer_start..start_excerpt.range.end.to_offset(&start_excerpt.buffer);
|
||||
|
@ -344,11 +352,11 @@ impl MultiBuffer {
|
|||
buffer_edits
|
||||
.entry(start_excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((start_excerpt_range, true));
|
||||
.push((start_excerpt_range, new_text.clone(), true));
|
||||
buffer_edits
|
||||
.entry(end_excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((end_excerpt_range, false));
|
||||
.push((end_excerpt_range, new_text.clone(), false));
|
||||
|
||||
cursor.seek(&start, Bias::Right, &());
|
||||
cursor.next(&());
|
||||
|
@ -359,25 +367,31 @@ impl MultiBuffer {
|
|||
buffer_edits
|
||||
.entry(excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((excerpt.range.to_offset(&excerpt.buffer), false));
|
||||
.push((
|
||||
excerpt.range.to_offset(&excerpt.buffer),
|
||||
new_text.clone(),
|
||||
false,
|
||||
));
|
||||
cursor.next(&());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let new_text = new_text.into();
|
||||
for (buffer_id, mut edits) in buffer_edits {
|
||||
edits.sort_unstable_by_key(|(range, _)| range.start);
|
||||
edits.sort_unstable_by_key(|(range, _, _)| range.start);
|
||||
self.buffers.borrow()[&buffer_id]
|
||||
.buffer
|
||||
.update(cx, |buffer, cx| {
|
||||
let mut edits = edits.into_iter().peekable();
|
||||
let mut insertions = Vec::new();
|
||||
let mut deletions = Vec::new();
|
||||
while let Some((mut range, mut is_insertion)) = edits.next() {
|
||||
while let Some((next_range, next_is_insertion)) = edits.peek() {
|
||||
while let Some((mut range, mut new_text, mut is_insertion)) = edits.next() {
|
||||
while let Some((next_range, next_new_text, next_is_insertion)) =
|
||||
edits.peek()
|
||||
{
|
||||
if range.end >= next_range.start {
|
||||
range.end = cmp::max(next_range.end, range.end);
|
||||
new_text = format!("{new_text}{next_new_text}").into();
|
||||
is_insertion |= *next_is_insertion;
|
||||
edits.next();
|
||||
} else {
|
||||
|
@ -386,24 +400,26 @@ impl MultiBuffer {
|
|||
}
|
||||
|
||||
if is_insertion {
|
||||
insertions.push(
|
||||
insertions.push((
|
||||
buffer.anchor_before(range.start)..buffer.anchor_before(range.end),
|
||||
);
|
||||
new_text,
|
||||
));
|
||||
} else if !range.is_empty() {
|
||||
deletions.push(
|
||||
deletions.push((
|
||||
buffer.anchor_before(range.start)..buffer.anchor_before(range.end),
|
||||
);
|
||||
"",
|
||||
));
|
||||
}
|
||||
}
|
||||
let language_name = buffer.language().map(|l| l.name());
|
||||
let indent_size = cx.global::<Settings>().tab_size(language_name.as_deref());
|
||||
|
||||
if autoindent {
|
||||
buffer.edit_with_autoindent(deletions, "", indent_size, cx);
|
||||
buffer.edit_with_autoindent(insertions, new_text.clone(), indent_size, cx);
|
||||
buffer.edit_with_autoindent_batched(deletions, indent_size, cx);
|
||||
buffer.edit_with_autoindent_batched(insertions, indent_size, cx);
|
||||
} else {
|
||||
buffer.edit(deletions, "", cx);
|
||||
buffer.edit(insertions, new_text.clone(), cx);
|
||||
buffer.edit_batched(deletions, cx);
|
||||
buffer.edit_batched(insertions, cx);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -1249,28 +1265,34 @@ impl MultiBuffer {
|
|||
pub fn randomly_edit(
|
||||
&mut self,
|
||||
rng: &mut impl rand::Rng,
|
||||
count: usize,
|
||||
edit_count: usize,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) {
|
||||
use text::RandomCharIter;
|
||||
|
||||
let snapshot = self.read(cx);
|
||||
let mut old_ranges: Vec<Range<usize>> = Vec::new();
|
||||
for _ in 0..count {
|
||||
let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
|
||||
if last_end > snapshot.len() {
|
||||
let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
|
||||
let mut last_end = None;
|
||||
for _ in 0..edit_count {
|
||||
if last_end.map_or(false, |last_end| last_end >= snapshot.len()) {
|
||||
break;
|
||||
}
|
||||
let end_ix = snapshot.clip_offset(rng.gen_range(0..=last_end), Bias::Right);
|
||||
let start_ix = snapshot.clip_offset(rng.gen_range(0..=end_ix), Bias::Left);
|
||||
old_ranges.push(start_ix..end_ix);
|
||||
|
||||
let new_start = last_end.map_or(0, |last_end| last_end + 1);
|
||||
let end = snapshot.clip_offset(rng.gen_range(new_start..=snapshot.len()), Bias::Right);
|
||||
let start = snapshot.clip_offset(rng.gen_range(new_start..=end), Bias::Right);
|
||||
last_end = Some(end);
|
||||
let range = start..end;
|
||||
|
||||
let new_text_len = rng.gen_range(0..10);
|
||||
let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
|
||||
|
||||
edits.push((range, new_text.into()));
|
||||
}
|
||||
let new_text_len = rng.gen_range(0..10);
|
||||
let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
|
||||
log::info!("mutating multi-buffer at {:?}: {:?}", old_ranges, new_text);
|
||||
log::info!("mutating multi-buffer with {:?}", edits);
|
||||
drop(snapshot);
|
||||
|
||||
self.edit(old_ranges.iter().cloned(), new_text.as_str(), cx);
|
||||
self.edit_batched(edits, cx);
|
||||
}
|
||||
|
||||
pub fn randomly_edit_excerpts(
|
||||
|
@ -2950,7 +2972,7 @@ mod tests {
|
|||
.collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
buffer.update(cx, |buffer, cx| buffer.edit([1..3], "XXX\n", cx));
|
||||
buffer.update(cx, |buffer, cx| buffer.edit(1..3, "XXX\n", cx));
|
||||
let snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
|
||||
assert_eq!(snapshot.text(), buffer.read(cx).text());
|
||||
|
@ -2973,11 +2995,11 @@ mod tests {
|
|||
let snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
assert_eq!(snapshot.text(), "a");
|
||||
|
||||
guest_buffer.update(cx, |buffer, cx| buffer.edit([1..1], "b", cx));
|
||||
guest_buffer.update(cx, |buffer, cx| buffer.edit(1..1, "b", cx));
|
||||
let snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
assert_eq!(snapshot.text(), "ab");
|
||||
|
||||
guest_buffer.update(cx, |buffer, cx| buffer.edit([2..2], "c", cx));
|
||||
guest_buffer.update(cx, |buffer, cx| buffer.edit(2..2, "c", cx));
|
||||
let snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
assert_eq!(snapshot.text(), "abc");
|
||||
}
|
||||
|
@ -3091,12 +3113,12 @@ mod tests {
|
|||
);
|
||||
|
||||
buffer_1.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
let text = "\n";
|
||||
buffer.edit_batched(
|
||||
[
|
||||
Point::new(0, 0)..Point::new(0, 0),
|
||||
Point::new(2, 1)..Point::new(2, 3),
|
||||
(Point::new(0, 0)..Point::new(0, 0), text),
|
||||
(Point::new(2, 1)..Point::new(2, 3), text),
|
||||
],
|
||||
"\n",
|
||||
cx,
|
||||
);
|
||||
});
|
||||
|
@ -3234,8 +3256,8 @@ mod tests {
|
|||
let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer.clone(), cx));
|
||||
let old_snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit([0..0], "X", cx);
|
||||
buffer.edit([5..5], "Y", cx);
|
||||
buffer.edit(0..0, "X", cx);
|
||||
buffer.edit(5..5, "Y", cx);
|
||||
});
|
||||
let new_snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
|
||||
|
@ -3268,12 +3290,12 @@ mod tests {
|
|||
assert_eq!(Anchor::max().to_offset(&old_snapshot), 10);
|
||||
|
||||
buffer_1.update(cx, |buffer, cx| {
|
||||
buffer.edit([0..0], "W", cx);
|
||||
buffer.edit([5..5], "X", cx);
|
||||
buffer.edit(0..0, "W", cx);
|
||||
buffer.edit(5..5, "X", cx);
|
||||
});
|
||||
buffer_2.update(cx, |buffer, cx| {
|
||||
buffer.edit([0..0], "Y", cx);
|
||||
buffer.edit([6..0], "Z", cx);
|
||||
buffer.edit(0..0, "Y", cx);
|
||||
buffer.edit(6..0, "Z", cx);
|
||||
});
|
||||
let new_snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
|
||||
|
@ -3302,7 +3324,7 @@ mod tests {
|
|||
|
||||
// Create an insertion id in buffer 1 that doesn't exist in buffer 2.
|
||||
// Add an excerpt from buffer 1 that spans this new insertion.
|
||||
buffer_1.update(cx, |buffer, cx| buffer.edit([4..4], "123", cx));
|
||||
buffer_1.update(cx, |buffer, cx| buffer.edit(4..4, "123", cx));
|
||||
let excerpt_id_1 = multibuffer.update(cx, |multibuffer, cx| {
|
||||
multibuffer
|
||||
.push_excerpts(buffer_1.clone(), [0..7], cx)
|
||||
|
@ -3821,20 +3843,18 @@ mod tests {
|
|||
|
||||
multibuffer.update(cx, |multibuffer, cx| {
|
||||
multibuffer.start_transaction_at(now, cx);
|
||||
multibuffer.edit(
|
||||
multibuffer.edit_batched(
|
||||
[
|
||||
Point::new(0, 0)..Point::new(0, 0),
|
||||
Point::new(1, 0)..Point::new(1, 0),
|
||||
(Point::new(0, 0)..Point::new(0, 0), "A"),
|
||||
(Point::new(1, 0)..Point::new(1, 0), "A"),
|
||||
],
|
||||
"A",
|
||||
cx,
|
||||
);
|
||||
multibuffer.edit(
|
||||
multibuffer.edit_batched(
|
||||
[
|
||||
Point::new(0, 1)..Point::new(0, 1),
|
||||
Point::new(1, 1)..Point::new(1, 1),
|
||||
(Point::new(0, 1)..Point::new(0, 1), "B"),
|
||||
(Point::new(1, 1)..Point::new(1, 1), "B"),
|
||||
],
|
||||
"B",
|
||||
cx,
|
||||
);
|
||||
multibuffer.end_transaction_at(now, cx);
|
||||
|
@ -3843,19 +3863,19 @@ mod tests {
|
|||
// Edit buffer 1 through the multibuffer
|
||||
now += 2 * group_interval;
|
||||
multibuffer.start_transaction_at(now, cx);
|
||||
multibuffer.edit([2..2], "C", cx);
|
||||
multibuffer.edit(2..2, "C", cx);
|
||||
multibuffer.end_transaction_at(now, cx);
|
||||
assert_eq!(multibuffer.read(cx).text(), "ABC1234\nAB5678");
|
||||
|
||||
// Edit buffer 1 independently
|
||||
buffer_1.update(cx, |buffer_1, cx| {
|
||||
buffer_1.start_transaction_at(now);
|
||||
buffer_1.edit([3..3], "D", cx);
|
||||
buffer_1.edit(3..3, "D", cx);
|
||||
buffer_1.end_transaction_at(now, cx);
|
||||
|
||||
now += 2 * group_interval;
|
||||
buffer_1.start_transaction_at(now);
|
||||
buffer_1.edit([4..4], "E", cx);
|
||||
buffer_1.edit(4..4, "E", cx);
|
||||
buffer_1.end_transaction_at(now, cx);
|
||||
});
|
||||
assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\nAB5678");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue