Building, but failing test WIP

This commit is contained in:
Keith Simmons 2022-04-27 17:25:38 -07:00
parent 065734e1de
commit 04fc1d5982
14 changed files with 492 additions and 419 deletions

View file

@ -880,13 +880,13 @@ impl Buffer {
if column > current_column {
let offset = Point::new(row, 0).to_offset(&*self);
self.edit(
[offset..offset],
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,11 +925,11 @@ impl Buffer {
match tag {
ChangeTag::Equal => offset += len,
ChangeTag::Delete => {
self.edit([range], "", cx);
self.edit(range, "", cx);
}
ChangeTag::Insert => {
self.edit(
[offset..offset],
offset..offset,
&diff.new_text
[range.start - diff.start_offset..range.end - diff.start_offset],
cx,
@ -1049,71 +1049,95 @@ impl Buffer {
pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Local>
where
T: Into<String>,
T: Into<Arc<str>>,
{
self.edit_internal([0..self.len()], text, None, cx)
self.edit_internal([(0..self.len(), text)], None, cx)
}
pub fn edit<I, S, T>(
pub fn edit<S, T>(
&mut self,
ranges_iter: I,
range: Range<S>,
new_text: T,
cx: &mut ModelContext<Self>,
) -> Option<clock::Local>
where
I: IntoIterator<Item = Range<S>>,
S: ToOffset,
T: Into<String>,
T: Into<Arc<str>>,
{
self.edit_internal(ranges_iter, new_text, None, cx)
self.edit_batched([(range, new_text)], cx)
}
pub fn edit_with_autoindent<I, S, T>(
pub fn edit_batched<I, S, T>(
&mut self,
ranges_iter: I,
edits_iter: I,
cx: &mut ModelContext<Self>,
) -> Option<clock::Local>
where
I: IntoIterator<Item = (Range<S>, T)>,
S: ToOffset,
T: Into<Arc<str>>,
{
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
I: IntoIterator<Item = Range<S>>,
S: ToOffset,
T: Into<String>,
T: Into<Arc<str>>,
{
self.edit_internal(ranges_iter, new_text, Some(indent_size), cx)
self.edit_with_autoindent_batched([(range, new_text)], indent_size, cx)
}
pub fn edit_with_autoindent_batched<I, S, T>(
&mut self,
edits_iter: I,
indent_size: u32,
cx: &mut ModelContext<Self>,
) -> Option<clock::Local>
where
I: IntoIterator<Item = (Range<S>, T)>,
S: ToOffset,
T: Into<Arc<str>>,
{
self.edit_internal(edits_iter, Some(indent_size), cx)
}
pub fn edit_internal<I, S, T>(
&mut self,
ranges_iter: I,
new_text: T,
edits_iter: I,
autoindent_size: Option<u32>,
cx: &mut ModelContext<Self>,
) -> Option<clock::Local>
where
I: IntoIterator<Item = Range<S>>,
I: IntoIterator<Item = (Range<S>, T)>,
S: ToOffset,
T: Into<String>,
T: Into<Arc<str>>,
{
let new_text = new_text.into();
// Skip invalid ranges and coalesce contiguous ones.
let mut ranges: Vec<Range<usize>> = Vec::new();
for range in ranges_iter {
// Skip invalid edits and coalesce contiguous ones.
let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
for (range, new_text) in edits_iter {
let range = range.start.to_offset(self)..range.end.to_offset(self);
let new_text = new_text.into();
if !new_text.is_empty() || !range.is_empty() {
if let Some(prev_range) = ranges.last_mut() {
if let Some((prev_range, prev_text)) = edits.last_mut() {
if prev_range.end >= range.start {
prev_range.end = cmp::max(prev_range.end, range.end);
*prev_text = format!("{prev_text}{new_text}").into();
} else {
ranges.push(range);
edits.push((range, new_text));
}
} else {
ranges.push(range);
edits.push((range, new_text));
}
}
}
if ranges.is_empty() {
if edits.is_empty() {
return None;
}
@ -1125,9 +1149,9 @@ impl Buffer {
.and_then(|_| autoindent_size)
.map(|autoindent_size| {
let before_edit = self.snapshot();
let edited = ranges
let edited = edits
.iter()
.filter_map(|range| {
.filter_map(|(range, new_text)| {
let start = range.start.to_point(self);
if new_text.starts_with('\n')
&& start.column == self.line_len(start.row)
@ -1141,30 +1165,30 @@ impl Buffer {
(before_edit, edited, autoindent_size)
});
let first_newline_ix = new_text.find('\n');
let new_text_len = new_text.len();
let edit = self.text.edit(ranges.iter().cloned(), new_text);
let edit_id = edit.local_timestamp();
let edit_operation = self.text.edit_batched(edits.iter().cloned());
let edit_id = edit_operation.local_timestamp();
if let Some((before_edit, edited, size)) = autoindent_request {
let mut inserted = None;
if let Some(first_newline_ix) = first_newline_ix {
let mut delta = 0isize;
inserted = Some(
ranges
.iter()
.map(|range| {
let start =
(delta + range.start as isize) as usize + first_newline_ix + 1;
let end = (delta + range.start as isize) as usize + new_text_len;
delta +=
(range.end as isize - range.start as isize) + new_text_len as isize;
self.anchor_before(start)..self.anchor_after(end)
})
.collect(),
);
}
let mut delta = 0isize;
let inserted_ranges = edits
.into_iter()
.filter_map(|(range, new_text)| {
let first_newline_ix = new_text.find('\n')?;
let new_text_len = new_text.len();
let start = (delta + range.start as isize) as usize + first_newline_ix + 1;
let end = (delta + range.start as isize) as usize + new_text_len;
delta += new_text_len as isize - (range.end as isize - range.start as isize);
dbg!(&range, new_text, start, end, delta);
Some(self.anchor_before(start)..self.anchor_after(end))
})
.collect::<Vec<Range<Anchor>>>();
let inserted = if inserted_ranges.is_empty() {
None
} else {
Some(inserted_ranges)
};
self.autoindent_requests.push(Arc::new(AutoindentRequest {
before_edit,
@ -1175,7 +1199,7 @@ impl Buffer {
}
self.end_transaction(cx);
self.send_operation(Operation::Buffer(edit), cx);
self.send_operation(Operation::Buffer(edit_operation), cx);
Some(edit_id)
}
@ -1433,25 +1457,26 @@ impl Buffer {
) where
T: rand::Rng,
{
let mut old_ranges: Vec<Range<usize>> = Vec::new();
let mut edits: Vec<(Range<usize>, String)> = Vec::new();
let mut last_end = None;
for _ in 0..old_range_count {
let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
if last_end > self.len() {
if last_end.map_or(false, |last_end| last_end >= self.len()) {
break;
}
old_ranges.push(self.text.random_byte_range(last_end, rng));
let new_start = last_end.map_or(0, |last_end| last_end + 1);
let range = self.random_byte_range(new_start, rng);
last_end = Some(range.end);
let new_text_len = rng.gen_range(0..10);
let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
.take(new_text_len)
.collect();
edits.push((range, new_text));
}
let new_text_len = rng.gen_range(0..10);
let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
.take(new_text_len)
.collect();
log::info!(
"mutating buffer {} at {:?}: {:?}",
self.replica_id(),
old_ranges,
new_text
);
self.edit(old_ranges.iter().cloned(), new_text.as_str(), cx);
log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
self.edit_batched(edits, cx);
}
pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {

View file

@ -78,7 +78,14 @@ pub fn serialize_edit_operation(operation: &EditOperation) -> proto::operation::
lamport_timestamp: operation.timestamp.lamport,
version: serialize_version(&operation.version),
ranges: operation.ranges.iter().map(serialize_range).collect(),
new_text: operation.new_text.clone(),
new_text: operation
.new_text
.iter()
.map(|text| {
text.as_ref()
.map_or_else(String::new, |text| text.to_string())
})
.collect(),
}
}
@ -243,7 +250,7 @@ pub fn deserialize_edit_operation(edit: proto::operation::Edit) -> EditOperation
},
version: deserialize_version(edit.version),
ranges: edit.ranges.into_iter().map(deserialize_range).collect(),
new_text: edit.new_text,
new_text: edit.new_text.into_iter().map(|t| Some(t.into())).collect(),
}
}

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,
// since the buffer was previously in a clean state.
buffer.edit(Some(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(Some(5..5), "u", cx);
buffer.edit(Some(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(vec![offset..offset], "b: C", cx);
buf.edit(offset..offset, "b: C", cx);
assert!(!buf.is_parsing());
let offset = buf.text().find("}").unwrap();
buf.edit(vec![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(vec![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(vec![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(vec![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,9 +604,11 @@ 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(
[empty(Point::new(1, 1)), empty(Point::new(2, 1))],
"()",
buffer.edit_with_autoindent_batched(
[
(empty(Point::new(1, 1)), "()"),
(empty(Point::new(2, 1)), "()"),
],
4,
cx,
);
@ -623,9 +625,11 @@ 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(
[empty(Point::new(1, 1)), empty(Point::new(2, 1))],
"\n.f\n.g",
buffer.edit_with_autoindent_batched(
[
(empty(Point::new(1, 1)), "\n.f\n.g"),
(empty(Point::new(2, 1)), "\n.f\n.g"),
],
4,
cx,
);
@ -657,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(),
"
@ -669,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(),
"
@ -683,24 +687,35 @@ fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppConte
});
}
#[gpui::test]
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);
assert_eq!(buffer.text(), "\n\n\n");
buffer
});
}
#[gpui::test]
fn test_serialization(cx: &mut gpui::MutableAppContext) {
let mut now = Instant::now();
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
});