Account for all excerpts ever inserted when determining new excerpt ID

This commit is contained in:
Antonio Scandurra 2022-03-12 17:50:09 +01:00
parent cd4a9f3178
commit dd1711d53f

View file

@ -751,29 +751,31 @@ impl MultiBuffer {
let mut cursor = snapshot.excerpts.cursor::<Option<&ExcerptId>>(); let mut cursor = snapshot.excerpts.cursor::<Option<&ExcerptId>>();
let mut new_excerpts = cursor.slice(&Some(prev_excerpt_id), Bias::Right, &()); let mut new_excerpts = cursor.slice(&Some(prev_excerpt_id), Bias::Right, &());
let mut prev_id = ExcerptId::min();
let edit_start = new_excerpts.summary().text.bytes; let edit_start = new_excerpts.summary().text.bytes;
new_excerpts.update_last( new_excerpts.update_last(
|excerpt| { |excerpt| {
excerpt.has_trailing_newline = true; excerpt.has_trailing_newline = true;
prev_id = excerpt.id.clone();
}, },
&(), &(),
); );
let mut next_id = ExcerptId::max(); let mut used_cursor = self.used_excerpt_ids.cursor::<Locator>();
{ used_cursor.seek(prev_excerpt_id, Bias::Right, &());
let mut used_cursor = self.used_excerpt_ids.cursor::<Locator>(); let mut prev_id = if let Some(excerpt_id) = used_cursor.prev_item() {
used_cursor.seek(&prev_id, Bias::Right, &()); excerpt_id.clone()
if let Some(used_id) = used_cursor.item() { } else {
next_id = used_id.clone(); ExcerptId::min()
} };
} let next_id = if let Some(excerpt_id) = used_cursor.item() {
excerpt_id.clone()
} else {
ExcerptId::max()
};
drop(used_cursor);
let mut ids = Vec::new(); let mut ids = Vec::new();
while let Some(range) = ranges.next() { while let Some(range) = ranges.next() {
let id = ExcerptId::between(&prev_id, &next_id); let id = ExcerptId::between(&prev_id, &next_id);
self.used_excerpt_ids.insert_or_replace(id.clone(), &());
if let Err(ix) = buffer_state.excerpts.binary_search(&id) { if let Err(ix) = buffer_state.excerpts.binary_search(&id) {
buffer_state.excerpts.insert(ix, id.clone()); buffer_state.excerpts.insert(ix, id.clone());
} }
@ -790,6 +792,10 @@ impl MultiBuffer {
prev_id = id.clone(); prev_id = id.clone();
ids.push(id); ids.push(id);
} }
self.used_excerpt_ids.edit(
ids.iter().cloned().map(sum_tree::Edit::Insert).collect(),
&(),
);
let edit_end = new_excerpts.summary().text.bytes; let edit_end = new_excerpts.summary().text.bytes;
@ -3223,8 +3229,8 @@ mod tests {
let snapshot_2 = multibuffer.read(cx).snapshot(cx); let snapshot_2 = multibuffer.read(cx).snapshot(cx);
assert_eq!(snapshot_2.text(), "ABCD\nGHIJ\nMNOP"); assert_eq!(snapshot_2.text(), "ABCD\nGHIJ\nMNOP");
// The old excerpt id has been reused. // The old excerpt id doesn't get reused.
assert_eq!(excerpt_id_2, excerpt_id_1); assert_ne!(excerpt_id_2, excerpt_id_1);
// Resolve some anchors from the previous snapshot in the new snapshot. // Resolve some anchors from the previous snapshot in the new snapshot.
// Although there is still an excerpt with the same id, it is for // Although there is still an excerpt with the same id, it is for
@ -3276,7 +3282,7 @@ mod tests {
]; ];
assert_eq!( assert_eq!(
snapshot_3.summaries_for_anchors::<usize, _>(&anchors), snapshot_3.summaries_for_anchors::<usize, _>(&anchors),
&[0, 2, 9, 13] &[0, 2, 5, 13]
); );
let new_anchors = snapshot_3.refresh_anchors(&anchors); let new_anchors = snapshot_3.refresh_anchors(&anchors);