Respect excerpt's range when comparing two anchors both belonging to it

This commit is contained in:
Antonio Scandurra 2022-03-11 12:41:05 +01:00
parent a067150b5a
commit c67cfd7fe1
3 changed files with 28 additions and 26 deletions

View file

@ -40,17 +40,19 @@ impl Anchor {
if excerpt_id_cmp.is_eq() {
if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() {
Ok(Ordering::Equal)
} else if let Some((buffer_id, buffer_snapshot)) =
snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id)
{
} else if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) {
// Even though the anchor refers to a valid excerpt the underlying buffer might have
// changed. In that case, treat the anchor as if it were at the start of that
// excerpt.
if self.buffer_id == Some(buffer_id) && other.buffer_id == Some(buffer_id) {
self.text_anchor.cmp(&other.text_anchor, buffer_snapshot)
} else if self.buffer_id == Some(buffer_id) {
if self.buffer_id == Some(excerpt.buffer_id)
&& other.buffer_id == Some(excerpt.buffer_id)
{
let self_anchor = excerpt.clip_anchor(self.text_anchor.clone());
let other_anchor = excerpt.clip_anchor(other.text_anchor.clone());
self_anchor.cmp(&other_anchor, &excerpt.buffer)
} else if self.buffer_id == Some(excerpt.buffer_id) {
Ok(Ordering::Greater)
} else if other.buffer_id == Some(buffer_id) {
} else if other.buffer_id == Some(excerpt.buffer_id) {
Ok(Ordering::Less)
} else {
Ok(Ordering::Equal)
@ -65,14 +67,12 @@ impl Anchor {
pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
if self.text_anchor.bias != Bias::Left {
if let Some((buffer_id, buffer_snapshot)) =
snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id)
{
if self.buffer_id == Some(buffer_id) {
if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) {
if self.buffer_id == Some(excerpt.buffer_id) {
return Self {
buffer_id: self.buffer_id,
excerpt_id: self.excerpt_id.clone(),
text_anchor: self.text_anchor.bias_left(buffer_snapshot),
text_anchor: self.text_anchor.bias_left(&excerpt.buffer),
};
}
}
@ -82,14 +82,12 @@ impl Anchor {
pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
if self.text_anchor.bias != Bias::Right {
if let Some((buffer_id, buffer_snapshot)) =
snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id)
{
if self.buffer_id == Some(buffer_id) {
if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) {
if self.buffer_id == Some(excerpt.buffer_id) {
return Self {
buffer_id: self.buffer_id,
excerpt_id: self.excerpt_id.clone(),
text_anchor: self.text_anchor.bias_right(buffer_snapshot),
text_anchor: self.text_anchor.bias_right(&excerpt.buffer),
};
}
}