From a74b602f1832784e3a9170d836d2f913d428a3ba Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 11 Mar 2022 17:03:13 +0100 Subject: [PATCH] Assume the anchor is valid if we can find an excerpt that contains it --- crates/editor/src/multi_buffer.rs | 42 ++++++++++++++---------- crates/editor/src/multi_buffer/anchor.rs | 41 +++++++---------------- 2 files changed, 35 insertions(+), 48 deletions(-) diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index 766acd717e..1b567d6993 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -971,10 +971,13 @@ impl MultiBuffer { ) -> Option<(ModelHandle, language::Anchor)> { let snapshot = self.read(cx); let anchor = snapshot.anchor_before(position); - Some(( - self.buffers.borrow()[&anchor.buffer_id?].buffer.clone(), - anchor.text_anchor, - )) + let buffer = self + .buffers + .borrow() + .get(&anchor.buffer_id?)? + .buffer + .clone(); + Some((buffer, anchor.text_anchor)) } fn on_buffer_event( @@ -1028,14 +1031,19 @@ impl MultiBuffer { let snapshot = self.snapshot(cx); let anchor = snapshot.anchor_before(position); - anchor.buffer_id.map_or(false, |buffer_id| { - let buffer = self.buffers.borrow()[&buffer_id].buffer.clone(); - buffer - .read(cx) - .completion_triggers() - .iter() - .any(|string| string == text) - }) + anchor + .buffer_id + .and_then(|buffer_id| { + let buffer = self.buffers.borrow().get(&buffer_id)?.buffer.clone(); + Some( + buffer + .read(cx) + .completion_triggers() + .iter() + .any(|string| string == text), + ) + }) + .unwrap_or(false) } pub fn language<'a>(&self, cx: &'a AppContext) -> Option<&'a Arc> { @@ -1765,7 +1773,7 @@ impl MultiBufferSnapshot { let mut position = D::from_text_summary(&cursor.start().text); if let Some(excerpt) = cursor.item() { - if excerpt.id == anchor.excerpt_id && Some(excerpt.buffer_id) == anchor.buffer_id { + if excerpt.id == anchor.excerpt_id { let excerpt_buffer_start = excerpt.range.start.summary::(&excerpt.buffer); let excerpt_buffer_end = excerpt.range.end.summary::(&excerpt.buffer); let buffer_position = cmp::min( @@ -1796,10 +1804,9 @@ impl MultiBufferSnapshot { let mut summaries = Vec::new(); while let Some(anchor) = anchors.peek() { let excerpt_id = &anchor.excerpt_id; - let buffer_id = anchor.buffer_id; let excerpt_anchors = iter::from_fn(|| { let anchor = anchors.peek()?; - if anchor.excerpt_id == *excerpt_id && anchor.buffer_id == buffer_id { + if anchor.excerpt_id == *excerpt_id { Some(&anchors.next().unwrap().text_anchor) } else { None @@ -1813,7 +1820,7 @@ impl MultiBufferSnapshot { let position = D::from_text_summary(&cursor.start().text); if let Some(excerpt) = cursor.item() { - if excerpt.id == *excerpt_id && Some(excerpt.buffer_id) == buffer_id { + if excerpt.id == *excerpt_id { let excerpt_buffer_start = excerpt.range.start.summary::(&excerpt.buffer); let excerpt_buffer_end = excerpt.range.end.summary::(&excerpt.buffer); summaries.extend( @@ -2007,8 +2014,7 @@ impl MultiBufferSnapshot { if anchor.excerpt_id == ExcerptId::min() || anchor.excerpt_id == ExcerptId::max() { true } else if let Some(excerpt) = self.excerpt(&anchor.excerpt_id) { - anchor.buffer_id == Some(excerpt.buffer_id) - && excerpt.buffer.can_resolve(&anchor.text_anchor) + excerpt.buffer.can_resolve(&anchor.text_anchor) } else { false } diff --git a/crates/editor/src/multi_buffer/anchor.rs b/crates/editor/src/multi_buffer/anchor.rs index 3de03fb123..33147ce285 100644 --- a/crates/editor/src/multi_buffer/anchor.rs +++ b/crates/editor/src/multi_buffer/anchor.rs @@ -41,22 +41,7 @@ impl Anchor { if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() { Ok(Ordering::Equal) } 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(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(excerpt.buffer_id) { - Ok(Ordering::Less) - } else { - Ok(Ordering::Equal) - } + self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer) } else { Ok(Ordering::Equal) } @@ -68,13 +53,11 @@ impl Anchor { pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor { if self.text_anchor.bias != Bias::Left { 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(&excerpt.buffer), - }; - } + return Self { + buffer_id: self.buffer_id, + excerpt_id: self.excerpt_id.clone(), + text_anchor: self.text_anchor.bias_left(&excerpt.buffer), + }; } } self.clone() @@ -83,13 +66,11 @@ impl Anchor { pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor { if self.text_anchor.bias != Bias::Right { 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(&excerpt.buffer), - }; - } + return Self { + buffer_id: self.buffer_id, + excerpt_id: self.excerpt_id.clone(), + text_anchor: self.text_anchor.bias_right(&excerpt.buffer), + }; } } self.clone()