Consolidate edit and edit_batched functions

This commit is contained in:
Keith Simmons 2022-04-28 16:52:04 -07:00
parent 74b467aaa8
commit b4b61b4bbc
12 changed files with 191 additions and 221 deletions

View file

@ -1635,8 +1635,8 @@ mod tests {
.update(cx_c, |p, cx| p.open_buffer((worktree_id, "file1"), cx)) .update(cx_c, |p, cx| p.open_buffer((worktree_id, "file1"), cx))
.await .await
.unwrap(); .unwrap();
buffer_b.update(cx_b, |buf, cx| buf.edit(0..0, "i-am-b, ", cx)); buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "i-am-b, ")], cx));
buffer_c.update(cx_c, |buf, cx| buf.edit(0..0, "i-am-c, ", cx)); buffer_c.update(cx_c, |buf, cx| buf.edit([(0..0, "i-am-c, ")], cx));
// Open and edit that buffer as the host. // Open and edit that buffer as the host.
let buffer_a = project_a let buffer_a = project_a
@ -1647,7 +1647,9 @@ mod tests {
buffer_a buffer_a
.condition(cx_a, |buf, _| buf.text() == "i-am-c, i-am-b, ") .condition(cx_a, |buf, _| buf.text() == "i-am-c, i-am-b, ")
.await; .await;
buffer_a.update(cx_a, |buf, cx| buf.edit(buf.len()..buf.len(), "i-am-a", cx)); buffer_a.update(cx_a, |buf, cx| {
buf.edit([(buf.len()..buf.len(), "i-am-a")], cx)
});
// Wait for edits to propagate // Wait for edits to propagate
buffer_a buffer_a
@ -1662,7 +1664,7 @@ mod tests {
// Edit the buffer as the host and concurrently save as guest B. // Edit the buffer as the host and concurrently save as guest B.
let save_b = buffer_b.update(cx_b, |buf, cx| buf.save(cx)); let save_b = buffer_b.update(cx_b, |buf, cx| buf.save(cx));
buffer_a.update(cx_a, |buf, cx| buf.edit(0..0, "hi-a, ", cx)); buffer_a.update(cx_a, |buf, cx| buf.edit([(0..0, "hi-a, ")], cx));
save_b.await.unwrap(); save_b.await.unwrap();
assert_eq!( assert_eq!(
fs.load("/a/file1".as_ref()).await.unwrap(), fs.load("/a/file1".as_ref()).await.unwrap(),
@ -1792,7 +1794,7 @@ mod tests {
.await .await
.unwrap(); .unwrap();
buffer_b.update(cx_b, |buf, cx| buf.edit(0..0, "world ", cx)); buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "world ")], cx));
buffer_b.read_with(cx_b, |buf, _| { buffer_b.read_with(cx_b, |buf, _| {
assert!(buf.is_dirty()); assert!(buf.is_dirty());
assert!(!buf.has_conflict()); assert!(!buf.has_conflict());
@ -1806,7 +1808,7 @@ mod tests {
assert!(!buf.has_conflict()); assert!(!buf.has_conflict());
}); });
buffer_b.update(cx_b, |buf, cx| buf.edit(0..0, "hello ", cx)); buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "hello ")], cx));
buffer_b.read_with(cx_b, |buf, _| { buffer_b.read_with(cx_b, |buf, _| {
assert!(buf.is_dirty()); assert!(buf.is_dirty());
assert!(!buf.has_conflict()); assert!(!buf.has_conflict());
@ -1962,9 +1964,9 @@ mod tests {
// Edit the buffer as client A while client B is still opening it. // Edit the buffer as client A while client B is still opening it.
cx_b.background().simulate_random_delay().await; cx_b.background().simulate_random_delay().await;
buffer_a.update(cx_a, |buf, cx| buf.edit(0..0, "X", cx)); buffer_a.update(cx_a, |buf, cx| buf.edit([(0..0, "X")], cx));
cx_b.background().simulate_random_delay().await; cx_b.background().simulate_random_delay().await;
buffer_a.update(cx_a, |buf, cx| buf.edit(1..1, "Y", cx)); buffer_a.update(cx_a, |buf, cx| buf.edit([(1..1, "Y")], cx));
let text = buffer_a.read_with(cx_a, |buf, _| buf.text()); let text = buffer_a.read_with(cx_a, |buf, _| buf.text());
let buffer_b = buffer_b.await.unwrap(); let buffer_b = buffer_b.await.unwrap();
@ -2650,8 +2652,8 @@ mod tests {
.await .await
.unwrap(); .unwrap();
buffer_b.update(cx_b, |buffer, cx| { buffer_b.update(cx_b, |buffer, cx| {
buffer.edit(4..7, "six", cx); buffer.edit([(4..7, "six")], cx);
buffer.edit(10..11, "6", cx); buffer.edit([(10..11, "6")], cx);
assert_eq!(buffer.text(), "let six = 6;"); assert_eq!(buffer.text(), "let six = 6;");
assert!(buffer.is_dirty()); assert!(buffer.is_dirty());
assert!(!buffer.has_conflict()); assert!(!buffer.has_conflict());
@ -3932,7 +3934,7 @@ mod tests {
); );
rename.editor.update(cx, |rename_editor, cx| { rename.editor.update(cx, |rename_editor, cx| {
rename_editor.buffer().update(cx, |rename_buffer, cx| { rename_editor.buffer().update(cx, |rename_buffer, cx| {
rename_buffer.edit(0..3, "THREE", cx); rename_buffer.edit([(0..3, "THREE")], cx);
}); });
}); });
}); });

View file

@ -843,7 +843,7 @@ pub mod tests {
let ix = snapshot.buffer_snapshot.text().find("seven").unwrap(); let ix = snapshot.buffer_snapshot.text().find("seven").unwrap();
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit(ix..ix, "and ", cx); buffer.edit([(ix..ix, "and ")], cx);
}); });
let snapshot = map.update(cx, |map, cx| map.snapshot(cx)); let snapshot = map.update(cx, |map, cx| map.snapshot(cx));
@ -876,7 +876,7 @@ pub mod tests {
let map = let map =
cx.add_model(|cx| DisplayMap::new(buffer.clone(), font_id, font_size, None, 1, 1, cx)); cx.add_model(|cx| DisplayMap::new(buffer.clone(), font_id, font_size, None, 1, 1, cx));
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit_batched( buffer.edit(
vec![ vec![
(Point::new(1, 0)..Point::new(1, 0), "\t"), (Point::new(1, 0)..Point::new(1, 0), "\t"),
(Point::new(1, 1)..Point::new(1, 1), "\t"), (Point::new(1, 1)..Point::new(1, 1), "\t"),

View file

@ -1154,7 +1154,7 @@ mod tests {
// Insert a line break, separating two block decorations into separate lines. // Insert a line break, separating two block decorations into separate lines.
let buffer_snapshot = buffer.update(cx, |buffer, cx| { 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) buffer.snapshot(cx)
}); });

View file

@ -1246,7 +1246,7 @@ mod tests {
); );
let buffer_snapshot = buffer.update(cx, |buffer, cx| { let buffer_snapshot = buffer.update(cx, |buffer, cx| {
buffer.edit_batched( buffer.edit(
vec![ vec![
(Point::new(0, 0)..Point::new(0, 1), "123"), (Point::new(0, 0)..Point::new(0, 1), "123"),
(Point::new(2, 3)..Point::new(2, 3), "123"), (Point::new(2, 3)..Point::new(2, 3), "123"),
@ -1273,7 +1273,7 @@ mod tests {
); );
let buffer_snapshot = buffer.update(cx, |buffer, cx| { let buffer_snapshot = buffer.update(cx, |buffer, cx| {
buffer.edit(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) buffer.snapshot(cx)
}); });
let (snapshot4, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner()); let (snapshot4, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner());
@ -1329,7 +1329,7 @@ mod tests {
// Edit within one of the folds. // Edit within one of the folds.
let buffer_snapshot = buffer.update(cx, |buffer, cx| { let buffer_snapshot = buffer.update(cx, |buffer, cx| {
buffer.edit(0..1, "12345", cx); buffer.edit([(0..1, "12345")], cx);
buffer.snapshot(cx) buffer.snapshot(cx)
}); });
let (snapshot, _) = let (snapshot, _) =
@ -1371,7 +1371,7 @@ mod tests {
assert_eq!(snapshot.text(), "aa…cccc\nd…eeeee"); assert_eq!(snapshot.text(), "aa…cccc\nd…eeeee");
let buffer_snapshot = buffer.update(cx, |buffer, cx| { let buffer_snapshot = buffer.update(cx, |buffer, cx| {
buffer.edit(Point::new(2, 2)..Point::new(3, 1), "", cx); buffer.edit([(Point::new(2, 2)..Point::new(3, 1), "")], cx);
buffer.snapshot(cx) buffer.snapshot(cx)
}); });
let (snapshot, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner()); let (snapshot, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner());

View file

@ -1918,7 +1918,7 @@ impl Editor {
}; };
this.buffer.update(cx, |buffer, cx| { this.buffer.update(cx, |buffer, cx| {
buffer.edit_with_autoindent_batched(edits, cx); buffer.edit_with_autoindent(edits, cx);
let buffer = buffer.read(cx); let buffer = buffer.read(cx);
this.selections = this this.selections = this
@ -1955,7 +1955,7 @@ impl Editor {
.map(|s| (s.id, s.goal, snapshot.anchor_after(s.end))) .map(|s| (s.id, s.goal, snapshot.anchor_after(s.end)))
.collect::<Vec<_>>() .collect::<Vec<_>>()
}; };
buffer.edit_with_autoindent_batched( buffer.edit_with_autoindent(
old_selections.iter().map(|s| (s.start..s.end, text)), old_selections.iter().map(|s| (s.start..s.end, text)),
cx, cx,
); );
@ -2017,14 +2017,14 @@ impl Editor {
self.buffer.update(cx, |buffer, cx| { self.buffer.update(cx, |buffer, cx| {
let pair_start: Arc<str> = pair.start.clone().into(); let pair_start: Arc<str> = pair.start.clone().into();
buffer.edit_batched( buffer.edit(
selections selections
.iter() .iter()
.map(|s| (s.start.clone()..s.start.clone(), pair_start.clone())), .map(|s| (s.start.clone()..s.start.clone(), pair_start.clone())),
cx, cx,
); );
let pair_end: Arc<str> = pair.end.clone().into(); let pair_end: Arc<str> = pair.end.clone().into();
buffer.edit_batched( buffer.edit(
selections selections
.iter() .iter()
.map(|s| (s.end.clone()..s.end.clone(), pair_end.clone())), .map(|s| (s.end.clone()..s.end.clone(), pair_end.clone())),
@ -2105,7 +2105,7 @@ impl Editor {
.collect::<SmallVec<[_; 32]>>(); .collect::<SmallVec<[_; 32]>>();
let pair_end: Arc<str> = pair.end.clone().into(); let pair_end: Arc<str> = pair.end.clone().into();
buffer.edit_batched( buffer.edit(
selection_ranges selection_ranges
.iter() .iter()
.map(|range| (range.clone(), pair_end.clone())), .map(|range| (range.clone(), pair_end.clone())),
@ -2369,10 +2369,8 @@ impl Editor {
this.insert_snippet(&ranges, snippet, cx).log_err(); this.insert_snippet(&ranges, snippet, cx).log_err();
} else { } else {
this.buffer.update(cx, |buffer, cx| { this.buffer.update(cx, |buffer, cx| {
buffer.edit_with_autoindent_batched( buffer
ranges.iter().map(|range| (range.clone(), text)), .edit_with_autoindent(ranges.iter().map(|range| (range.clone(), text)), cx);
cx,
);
}); });
} }
}); });
@ -2725,7 +2723,7 @@ impl Editor {
) -> Result<()> { ) -> Result<()> {
let tabstops = self.buffer.update(cx, |buffer, cx| { let tabstops = self.buffer.update(cx, |buffer, cx| {
let snippet_text: Arc<str> = snippet.text.clone().into(); let snippet_text: Arc<str> = snippet.text.clone().into();
buffer.edit_with_autoindent_batched( buffer.edit_with_autoindent(
insertion_ranges insertion_ranges
.iter() .iter()
.cloned() .cloned()
@ -2912,8 +2910,10 @@ impl Editor {
.count(); .count();
let chars_to_next_tab_stop = tab_size - (char_column as u32 % tab_size); let chars_to_next_tab_stop = tab_size - (char_column as u32 % tab_size);
buffer.edit( buffer.edit(
[(
selection.start..selection.start, selection.start..selection.start,
" ".repeat(chars_to_next_tab_stop as usize), " ".repeat(chars_to_next_tab_stop as usize),
)],
cx, cx,
); );
selection.start.column += chars_to_next_tab_stop; selection.start.column += chars_to_next_tab_stop;
@ -2963,8 +2963,10 @@ impl Editor {
let columns_to_next_tab_stop = tab_size - (indent_column % tab_size); let columns_to_next_tab_stop = tab_size - (indent_column % tab_size);
let row_start = Point::new(row, 0); let row_start = Point::new(row, 0);
buffer.edit( buffer.edit(
[(
row_start..row_start, row_start..row_start,
" ".repeat(columns_to_next_tab_stop as usize), " ".repeat(columns_to_next_tab_stop as usize),
)],
cx, cx,
); );
@ -3022,7 +3024,7 @@ impl Editor {
self.transact(cx, |this, cx| { self.transact(cx, |this, cx| {
this.buffer.update(cx, |buffer, cx| { this.buffer.update(cx, |buffer, cx| {
buffer.edit_batched(deletion_ranges.into_iter().map(|range| (range, "")), cx); buffer.edit(deletion_ranges.into_iter().map(|range| (range, "")), cx);
}); });
this.update_selections( this.update_selections(
this.local_selections::<usize>(cx), this.local_selections::<usize>(cx),
@ -3084,7 +3086,7 @@ impl Editor {
self.transact(cx, |this, cx| { self.transact(cx, |this, cx| {
let buffer = this.buffer.update(cx, |buffer, cx| { let buffer = this.buffer.update(cx, |buffer, cx| {
buffer.edit_batched(edit_ranges.into_iter().map(|range| (range, "")), cx); buffer.edit(edit_ranges.into_iter().map(|range| (range, "")), cx);
buffer.snapshot(cx) buffer.snapshot(cx)
}); });
let new_selections = new_cursors let new_selections = new_cursors
@ -3138,7 +3140,7 @@ impl Editor {
self.transact(cx, |this, cx| { self.transact(cx, |this, cx| {
this.buffer.update(cx, |buffer, cx| { this.buffer.update(cx, |buffer, cx| {
for (point, text, _) in edits.into_iter().rev() { for (point, text, _) in edits.into_iter().rev() {
buffer.edit(point..point, text, cx); buffer.edit([(point..point, text)], cx);
} }
}); });
@ -3248,7 +3250,7 @@ impl Editor {
this.unfold_ranges(unfold_ranges, true, cx); this.unfold_ranges(unfold_ranges, true, cx);
this.buffer.update(cx, |buffer, cx| { this.buffer.update(cx, |buffer, cx| {
for (range, text) in edits { for (range, text) in edits {
buffer.edit(range, text, cx); buffer.edit([(range, text)], cx);
} }
}); });
this.fold_ranges(refold_ranges, cx); this.fold_ranges(refold_ranges, cx);
@ -3351,7 +3353,7 @@ impl Editor {
this.unfold_ranges(unfold_ranges, true, cx); this.unfold_ranges(unfold_ranges, true, cx);
this.buffer.update(cx, |buffer, cx| { this.buffer.update(cx, |buffer, cx| {
for (range, text) in edits { for (range, text) in edits {
buffer.edit(range, text, cx); buffer.edit([(range, text)], cx);
} }
}); });
this.fold_ranges(refold_ranges, cx); this.fold_ranges(refold_ranges, cx);
@ -3467,7 +3469,7 @@ impl Editor {
}; };
delta += to_insert.len() as isize - range.len() as isize; 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.start += to_insert.len();
selection.end = selection.start; selection.end = selection.start;
}); });
@ -4254,10 +4256,7 @@ impl Editor {
if !edit_ranges.is_empty() { if !edit_ranges.is_empty() {
if all_selection_lines_are_comments { if all_selection_lines_are_comments {
buffer.edit_batched( buffer.edit(edit_ranges.iter().cloned().map(|range| (range, "")), cx);
edit_ranges.iter().cloned().map(|range| (range, "")),
cx,
);
} else { } else {
let min_column = let min_column =
edit_ranges.iter().map(|r| r.start.column).min().unwrap(); edit_ranges.iter().map(|r| r.start.column).min().unwrap();
@ -4265,7 +4264,7 @@ impl Editor {
let position = Point::new(range.start.row, min_column); let position = Point::new(range.start.row, min_column);
(position..position, full_comment_prefix.clone()) (position..position, full_comment_prefix.clone())
}); });
buffer.edit_batched(edits, cx); buffer.edit(edits, cx);
} }
} }
} }
@ -4669,7 +4668,7 @@ impl Editor {
} }
editor editor
.buffer .buffer
.update(cx, |buffer, cx| buffer.edit(0..0, old_name.clone(), cx)); .update(cx, |buffer, cx| buffer.edit([(0..0, old_name.clone())], cx));
editor.select_all(&SelectAll, cx); editor.select_all(&SelectAll, cx);
editor editor
}); });
@ -6596,8 +6595,8 @@ mod tests {
// Simulate an edit in another editor // Simulate an edit in another editor
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.start_transaction_at(now, cx); buffer.start_transaction_at(now, cx);
buffer.edit(0..1, "a", cx); buffer.edit([(0..1, "a")], cx);
buffer.edit(1..1, "b", cx); buffer.edit([(1..1, "b")], cx);
buffer.end_transaction_at(now, cx); buffer.end_transaction_at(now, cx);
}); });
@ -6940,7 +6939,7 @@ mod tests {
let (_, view) = cx.add_window(Default::default(), |cx| build_editor(buffer.clone(), cx)); let (_, view) = cx.add_window(Default::default(), |cx| build_editor(buffer.clone(), cx));
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit_batched( buffer.edit(
vec![ vec![
(Point::new(1, 0)..Point::new(1, 0), "\t"), (Point::new(1, 0)..Point::new(1, 0), "\t"),
(Point::new(1, 1)..Point::new(1, 1), "\t"), (Point::new(1, 1)..Point::new(1, 1), "\t"),
@ -7577,7 +7576,7 @@ mod tests {
// Edit the buffer directly, deleting ranges surrounding the editor's selections // Edit the buffer directly, deleting ranges surrounding the editor's selections
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit_batched( buffer.edit(
[ [
(Point::new(1, 2)..Point::new(3, 0), ""), (Point::new(1, 2)..Point::new(3, 0), ""),
(Point::new(4, 2)..Point::new(6, 0), ""), (Point::new(4, 2)..Point::new(6, 0), ""),
@ -7640,7 +7639,7 @@ mod tests {
// Edit the buffer directly, deleting ranges surrounding the editor's selections // Edit the buffer directly, deleting ranges surrounding the editor's selections
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit_batched([(2..5, ""), (10..13, ""), (18..21, "")], cx); buffer.edit([(2..5, ""), (10..13, ""), (18..21, "")], cx);
assert_eq!(buffer.read(cx).text(), "a(), b(), c()".unindent()); assert_eq!(buffer.read(cx).text(), "a(), b(), c()".unindent());
}); });

View file

@ -255,15 +255,7 @@ impl MultiBuffer {
self.subscriptions.subscribe() self.subscriptions.subscribe()
} }
pub fn edit<S, T>(&mut self, range: Range<S>, new_text: T, cx: &mut ModelContext<Self>) pub fn edit<I, S, T>(&mut self, edits: I, cx: &mut ModelContext<Self>)
where
S: ToOffset,
T: Into<Arc<str>>,
{
self.edit_internal([(range, new_text)], false, cx);
}
pub fn edit_batched<I, S, T>(&mut self, edits: I, cx: &mut ModelContext<Self>)
where where
I: IntoIterator<Item = (Range<S>, T)>, I: IntoIterator<Item = (Range<S>, T)>,
S: ToOffset, S: ToOffset,
@ -272,7 +264,7 @@ impl MultiBuffer {
self.edit_internal(edits, false, cx) self.edit_internal(edits, false, cx)
} }
pub fn edit_with_autoindent_batched<I, S, T>(&mut self, edits: I, cx: &mut ModelContext<Self>) pub fn edit_with_autoindent<I, S, T>(&mut self, edits: I, cx: &mut ModelContext<Self>)
where where
I: IntoIterator<Item = (Range<S>, T)>, I: IntoIterator<Item = (Range<S>, T)>,
S: ToOffset, S: ToOffset,
@ -307,9 +299,9 @@ impl MultiBuffer {
let language_name = buffer.language().map(|language| language.name()); let language_name = buffer.language().map(|language| language.name());
let indent_size = cx.global::<Settings>().tab_size(language_name.as_deref()); let indent_size = cx.global::<Settings>().tab_size(language_name.as_deref());
if autoindent { if autoindent {
buffer.edit_with_autoindent_batched(edits, indent_size, cx); buffer.edit_with_autoindent(edits, indent_size, cx);
} else { } else {
buffer.edit_batched(edits, cx); buffer.edit(edits, cx);
} }
}); });
} }
@ -415,11 +407,11 @@ impl MultiBuffer {
let indent_size = cx.global::<Settings>().tab_size(language_name.as_deref()); let indent_size = cx.global::<Settings>().tab_size(language_name.as_deref());
if autoindent { if autoindent {
buffer.edit_with_autoindent_batched(deletions, indent_size, cx); buffer.edit_with_autoindent(deletions, indent_size, cx);
buffer.edit_with_autoindent_batched(insertions, indent_size, cx); buffer.edit_with_autoindent(insertions, indent_size, cx);
} else { } else {
buffer.edit_batched(deletions, cx); buffer.edit(deletions, cx);
buffer.edit_batched(insertions, cx); buffer.edit(insertions, cx);
} }
}) })
} }
@ -1292,7 +1284,7 @@ impl MultiBuffer {
log::info!("mutating multi-buffer with {:?}", edits); log::info!("mutating multi-buffer with {:?}", edits);
drop(snapshot); drop(snapshot);
self.edit_batched(edits, cx); self.edit(edits, cx);
} }
pub fn randomly_edit_excerpts( pub fn randomly_edit_excerpts(
@ -2972,7 +2964,7 @@ mod tests {
.collect::<Vec<_>>() .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); let snapshot = multibuffer.read(cx).snapshot(cx);
assert_eq!(snapshot.text(), buffer.read(cx).text()); assert_eq!(snapshot.text(), buffer.read(cx).text());
@ -2995,11 +2987,11 @@ mod tests {
let snapshot = multibuffer.read(cx).snapshot(cx); let snapshot = multibuffer.read(cx).snapshot(cx);
assert_eq!(snapshot.text(), "a"); 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); let snapshot = multibuffer.read(cx).snapshot(cx);
assert_eq!(snapshot.text(), "ab"); 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); let snapshot = multibuffer.read(cx).snapshot(cx);
assert_eq!(snapshot.text(), "abc"); assert_eq!(snapshot.text(), "abc");
} }
@ -3114,7 +3106,7 @@ mod tests {
buffer_1.update(cx, |buffer, cx| { buffer_1.update(cx, |buffer, cx| {
let text = "\n"; let text = "\n";
buffer.edit_batched( buffer.edit(
[ [
(Point::new(0, 0)..Point::new(0, 0), text), (Point::new(0, 0)..Point::new(0, 0), text),
(Point::new(2, 1)..Point::new(2, 3), text), (Point::new(2, 1)..Point::new(2, 3), text),
@ -3256,8 +3248,8 @@ mod tests {
let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer.clone(), cx)); let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer.clone(), cx));
let old_snapshot = multibuffer.read(cx).snapshot(cx); let old_snapshot = multibuffer.read(cx).snapshot(cx);
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit(0..0, "X", cx); buffer.edit([(0..0, "X")], cx);
buffer.edit(5..5, "Y", cx); buffer.edit([(5..5, "Y")], cx);
}); });
let new_snapshot = multibuffer.read(cx).snapshot(cx); let new_snapshot = multibuffer.read(cx).snapshot(cx);
@ -3290,12 +3282,12 @@ mod tests {
assert_eq!(Anchor::max().to_offset(&old_snapshot), 10); assert_eq!(Anchor::max().to_offset(&old_snapshot), 10);
buffer_1.update(cx, |buffer, cx| { buffer_1.update(cx, |buffer, cx| {
buffer.edit(0..0, "W", cx); buffer.edit([(0..0, "W")], cx);
buffer.edit(5..5, "X", cx); buffer.edit([(5..5, "X")], cx);
}); });
buffer_2.update(cx, |buffer, cx| { buffer_2.update(cx, |buffer, cx| {
buffer.edit(0..0, "Y", cx); buffer.edit([(0..0, "Y")], cx);
buffer.edit(6..0, "Z", cx); buffer.edit([(6..0, "Z")], cx);
}); });
let new_snapshot = multibuffer.read(cx).snapshot(cx); let new_snapshot = multibuffer.read(cx).snapshot(cx);
@ -3324,7 +3316,7 @@ mod tests {
// Create an insertion id in buffer 1 that doesn't exist in buffer 2. // 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. // 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| { let excerpt_id_1 = multibuffer.update(cx, |multibuffer, cx| {
multibuffer multibuffer
.push_excerpts(buffer_1.clone(), [0..7], cx) .push_excerpts(buffer_1.clone(), [0..7], cx)
@ -3843,14 +3835,14 @@ mod tests {
multibuffer.update(cx, |multibuffer, cx| { multibuffer.update(cx, |multibuffer, cx| {
multibuffer.start_transaction_at(now, cx); multibuffer.start_transaction_at(now, cx);
multibuffer.edit_batched( multibuffer.edit(
[ [
(Point::new(0, 0)..Point::new(0, 0), "A"), (Point::new(0, 0)..Point::new(0, 0), "A"),
(Point::new(1, 0)..Point::new(1, 0), "A"), (Point::new(1, 0)..Point::new(1, 0), "A"),
], ],
cx, cx,
); );
multibuffer.edit_batched( multibuffer.edit(
[ [
(Point::new(0, 1)..Point::new(0, 1), "B"), (Point::new(0, 1)..Point::new(0, 1), "B"),
(Point::new(1, 1)..Point::new(1, 1), "B"), (Point::new(1, 1)..Point::new(1, 1), "B"),
@ -3863,19 +3855,19 @@ mod tests {
// Edit buffer 1 through the multibuffer // Edit buffer 1 through the multibuffer
now += 2 * group_interval; now += 2 * group_interval;
multibuffer.start_transaction_at(now, cx); multibuffer.start_transaction_at(now, cx);
multibuffer.edit(2..2, "C", cx); multibuffer.edit([(2..2, "C")], cx);
multibuffer.end_transaction_at(now, cx); multibuffer.end_transaction_at(now, cx);
assert_eq!(multibuffer.read(cx).text(), "ABC1234\nAB5678"); assert_eq!(multibuffer.read(cx).text(), "ABC1234\nAB5678");
// Edit buffer 1 independently // Edit buffer 1 independently
buffer_1.update(cx, |buffer_1, cx| { buffer_1.update(cx, |buffer_1, cx| {
buffer_1.start_transaction_at(now); 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); buffer_1.end_transaction_at(now, cx);
now += 2 * group_interval; now += 2 * group_interval;
buffer_1.start_transaction_at(now); 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); buffer_1.end_transaction_at(now, cx);
}); });
assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\nAB5678"); assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\nAB5678");

View file

@ -880,14 +880,18 @@ impl Buffer {
if column > current_column { if column > current_column {
let offset = Point::new(row, 0).to_offset(&*self); let offset = Point::new(row, 0).to_offset(&*self);
self.edit( self.edit(
[(
offset..offset, offset..offset,
" ".repeat((column - current_column) as usize), " ".repeat((column - current_column) as usize),
)],
cx, cx,
); );
} else if column < current_column { } else if column < current_column {
self.edit( self.edit(
[(
Point::new(row, 0)..Point::new(row, current_column - column), Point::new(row, 0)..Point::new(row, current_column - column),
"", "",
)],
cx, cx,
); );
} }
@ -925,13 +929,15 @@ impl Buffer {
match tag { match tag {
ChangeTag::Equal => offset += len, ChangeTag::Equal => offset += len,
ChangeTag::Delete => { ChangeTag::Delete => {
self.edit(range, "", cx); self.edit([(range, "")], cx);
} }
ChangeTag::Insert => { ChangeTag::Insert => {
self.edit( self.edit(
[(
offset..offset, offset..offset,
&diff.new_text &diff.new_text[range.start - diff.start_offset
[range.start - diff.start_offset..range.end - diff.start_offset], ..range.end - diff.start_offset],
)],
cx, cx,
); );
offset += len; offset += len;
@ -1054,20 +1060,7 @@ impl Buffer {
self.edit_internal([(0..self.len(), text)], None, cx) self.edit_internal([(0..self.len(), text)], None, cx)
} }
pub fn edit<S, T>( pub fn edit<I, S, T>(
&mut self,
range: Range<S>,
new_text: T,
cx: &mut ModelContext<Self>,
) -> Option<clock::Local>
where
S: ToOffset,
T: Into<Arc<str>>,
{
self.edit_batched([(range, new_text)], cx)
}
pub fn edit_batched<I, S, T>(
&mut self, &mut self,
edits_iter: I, edits_iter: I,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
@ -1080,21 +1073,7 @@ impl Buffer {
self.edit_internal(edits_iter, None, cx) self.edit_internal(edits_iter, None, cx)
} }
pub fn edit_with_autoindent<S, T>( pub fn edit_with_autoindent<I, S, T>(
&mut self,
range: Range<S>,
new_text: T,
indent_size: u32,
cx: &mut ModelContext<Self>,
) -> Option<clock::Local>
where
S: ToOffset,
T: Into<Arc<str>>,
{
self.edit_with_autoindent_batched([(range, new_text)], indent_size, cx)
}
pub fn edit_with_autoindent_batched<I, S, T>(
&mut self, &mut self,
edits_iter: I, edits_iter: I,
indent_size: u32, indent_size: u32,
@ -1165,7 +1144,7 @@ impl Buffer {
(before_edit, edited, autoindent_size) (before_edit, edited, autoindent_size)
}); });
let edit_operation = self.text.edit_batched(edits.iter().cloned()); let edit_operation = self.text.edit(edits.iter().cloned());
let edit_id = edit_operation.local_timestamp(); let edit_id = edit_operation.local_timestamp();
if let Some((before_edit, edited, size)) = autoindent_request { if let Some((before_edit, edited, size)) = autoindent_request {
@ -1475,7 +1454,7 @@ impl Buffer {
edits.push((range, new_text)); edits.push((range, new_text));
} }
log::info!("mutating buffer {} with {:?}", self.replica_id(), edits); log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
self.edit_batched(edits, cx); self.edit(edits, cx);
} }
pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) { pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {

View file

@ -93,7 +93,7 @@ fn test_edit_events(cx: &mut gpui::MutableAppContext) {
// An edit emits an edited event, followed by a dirtied event, // An edit emits an edited event, followed by a dirtied event,
// since the buffer was previously in a clean state. // since the buffer was previously in a clean state.
buffer.edit(2..4, "XYZ", cx); buffer.edit([(2..4, "XYZ")], cx);
// An empty transaction does not emit any events. // An empty transaction does not emit any events.
buffer.start_transaction(); buffer.start_transaction();
@ -102,8 +102,8 @@ fn test_edit_events(cx: &mut gpui::MutableAppContext) {
// A transaction containing two edits emits one edited event. // A transaction containing two edits emits one edited event.
now += Duration::from_secs(1); now += Duration::from_secs(1);
buffer.start_transaction_at(now); buffer.start_transaction_at(now);
buffer.edit(5..5, "u", cx); buffer.edit([(5..5, "u")], cx);
buffer.edit(6..6, "w", cx); buffer.edit([(6..6, "w")], cx);
buffer.end_transaction_at(now, cx); buffer.end_transaction_at(now, cx);
// Undoing a transaction emits one edited event. // Undoing a transaction emits one edited event.
@ -178,11 +178,11 @@ async fn test_reparse(cx: &mut gpui::TestAppContext) {
buf.start_transaction(); buf.start_transaction();
let offset = buf.text().find(")").unwrap(); let offset = buf.text().find(")").unwrap();
buf.edit(offset..offset, "b: C", cx); buf.edit([(offset..offset, "b: C")], cx);
assert!(!buf.is_parsing()); assert!(!buf.is_parsing());
let offset = buf.text().find("}").unwrap(); let offset = buf.text().find("}").unwrap();
buf.edit(offset..offset, " d; ", cx); buf.edit([(offset..offset, " d; ")], cx);
assert!(!buf.is_parsing()); assert!(!buf.is_parsing());
buf.end_transaction(cx); buf.end_transaction(cx);
@ -207,19 +207,19 @@ async fn test_reparse(cx: &mut gpui::TestAppContext) {
// * add a turbofish to the method call // * add a turbofish to the method call
buffer.update(cx, |buf, cx| { buffer.update(cx, |buf, cx| {
let offset = buf.text().find(";").unwrap(); let offset = buf.text().find(";").unwrap();
buf.edit(offset..offset, ".e", cx); buf.edit([(offset..offset, ".e")], cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e; }"); assert_eq!(buf.text(), "fn a(b: C) { d.e; }");
assert!(buf.is_parsing()); assert!(buf.is_parsing());
}); });
buffer.update(cx, |buf, cx| { buffer.update(cx, |buf, cx| {
let offset = buf.text().find(";").unwrap(); let offset = buf.text().find(";").unwrap();
buf.edit(offset..offset, "(f)", cx); buf.edit([(offset..offset, "(f)")], cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e(f); }"); assert_eq!(buf.text(), "fn a(b: C) { d.e(f); }");
assert!(buf.is_parsing()); assert!(buf.is_parsing());
}); });
buffer.update(cx, |buf, cx| { buffer.update(cx, |buf, cx| {
let offset = buf.text().find("(f)").unwrap(); let offset = buf.text().find("(f)").unwrap();
buf.edit(offset..offset, "::<G>", cx); buf.edit([(offset..offset, "::<G>")], cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e::<G>(f); }"); assert_eq!(buf.text(), "fn a(b: C) { d.e::<G>(f); }");
assert!(buf.is_parsing()); assert!(buf.is_parsing());
}); });
@ -576,13 +576,13 @@ fn test_edit_with_autoindent(cx: &mut MutableAppContext) {
let text = "fn a() {}"; let text = "fn a() {}";
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx); let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
buffer.edit_with_autoindent(8..8, "\n\n", 4, cx); buffer.edit_with_autoindent([(8..8, "\n\n")], 4, cx);
assert_eq!(buffer.text(), "fn a() {\n \n}"); assert_eq!(buffer.text(), "fn a() {\n \n}");
buffer.edit_with_autoindent(Point::new(1, 4)..Point::new(1, 4), "b()\n", 4, cx); buffer.edit_with_autoindent([(Point::new(1, 4)..Point::new(1, 4), "b()\n")], 4, cx);
assert_eq!(buffer.text(), "fn a() {\n b()\n \n}"); assert_eq!(buffer.text(), "fn a() {\n b()\n \n}");
buffer.edit_with_autoindent(Point::new(2, 4)..Point::new(2, 4), ".c", 4, cx); buffer.edit_with_autoindent([(Point::new(2, 4)..Point::new(2, 4), ".c")], 4, cx);
assert_eq!(buffer.text(), "fn a() {\n b()\n .c\n}"); assert_eq!(buffer.text(), "fn a() {\n b()\n .c\n}");
buffer buffer
@ -604,7 +604,7 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
// Lines 2 and 3 don't match the indentation suggestion. When editing these lines, // Lines 2 and 3 don't match the indentation suggestion. When editing these lines,
// their indentation is not adjusted. // their indentation is not adjusted.
buffer.edit_with_autoindent_batched( buffer.edit_with_autoindent(
[ [
(empty(Point::new(1, 1)), "()"), (empty(Point::new(1, 1)), "()"),
(empty(Point::new(2, 1)), "()"), (empty(Point::new(2, 1)), "()"),
@ -625,7 +625,7 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
// When appending new content after these lines, the indentation is based on the // When appending new content after these lines, the indentation is based on the
// preceding lines' actual indentation. // preceding lines' actual indentation.
buffer.edit_with_autoindent_batched( buffer.edit_with_autoindent(
[ [
(empty(Point::new(1, 1)), "\n.f\n.g"), (empty(Point::new(1, 1)), "\n.f\n.g"),
(empty(Point::new(2, 1)), "\n.f\n.g"), (empty(Point::new(2, 1)), "\n.f\n.g"),
@ -661,7 +661,7 @@ fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppConte
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx); let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
buffer.edit_with_autoindent(5..5, "\nb", 4, cx); buffer.edit_with_autoindent([(5..5, "\nb")], 4, cx);
assert_eq!( assert_eq!(
buffer.text(), buffer.text(),
" "
@ -673,7 +673,7 @@ fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppConte
// The indentation suggestion changed because `@end` node (a close paren) // The indentation suggestion changed because `@end` node (a close paren)
// is now at the beginning of the line. // is now at the beginning of the line.
buffer.edit_with_autoindent(Point::new(1, 4)..Point::new(1, 5), "", 4, cx); buffer.edit_with_autoindent([(Point::new(1, 4)..Point::new(1, 5), "")], 4, cx);
assert_eq!( assert_eq!(
buffer.text(), buffer.text(),
" "
@ -692,7 +692,7 @@ fn test_autoindent_with_edit_at_end_of_buffer(cx: &mut MutableAppContext) {
cx.add_model(|cx| { cx.add_model(|cx| {
let text = "a\nb"; let text = "a\nb";
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx); let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
buffer.edit_with_autoindent_batched([(0..1, "\n"), (2..3, "\n")], 4, cx); buffer.edit_with_autoindent([(0..1, "\n"), (2..3, "\n")], 4, cx);
assert_eq!(buffer.text(), "\n\n\n"); assert_eq!(buffer.text(), "\n\n\n");
buffer buffer
}); });
@ -704,18 +704,18 @@ fn test_serialization(cx: &mut gpui::MutableAppContext) {
let buffer1 = cx.add_model(|cx| { let buffer1 = cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "abc", cx); let mut buffer = Buffer::new(0, "abc", cx);
buffer.edit(3..3, "D", cx); buffer.edit([(3..3, "D")], cx);
now += Duration::from_secs(1); now += Duration::from_secs(1);
buffer.start_transaction_at(now); buffer.start_transaction_at(now);
buffer.edit(4..4, "E", cx); buffer.edit([(4..4, "E")], cx);
buffer.end_transaction_at(now, cx); buffer.end_transaction_at(now, cx);
assert_eq!(buffer.text(), "abcDE"); assert_eq!(buffer.text(), "abcDE");
buffer.undo(cx); buffer.undo(cx);
assert_eq!(buffer.text(), "abcD"); assert_eq!(buffer.text(), "abcD");
buffer.edit(4..4, "F", cx); buffer.edit([(4..4, "F")], cx);
assert_eq!(buffer.text(), "abcDF"); assert_eq!(buffer.text(), "abcDF");
buffer buffer
}); });

View file

@ -2230,7 +2230,7 @@ impl Project {
buffer.finalize_last_transaction(); buffer.finalize_last_transaction();
buffer.start_transaction(); buffer.start_transaction();
for (range, text) in edits { for (range, text) in edits {
buffer.edit(range, text, cx); buffer.edit([(range, text)], cx);
} }
if buffer.end_transaction(cx).is_some() { if buffer.end_transaction(cx).is_some() {
let transaction = buffer.finalize_last_transaction().unwrap().clone(); let transaction = buffer.finalize_last_transaction().unwrap().clone();
@ -2604,7 +2604,7 @@ impl Project {
buffer.finalize_last_transaction(); buffer.finalize_last_transaction();
buffer.start_transaction(); buffer.start_transaction();
for (range, text) in edits { for (range, text) in edits {
buffer.edit(range, text, cx); buffer.edit([(range, text)], cx);
} }
let transaction = if buffer.end_transaction(cx).is_some() { let transaction = if buffer.end_transaction(cx).is_some() {
let transaction = buffer.finalize_last_transaction().unwrap().clone(); let transaction = buffer.finalize_last_transaction().unwrap().clone();
@ -2960,7 +2960,7 @@ impl Project {
buffer.finalize_last_transaction(); buffer.finalize_last_transaction();
buffer.start_transaction(); buffer.start_transaction();
for (range, text) in edits { for (range, text) in edits {
buffer.edit(range, text, cx); buffer.edit([(range, text)], cx);
} }
let transaction = if buffer.end_transaction(cx).is_some() { let transaction = if buffer.end_transaction(cx).is_some() {
let transaction = buffer.finalize_last_transaction().unwrap().clone(); let transaction = buffer.finalize_last_transaction().unwrap().clone();
@ -5075,7 +5075,7 @@ mod tests {
}); });
// Edit a buffer. The changes are reported to the language server. // Edit a buffer. The changes are reported to the language server.
rust_buffer.update(cx, |buffer, cx| buffer.edit(16..16, "2", cx)); rust_buffer.update(cx, |buffer, cx| buffer.edit([(16..16, "2")], cx));
assert_eq!( assert_eq!(
fake_rust_server fake_rust_server
.receive_notification::<lsp::notification::DidChangeTextDocument>() .receive_notification::<lsp::notification::DidChangeTextDocument>()
@ -5132,8 +5132,8 @@ mod tests {
}); });
// Changes are reported only to servers matching the buffer's language. // Changes are reported only to servers matching the buffer's language.
toml_buffer.update(cx, |buffer, cx| buffer.edit(5..5, "23", cx)); toml_buffer.update(cx, |buffer, cx| buffer.edit([(5..5, "23")], cx));
rust_buffer2.update(cx, |buffer, cx| buffer.edit(0..0, "let x = 1;", cx)); rust_buffer2.update(cx, |buffer, cx| buffer.edit([(0..0, "let x = 1;")], cx));
assert_eq!( assert_eq!(
fake_rust_server fake_rust_server
.receive_notification::<lsp::notification::DidChangeTextDocument>() .receive_notification::<lsp::notification::DidChangeTextDocument>()
@ -5261,7 +5261,7 @@ mod tests {
}); });
// The renamed file's version resets after changing language server. // The renamed file's version resets after changing language server.
rust_buffer2.update(cx, |buffer, cx| buffer.edit(0..0, "// ", cx)); rust_buffer2.update(cx, |buffer, cx| buffer.edit([(0..0, "// ")], cx));
assert_eq!( assert_eq!(
fake_json_server fake_json_server
.receive_notification::<lsp::notification::DidChangeTextDocument>() .receive_notification::<lsp::notification::DidChangeTextDocument>()
@ -5730,7 +5730,7 @@ mod tests {
.await; .await;
// Edit the buffer, moving the content down // Edit the buffer, moving the content down
buffer.update(cx, |buffer, cx| buffer.edit(0..0, "\n\n", cx)); buffer.update(cx, |buffer, cx| buffer.edit([(0..0, "\n\n")], cx));
let change_notification_1 = fake_server let change_notification_1 = fake_server
.receive_notification::<lsp::notification::DidChangeTextDocument>() .receive_notification::<lsp::notification::DidChangeTextDocument>()
.await; .await;
@ -5901,9 +5901,9 @@ mod tests {
// Keep editing the buffer and ensure disk-based diagnostics get translated according to the // Keep editing the buffer and ensure disk-based diagnostics get translated according to the
// changes since the last save. // changes since the last save.
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit(Point::new(2, 0)..Point::new(2, 0), " ", cx); buffer.edit([(Point::new(2, 0)..Point::new(2, 0), " ")], cx);
buffer.edit(Point::new(2, 8)..Point::new(2, 10), "(x: usize)", cx); buffer.edit([(Point::new(2, 8)..Point::new(2, 10), "(x: usize)")], cx);
buffer.edit(Point::new(3, 10)..Point::new(3, 10), "xxx", cx); buffer.edit([(Point::new(3, 10)..Point::new(3, 10), "xxx")], cx);
}); });
let change_notification_2 = fake_server let change_notification_2 = fake_server
.receive_notification::<lsp::notification::DidChangeTextDocument>() .receive_notification::<lsp::notification::DidChangeTextDocument>()
@ -6116,18 +6116,24 @@ mod tests {
// Simulate editing the buffer after the language server computes some edits. // Simulate editing the buffer after the language server computes some edits.
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit( buffer.edit(
[(
Point::new(0, 0)..Point::new(0, 0), Point::new(0, 0)..Point::new(0, 0),
"// above first function\n", "// above first function\n",
)],
cx, cx,
); );
buffer.edit( buffer.edit(
[(
Point::new(2, 0)..Point::new(2, 0), Point::new(2, 0)..Point::new(2, 0),
" // inside first function\n", " // inside first function\n",
)],
cx, cx,
); );
buffer.edit( buffer.edit(
[(
Point::new(6, 4)..Point::new(6, 4), Point::new(6, 4)..Point::new(6, 4),
"// inside second function ", "// inside second function ",
)],
cx, cx,
); );
@ -6201,7 +6207,7 @@ mod tests {
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
for (range, new_text) in edits { for (range, new_text) in edits {
buffer.edit(range, new_text, cx); buffer.edit([(range, new_text)], cx);
} }
assert_eq!( assert_eq!(
buffer.text(), buffer.text(),
@ -6336,7 +6342,7 @@ mod tests {
); );
for (range, new_text) in edits { for (range, new_text) in edits {
buffer.edit(range, new_text, cx); buffer.edit([(range, new_text)], cx);
} }
assert_eq!( assert_eq!(
buffer.text(), buffer.text(),
@ -6662,7 +6668,7 @@ mod tests {
buffer buffer
.update(cx, |buffer, cx| { .update(cx, |buffer, cx| {
assert_eq!(buffer.text(), "the old contents"); assert_eq!(buffer.text(), "the old contents");
buffer.edit(0..0, "a line of text.\n".repeat(10 * 1024), cx); buffer.edit([(0..0, "a line of text.\n".repeat(10 * 1024))], cx);
buffer.save(cx) buffer.save(cx)
}) })
.await .await
@ -6699,7 +6705,7 @@ mod tests {
.unwrap(); .unwrap();
buffer buffer
.update(cx, |buffer, cx| { .update(cx, |buffer, cx| {
buffer.edit(0..0, "a line of text.\n".repeat(10 * 1024), cx); buffer.edit([(0..0, "a line of text.\n".repeat(10 * 1024))], cx);
buffer.save(cx) buffer.save(cx)
}) })
.await .await
@ -6727,7 +6733,7 @@ mod tests {
project.create_buffer("", None, cx).unwrap() project.create_buffer("", None, cx).unwrap()
}); });
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit(0..0, "abc", cx); buffer.edit([(0..0, "abc")], cx);
assert!(buffer.is_dirty()); assert!(buffer.is_dirty());
assert!(!buffer.has_conflict()); assert!(!buffer.has_conflict());
}); });
@ -7001,7 +7007,7 @@ mod tests {
assert!(!buffer.is_dirty()); assert!(!buffer.is_dirty());
assert!(events.borrow().is_empty()); assert!(events.borrow().is_empty());
buffer.edit(1..2, "", cx); buffer.edit([(1..2, "")], cx);
}); });
// after the first edit, the buffer is dirty, and emits a dirtied event. // after the first edit, the buffer is dirty, and emits a dirtied event.
@ -7022,8 +7028,8 @@ mod tests {
assert_eq!(*events.borrow(), &[language::Event::Saved]); assert_eq!(*events.borrow(), &[language::Event::Saved]);
events.borrow_mut().clear(); events.borrow_mut().clear();
buffer.edit(1..1, "B", cx); buffer.edit([(1..1, "B")], cx);
buffer.edit(2..2, "D", cx); buffer.edit([(2..2, "D")], cx);
}); });
// after editing again, the buffer is dirty, and emits another dirty event. // after editing again, the buffer is dirty, and emits another dirty event.
@ -7042,7 +7048,7 @@ mod tests {
// TODO - currently, after restoring the buffer to its // TODO - currently, after restoring the buffer to its
// previously-saved state, the is still considered dirty. // previously-saved state, the is still considered dirty.
buffer.edit(1..3, "", cx); buffer.edit([(1..3, "")], cx);
assert!(buffer.text() == "ac"); assert!(buffer.text() == "ac");
assert!(buffer.is_dirty()); assert!(buffer.is_dirty());
}); });
@ -7086,7 +7092,7 @@ mod tests {
worktree.flush_fs_events(&cx).await; worktree.flush_fs_events(&cx).await;
buffer3.update(cx, |buffer, cx| { buffer3.update(cx, |buffer, cx| {
buffer.edit(0..0, "x", cx); buffer.edit([(0..0, "x")], cx);
}); });
events.borrow_mut().clear(); events.borrow_mut().clear();
fs::remove_file(dir.path().join("file3")).unwrap(); fs::remove_file(dir.path().join("file3")).unwrap();
@ -7180,7 +7186,7 @@ mod tests {
// Modify the buffer // Modify the buffer
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit(0..0, " ", cx); buffer.edit([(0..0, " ")], cx);
assert!(buffer.is_dirty()); assert!(buffer.is_dirty());
assert!(!buffer.has_conflict()); assert!(!buffer.has_conflict());
}); });
@ -7646,7 +7652,7 @@ mod tests {
.unwrap(); .unwrap();
buffer_4.update(cx, |buffer, cx| { buffer_4.update(cx, |buffer, cx| {
let text = "two::TWO"; let text = "two::TWO";
buffer.edit_batched([(20..28, text), (31..43, text)], cx); buffer.edit([(20..28, text), (31..43, text)], cx);
}); });
assert_eq!( assert_eq!(

View file

@ -268,7 +268,7 @@ impl BufferSearchBar {
self.query_editor.update(cx, |query_editor, cx| { self.query_editor.update(cx, |query_editor, cx| {
query_editor.buffer().update(cx, |query_buffer, cx| { query_editor.buffer().update(cx, |query_buffer, cx| {
let len = query_buffer.read(cx).len(); let len = query_buffer.read(cx).len();
query_buffer.edit(0..len, query, cx); query_buffer.edit([(0..len, query)], cx);
}); });
}); });
} }

View file

@ -20,15 +20,15 @@ fn init_logger() {
fn test_edit() { fn test_edit() {
let mut buffer = Buffer::new(0, 0, History::new("abc".into())); let mut buffer = Buffer::new(0, 0, History::new("abc".into()));
assert_eq!(buffer.text(), "abc"); assert_eq!(buffer.text(), "abc");
buffer.edit(3..3, "def"); buffer.edit([(3..3, "def")]);
assert_eq!(buffer.text(), "abcdef"); assert_eq!(buffer.text(), "abcdef");
buffer.edit(0..0, "ghi"); buffer.edit([(0..0, "ghi")]);
assert_eq!(buffer.text(), "ghiabcdef"); assert_eq!(buffer.text(), "ghiabcdef");
buffer.edit(5..5, "jkl"); buffer.edit([(5..5, "jkl")]);
assert_eq!(buffer.text(), "ghiabjklcdef"); assert_eq!(buffer.text(), "ghiabjklcdef");
buffer.edit(6..7, ""); buffer.edit([(6..7, "")]);
assert_eq!(buffer.text(), "ghiabjlcdef"); assert_eq!(buffer.text(), "ghiabjlcdef");
buffer.edit(4..9, "mno"); buffer.edit([(4..9, "mno")]);
assert_eq!(buffer.text(), "ghiamnoef"); assert_eq!(buffer.text(), "ghiamnoef");
} }
@ -151,10 +151,10 @@ fn test_random_edits(mut rng: StdRng) {
#[test] #[test]
fn test_line_len() { fn test_line_len() {
let mut buffer = Buffer::new(0, 0, History::new("".into())); let mut buffer = Buffer::new(0, 0, History::new("".into()));
buffer.edit(0..0, "abcd\nefg\nhij"); buffer.edit([(0..0, "abcd\nefg\nhij")]);
buffer.edit(12..12, "kl\nmno"); buffer.edit([(12..12, "kl\nmno")]);
buffer.edit(18..18, "\npqrs\n"); buffer.edit([(18..18, "\npqrs\n")]);
buffer.edit(18..21, "\nPQ"); buffer.edit([(18..21, "\nPQ")]);
assert_eq!(buffer.line_len(0), 4); assert_eq!(buffer.line_len(0), 4);
assert_eq!(buffer.line_len(1), 3); assert_eq!(buffer.line_len(1), 3);
@ -281,10 +281,10 @@ fn test_text_summary_for_range() {
#[test] #[test]
fn test_chars_at() { fn test_chars_at() {
let mut buffer = Buffer::new(0, 0, History::new("".into())); let mut buffer = Buffer::new(0, 0, History::new("".into()));
buffer.edit(0..0, "abcd\nefgh\nij"); buffer.edit([(0..0, "abcd\nefgh\nij")]);
buffer.edit(12..12, "kl\nmno"); buffer.edit([(12..12, "kl\nmno")]);
buffer.edit(18..18, "\npqrs"); buffer.edit([(18..18, "\npqrs")]);
buffer.edit(18..21, "\nPQ"); buffer.edit([(18..21, "\nPQ")]);
let chars = buffer.chars_at(Point::new(0, 0)); let chars = buffer.chars_at(Point::new(0, 0));
assert_eq!(chars.collect::<String>(), "abcd\nefgh\nijkl\nmno\nPQrs"); assert_eq!(chars.collect::<String>(), "abcd\nefgh\nijkl\nmno\nPQrs");
@ -303,8 +303,8 @@ fn test_chars_at() {
// Regression test: // Regression test:
let mut buffer = Buffer::new(0, 0, History::new("".into())); let mut buffer = Buffer::new(0, 0, History::new("".into()));
buffer.edit(0..0, "[workspace]\nmembers = [\n \"xray_core\",\n \"xray_server\",\n \"xray_cli\",\n \"xray_wasm\",\n]\n"); buffer.edit([(0..0, "[workspace]\nmembers = [\n \"xray_core\",\n \"xray_server\",\n \"xray_cli\",\n \"xray_wasm\",\n]\n")]);
buffer.edit(60..60, "\n"); buffer.edit([(60..60, "\n")]);
let chars = buffer.chars_at(Point::new(6, 0)); let chars = buffer.chars_at(Point::new(6, 0));
assert_eq!(chars.collect::<String>(), " \"xray_wasm\",\n]\n"); assert_eq!(chars.collect::<String>(), " \"xray_wasm\",\n]\n");
@ -313,32 +313,32 @@ fn test_chars_at() {
#[test] #[test]
fn test_anchors() { fn test_anchors() {
let mut buffer = Buffer::new(0, 0, History::new("".into())); let mut buffer = Buffer::new(0, 0, History::new("".into()));
buffer.edit(0..0, "abc"); buffer.edit([(0..0, "abc")]);
let left_anchor = buffer.anchor_before(2); let left_anchor = buffer.anchor_before(2);
let right_anchor = buffer.anchor_after(2); let right_anchor = buffer.anchor_after(2);
buffer.edit(1..1, "def\n"); buffer.edit([(1..1, "def\n")]);
assert_eq!(buffer.text(), "adef\nbc"); assert_eq!(buffer.text(), "adef\nbc");
assert_eq!(left_anchor.to_offset(&buffer), 6); assert_eq!(left_anchor.to_offset(&buffer), 6);
assert_eq!(right_anchor.to_offset(&buffer), 6); assert_eq!(right_anchor.to_offset(&buffer), 6);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 }); assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 }); assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
buffer.edit(2..3, ""); buffer.edit([(2..3, "")]);
assert_eq!(buffer.text(), "adf\nbc"); assert_eq!(buffer.text(), "adf\nbc");
assert_eq!(left_anchor.to_offset(&buffer), 5); assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 5); assert_eq!(right_anchor.to_offset(&buffer), 5);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 }); assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 }); assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
buffer.edit(5..5, "ghi\n"); buffer.edit([(5..5, "ghi\n")]);
assert_eq!(buffer.text(), "adf\nbghi\nc"); assert_eq!(buffer.text(), "adf\nbghi\nc");
assert_eq!(left_anchor.to_offset(&buffer), 5); assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 9); assert_eq!(right_anchor.to_offset(&buffer), 9);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 }); assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 2, column: 0 }); assert_eq!(right_anchor.to_point(&buffer), Point { row: 2, column: 0 });
buffer.edit(7..9, ""); buffer.edit([(7..9, "")]);
assert_eq!(buffer.text(), "adf\nbghc"); assert_eq!(buffer.text(), "adf\nbghc");
assert_eq!(left_anchor.to_offset(&buffer), 5); assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 7); assert_eq!(right_anchor.to_offset(&buffer), 7);
@ -434,7 +434,7 @@ fn test_anchors_at_start_and_end() {
let before_start_anchor = buffer.anchor_before(0); let before_start_anchor = buffer.anchor_before(0);
let after_end_anchor = buffer.anchor_after(0); let after_end_anchor = buffer.anchor_after(0);
buffer.edit(0..0, "abc"); buffer.edit([(0..0, "abc")]);
assert_eq!(buffer.text(), "abc"); assert_eq!(buffer.text(), "abc");
assert_eq!(before_start_anchor.to_offset(&buffer), 0); assert_eq!(before_start_anchor.to_offset(&buffer), 0);
assert_eq!(after_end_anchor.to_offset(&buffer), 3); assert_eq!(after_end_anchor.to_offset(&buffer), 3);
@ -442,8 +442,8 @@ fn test_anchors_at_start_and_end() {
let after_start_anchor = buffer.anchor_after(0); let after_start_anchor = buffer.anchor_after(0);
let before_end_anchor = buffer.anchor_before(3); let before_end_anchor = buffer.anchor_before(3);
buffer.edit(3..3, "def"); buffer.edit([(3..3, "def")]);
buffer.edit(0..0, "ghi"); buffer.edit([(0..0, "ghi")]);
assert_eq!(buffer.text(), "ghiabcdef"); assert_eq!(buffer.text(), "ghiabcdef");
assert_eq!(before_start_anchor.to_offset(&buffer), 0); assert_eq!(before_start_anchor.to_offset(&buffer), 0);
assert_eq!(after_start_anchor.to_offset(&buffer), 3); assert_eq!(after_start_anchor.to_offset(&buffer), 3);
@ -457,9 +457,9 @@ fn test_undo_redo() {
// Set group interval to zero so as to not group edits in the undo stack. // Set group interval to zero so as to not group edits in the undo stack.
buffer.history.group_interval = Duration::from_secs(0); buffer.history.group_interval = Duration::from_secs(0);
buffer.edit(1..1, "abx"); buffer.edit([(1..1, "abx")]);
buffer.edit(3..4, "yzef"); buffer.edit([(3..4, "yzef")]);
buffer.edit(3..5, "cd"); buffer.edit([(3..5, "cd")]);
assert_eq!(buffer.text(), "1abcdef234"); assert_eq!(buffer.text(), "1abcdef234");
let entries = buffer.history.undo_stack.clone(); let entries = buffer.history.undo_stack.clone();
@ -493,19 +493,19 @@ fn test_history() {
let mut buffer = Buffer::new(0, 0, History::new("123456".into())); let mut buffer = Buffer::new(0, 0, History::new("123456".into()));
buffer.start_transaction_at(now); buffer.start_transaction_at(now);
buffer.edit(2..4, "cd"); buffer.edit([(2..4, "cd")]);
buffer.end_transaction_at(now); buffer.end_transaction_at(now);
assert_eq!(buffer.text(), "12cd56"); assert_eq!(buffer.text(), "12cd56");
buffer.start_transaction_at(now); buffer.start_transaction_at(now);
buffer.edit(4..5, "e"); buffer.edit([(4..5, "e")]);
buffer.end_transaction_at(now).unwrap(); buffer.end_transaction_at(now).unwrap();
assert_eq!(buffer.text(), "12cde6"); assert_eq!(buffer.text(), "12cde6");
now += buffer.history.group_interval + Duration::from_millis(1); now += buffer.history.group_interval + Duration::from_millis(1);
buffer.start_transaction_at(now); buffer.start_transaction_at(now);
buffer.edit(0..1, "a"); buffer.edit([(0..1, "a")]);
buffer.edit(1..1, "b"); buffer.edit([(1..1, "b")]);
buffer.end_transaction_at(now).unwrap(); buffer.end_transaction_at(now).unwrap();
assert_eq!(buffer.text(), "ab2cde6"); assert_eq!(buffer.text(), "ab2cde6");
@ -537,19 +537,19 @@ fn test_finalize_last_transaction() {
let mut buffer = Buffer::new(0, 0, History::new("123456".into())); let mut buffer = Buffer::new(0, 0, History::new("123456".into()));
buffer.start_transaction_at(now); buffer.start_transaction_at(now);
buffer.edit(2..4, "cd"); buffer.edit([(2..4, "cd")]);
buffer.end_transaction_at(now); buffer.end_transaction_at(now);
assert_eq!(buffer.text(), "12cd56"); assert_eq!(buffer.text(), "12cd56");
buffer.finalize_last_transaction(); buffer.finalize_last_transaction();
buffer.start_transaction_at(now); buffer.start_transaction_at(now);
buffer.edit(4..5, "e"); buffer.edit([(4..5, "e")]);
buffer.end_transaction_at(now).unwrap(); buffer.end_transaction_at(now).unwrap();
assert_eq!(buffer.text(), "12cde6"); assert_eq!(buffer.text(), "12cde6");
buffer.start_transaction_at(now); buffer.start_transaction_at(now);
buffer.edit(0..1, "a"); buffer.edit([(0..1, "a")]);
buffer.edit(1..1, "b"); buffer.edit([(1..1, "b")]);
buffer.end_transaction_at(now).unwrap(); buffer.end_transaction_at(now).unwrap();
assert_eq!(buffer.text(), "ab2cde6"); assert_eq!(buffer.text(), "ab2cde6");
@ -572,8 +572,8 @@ fn test_edited_ranges_for_transaction() {
let mut buffer = Buffer::new(0, 0, History::new("1234567".into())); let mut buffer = Buffer::new(0, 0, History::new("1234567".into()));
buffer.start_transaction_at(now); buffer.start_transaction_at(now);
buffer.edit(2..4, "cd"); buffer.edit([(2..4, "cd")]);
buffer.edit(6..6, "efg"); buffer.edit([(6..6, "efg")]);
buffer.end_transaction_at(now); buffer.end_transaction_at(now);
assert_eq!(buffer.text(), "12cd56efg7"); assert_eq!(buffer.text(), "12cd56efg7");
@ -585,7 +585,7 @@ fn test_edited_ranges_for_transaction() {
[2..4, 6..9] [2..4, 6..9]
); );
buffer.edit(5..5, "hijk"); buffer.edit([(5..5, "hijk")]);
assert_eq!(buffer.text(), "12cd5hijk6efg7"); assert_eq!(buffer.text(), "12cd5hijk6efg7");
assert_eq!( assert_eq!(
buffer buffer
@ -594,7 +594,7 @@ fn test_edited_ranges_for_transaction() {
[2..4, 10..13] [2..4, 10..13]
); );
buffer.edit(4..4, "l"); buffer.edit([(4..4, "l")]);
assert_eq!(buffer.text(), "12cdl5hijk6efg7"); assert_eq!(buffer.text(), "12cdl5hijk6efg7");
assert_eq!( assert_eq!(
buffer buffer
@ -612,11 +612,11 @@ fn test_concurrent_edits() {
let mut buffer2 = Buffer::new(2, 0, History::new(text.into())); let mut buffer2 = Buffer::new(2, 0, History::new(text.into()));
let mut buffer3 = Buffer::new(3, 0, History::new(text.into())); let mut buffer3 = Buffer::new(3, 0, History::new(text.into()));
let buf1_op = buffer1.edit(1..2, "12"); let buf1_op = buffer1.edit([(1..2, "12")]);
assert_eq!(buffer1.text(), "a12cdef"); assert_eq!(buffer1.text(), "a12cdef");
let buf2_op = buffer2.edit(3..4, "34"); let buf2_op = buffer2.edit([(3..4, "34")]);
assert_eq!(buffer2.text(), "abc34ef"); assert_eq!(buffer2.text(), "abc34ef");
let buf3_op = buffer3.edit(5..6, "56"); let buf3_op = buffer3.edit([(5..6, "56")]);
assert_eq!(buffer3.text(), "abcde56"); assert_eq!(buffer3.text(), "abcde56");
buffer1.apply_op(buf2_op.clone()).unwrap(); buffer1.apply_op(buf2_op.clone()).unwrap();

View file

@ -615,15 +615,7 @@ impl Buffer {
self.history.group_interval self.history.group_interval
} }
pub fn edit<S, T>(&mut self, range: Range<S>, new_text: T) -> Operation pub fn edit<R, I, S, T>(&mut self, edits: R) -> Operation
where
S: ToOffset,
T: Into<Arc<str>>,
{
self.edit_batched([(range, new_text)])
}
pub fn edit_batched<R, I, S, T>(&mut self, edits: R) -> Operation
where where
R: IntoIterator<IntoIter = I>, R: IntoIterator<IntoIter = I>,
I: ExactSizeIterator<Item = (Range<S>, T)>, I: ExactSizeIterator<Item = (Range<S>, T)>,
@ -1457,7 +1449,7 @@ impl Buffer {
} }
log::info!("mutating buffer {} with {:?}", self.replica_id, edits); log::info!("mutating buffer {} with {:?}", self.replica_id, edits);
let op = self.edit_batched(edits.iter().cloned()); let op = self.edit(edits.iter().cloned());
(edits, op) (edits, op)
} }