Make BlockMap randomized test pass in low-complexity cases

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-11-15 16:47:30 +01:00
parent 296944e34d
commit cebab56c94
2 changed files with 19 additions and 29 deletions

View file

@ -94,16 +94,6 @@ impl Rope {
self.append(suffix); self.append(suffix);
} }
pub fn starts_with(&self, text: &str) -> bool {
self.chunks().flat_map(|c| c.bytes()).eq(text.bytes())
}
pub fn ends_with(&self, text: &str) -> bool {
self.reversed_chunks_in_range(0..self.len())
.flat_map(|c| c.bytes().rev())
.eq(text.bytes().rev())
}
fn check_invariants(&self) { fn check_invariants(&self) {
#[cfg(test)] #[cfg(test)]
{ {

View file

@ -189,20 +189,24 @@ impl BlockMap {
dbg!("expanded edit", &edit); dbg!("expanded edit", &edit);
let start_anchor = buffer.anchor_before(Point::new(edit.new.start, 0)); let start_anchor = buffer.anchor_before(Point::new(edit.new.start, 0));
let end_anchor = buffer.anchor_before(Point::new(edit.new.end, 0));
let start_block_ix = match self.blocks[last_block_ix..] let start_block_ix = match self.blocks[last_block_ix..]
.binary_search_by(|probe| probe.position.cmp(&start_anchor, buffer).unwrap()) .binary_search_by(|probe| probe.position.cmp(&start_anchor, buffer).unwrap())
{ {
Ok(ix) | Err(ix) => last_block_ix + ix, Ok(ix) | Err(ix) => last_block_ix + ix,
}; };
let end_block_ix = match self.blocks[start_block_ix..].binary_search_by(|probe| { let end_block_ix = if edit.new.end > wrap_snapshot.max_point().row() {
probe self.blocks.len()
.position } else {
.cmp(&end_anchor, buffer) let end_anchor = buffer.anchor_before(Point::new(edit.new.end, 0));
.unwrap() match self.blocks[start_block_ix..].binary_search_by(|probe| {
.then(Ordering::Greater) probe
}) { .position
Ok(ix) | Err(ix) => start_block_ix + ix, .cmp(&end_anchor, buffer)
.unwrap()
.then(Ordering::Greater)
}) {
Ok(ix) | Err(ix) => start_block_ix + ix,
}
}; };
last_block_ix = end_block_ix; last_block_ix = end_block_ix;
@ -240,10 +244,9 @@ impl BlockMap {
} }
let new_transforms_end = new_transforms.summary().input; let new_transforms_end = new_transforms.summary().input;
if new_transforms_end.row < edit.new.end - 1 { let edit_new_end_point =
// TODO: Should we just report the wrap edits in 2d so we can skip this max point check? cmp::min(Point::new(edit.new.end, 0), wrap_snapshot.max_point().0);
let edit_new_end_point = if new_transforms_end < edit_new_end_point {
cmp::min(Point::new(edit.new.end, 0), wrap_snapshot.max_point().0);
new_transforms.push( new_transforms.push(
Transform::isomorphic(edit_new_end_point - new_transforms_end), Transform::isomorphic(edit_new_end_point - new_transforms_end),
&(), &(),
@ -306,13 +309,9 @@ impl<'a> BlockMapWriter<'a> {
}; };
let mut text = block.text.into(); let mut text = block.text.into();
if block.disposition.is_above() { if block.disposition.is_above() {
if !text.ends_with("\n") { text.push("\n");
text.push("\n");
}
} else { } else {
if !text.starts_with("\n") { text.push_front("\n");
text.push_front("\n");
}
} }
self.0.blocks.insert( self.0.blocks.insert(
@ -745,6 +744,7 @@ mod tests {
let buffer = cx.add_model(|cx| { let buffer = cx.add_model(|cx| {
let len = rng.gen_range(0..10); let len = rng.gen_range(0..10);
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>(); let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
log::info!("initial buffer text: {:?}", text);
Buffer::new(0, text, cx) Buffer::new(0, text, cx)
}); });
let (fold_map, folds_snapshot) = FoldMap::new(buffer.clone(), cx); let (fold_map, folds_snapshot) = FoldMap::new(buffer.clone(), cx);