Ensure muiltibuffer anchors are contained within their excerpt ranges

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Max Brunsfeld 2021-12-15 10:52:27 -08:00
parent cec0c5912c
commit 4bea16eb31
3 changed files with 126 additions and 79 deletions

View file

@ -1360,9 +1360,11 @@ impl MultiBufferSnapshot {
}
let buffer_start = excerpt.range.start.to_offset(&excerpt.buffer);
let text_anchor =
excerpt.clip_anchor(excerpt.buffer.anchor_at(buffer_start + overshoot, bias));
Anchor {
excerpt_id: excerpt.id.clone(),
text_anchor: excerpt.buffer.anchor_at(buffer_start + overshoot, bias),
text_anchor,
}
} else if offset == 0 && bias == Bias::Left {
Anchor::min()
@ -1371,6 +1373,22 @@ impl MultiBufferSnapshot {
}
}
pub fn anchor_in_excerpt(&self, excerpt_id: ExcerptId, text_anchor: text::Anchor) -> Anchor {
let mut cursor = self.excerpts.cursor::<Option<&ExcerptId>>();
cursor.seek(&Some(&excerpt_id), Bias::Left, &());
if let Some(excerpt) = cursor.item() {
if excerpt.id == excerpt_id {
let text_anchor = excerpt.clip_anchor(text_anchor);
drop(cursor);
return Anchor {
excerpt_id,
text_anchor,
};
}
}
panic!("excerpt not found");
}
pub fn parse_count(&self) -> usize {
self.parse_count
}
@ -1688,6 +1706,24 @@ impl Excerpt {
footer_height,
}
}
fn clip_anchor(&self, text_anchor: text::Anchor) -> text::Anchor {
if text_anchor
.cmp(&self.range.start, &self.buffer)
.unwrap()
.is_lt()
{
self.range.start.clone()
} else if text_anchor
.cmp(&self.range.end, &self.buffer)
.unwrap()
.is_gt()
{
self.range.end.clone()
} else {
text_anchor
}
}
}
impl fmt::Debug for Excerpt {