Consolidate edit and edit_batched functions
This commit is contained in:
parent
74b467aaa8
commit
b4b61b4bbc
12 changed files with 191 additions and 221 deletions
|
@ -880,14 +880,18 @@ impl Buffer {
|
|||
if column > current_column {
|
||||
let offset = Point::new(row, 0).to_offset(&*self);
|
||||
self.edit(
|
||||
offset..offset,
|
||||
" ".repeat((column - current_column) as usize),
|
||||
[(
|
||||
offset..offset,
|
||||
" ".repeat((column - current_column) as usize),
|
||||
)],
|
||||
cx,
|
||||
);
|
||||
} else if column < current_column {
|
||||
self.edit(
|
||||
Point::new(row, 0)..Point::new(row, current_column - column),
|
||||
"",
|
||||
[(
|
||||
Point::new(row, 0)..Point::new(row, current_column - column),
|
||||
"",
|
||||
)],
|
||||
cx,
|
||||
);
|
||||
}
|
||||
|
@ -925,13 +929,15 @@ impl Buffer {
|
|||
match tag {
|
||||
ChangeTag::Equal => offset += len,
|
||||
ChangeTag::Delete => {
|
||||
self.edit(range, "", cx);
|
||||
self.edit([(range, "")], cx);
|
||||
}
|
||||
ChangeTag::Insert => {
|
||||
self.edit(
|
||||
offset..offset,
|
||||
&diff.new_text
|
||||
[range.start - diff.start_offset..range.end - diff.start_offset],
|
||||
[(
|
||||
offset..offset,
|
||||
&diff.new_text[range.start - diff.start_offset
|
||||
..range.end - diff.start_offset],
|
||||
)],
|
||||
cx,
|
||||
);
|
||||
offset += len;
|
||||
|
@ -1054,20 +1060,7 @@ impl Buffer {
|
|||
self.edit_internal([(0..self.len(), text)], None, cx)
|
||||
}
|
||||
|
||||
pub fn edit<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>(
|
||||
pub fn edit<I, S, T>(
|
||||
&mut self,
|
||||
edits_iter: I,
|
||||
cx: &mut ModelContext<Self>,
|
||||
|
@ -1080,21 +1073,7 @@ impl Buffer {
|
|||
self.edit_internal(edits_iter, None, cx)
|
||||
}
|
||||
|
||||
pub fn edit_with_autoindent<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>(
|
||||
pub fn edit_with_autoindent<I, S, T>(
|
||||
&mut self,
|
||||
edits_iter: I,
|
||||
indent_size: u32,
|
||||
|
@ -1165,7 +1144,7 @@ impl Buffer {
|
|||
(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();
|
||||
|
||||
if let Some((before_edit, edited, size)) = autoindent_request {
|
||||
|
@ -1475,7 +1454,7 @@ impl Buffer {
|
|||
edits.push((range, new_text));
|
||||
}
|
||||
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>) {
|
||||
|
|
|
@ -93,7 +93,7 @@ fn test_edit_events(cx: &mut gpui::MutableAppContext) {
|
|||
|
||||
// An edit emits an edited event, followed by a dirtied event,
|
||||
// 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.
|
||||
buffer.start_transaction();
|
||||
|
@ -102,8 +102,8 @@ fn test_edit_events(cx: &mut gpui::MutableAppContext) {
|
|||
// A transaction containing two edits emits one edited event.
|
||||
now += Duration::from_secs(1);
|
||||
buffer.start_transaction_at(now);
|
||||
buffer.edit(5..5, "u", cx);
|
||||
buffer.edit(6..6, "w", cx);
|
||||
buffer.edit([(5..5, "u")], cx);
|
||||
buffer.edit([(6..6, "w")], cx);
|
||||
buffer.end_transaction_at(now, cx);
|
||||
|
||||
// Undoing a transaction emits one edited event.
|
||||
|
@ -178,11 +178,11 @@ async fn test_reparse(cx: &mut gpui::TestAppContext) {
|
|||
buf.start_transaction();
|
||||
|
||||
let offset = buf.text().find(")").unwrap();
|
||||
buf.edit(offset..offset, "b: C", cx);
|
||||
buf.edit([(offset..offset, "b: C")], cx);
|
||||
assert!(!buf.is_parsing());
|
||||
|
||||
let offset = buf.text().find("}").unwrap();
|
||||
buf.edit(offset..offset, " d; ", cx);
|
||||
buf.edit([(offset..offset, " d; ")], cx);
|
||||
assert!(!buf.is_parsing());
|
||||
|
||||
buf.end_transaction(cx);
|
||||
|
@ -207,19 +207,19 @@ async fn test_reparse(cx: &mut gpui::TestAppContext) {
|
|||
// * add a turbofish to the method call
|
||||
buffer.update(cx, |buf, cx| {
|
||||
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!(buf.is_parsing());
|
||||
});
|
||||
buffer.update(cx, |buf, cx| {
|
||||
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!(buf.is_parsing());
|
||||
});
|
||||
buffer.update(cx, |buf, cx| {
|
||||
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!(buf.is_parsing());
|
||||
});
|
||||
|
@ -576,13 +576,13 @@ fn test_edit_with_autoindent(cx: &mut MutableAppContext) {
|
|||
let text = "fn a() {}";
|
||||
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}");
|
||||
|
||||
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}");
|
||||
|
||||
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}");
|
||||
|
||||
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,
|
||||
// their indentation is not adjusted.
|
||||
buffer.edit_with_autoindent_batched(
|
||||
buffer.edit_with_autoindent(
|
||||
[
|
||||
(empty(Point::new(1, 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
|
||||
// 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(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);
|
||||
|
||||
buffer.edit_with_autoindent(5..5, "\nb", 4, cx);
|
||||
buffer.edit_with_autoindent([(5..5, "\nb")], 4, cx);
|
||||
assert_eq!(
|
||||
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)
|
||||
// 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!(
|
||||
buffer.text(),
|
||||
"
|
||||
|
@ -692,7 +692,7 @@ fn test_autoindent_with_edit_at_end_of_buffer(cx: &mut MutableAppContext) {
|
|||
cx.add_model(|cx| {
|
||||
let text = "a\nb";
|
||||
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");
|
||||
buffer
|
||||
});
|
||||
|
@ -704,18 +704,18 @@ fn test_serialization(cx: &mut gpui::MutableAppContext) {
|
|||
|
||||
let buffer1 = cx.add_model(|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);
|
||||
buffer.start_transaction_at(now);
|
||||
buffer.edit(4..4, "E", cx);
|
||||
buffer.edit([(4..4, "E")], cx);
|
||||
buffer.end_transaction_at(now, cx);
|
||||
assert_eq!(buffer.text(), "abcDE");
|
||||
|
||||
buffer.undo(cx);
|
||||
assert_eq!(buffer.text(), "abcD");
|
||||
|
||||
buffer.edit(4..4, "F", cx);
|
||||
buffer.edit([(4..4, "F")], cx);
|
||||
assert_eq!(buffer.text(), "abcDF");
|
||||
buffer
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue