Assume the anchor is valid if we can find an excerpt that contains it

This commit is contained in:
Antonio Scandurra 2022-03-11 17:03:13 +01:00
parent 5407f25c94
commit a74b602f18
2 changed files with 35 additions and 48 deletions

View file

@ -971,10 +971,13 @@ impl MultiBuffer {
) -> Option<(ModelHandle<Buffer>, language::Anchor)> { ) -> Option<(ModelHandle<Buffer>, language::Anchor)> {
let snapshot = self.read(cx); let snapshot = self.read(cx);
let anchor = snapshot.anchor_before(position); let anchor = snapshot.anchor_before(position);
Some(( let buffer = self
self.buffers.borrow()[&anchor.buffer_id?].buffer.clone(), .buffers
anchor.text_anchor, .borrow()
)) .get(&anchor.buffer_id?)?
.buffer
.clone();
Some((buffer, anchor.text_anchor))
} }
fn on_buffer_event( fn on_buffer_event(
@ -1028,14 +1031,19 @@ impl MultiBuffer {
let snapshot = self.snapshot(cx); let snapshot = self.snapshot(cx);
let anchor = snapshot.anchor_before(position); let anchor = snapshot.anchor_before(position);
anchor.buffer_id.map_or(false, |buffer_id| { anchor
let buffer = self.buffers.borrow()[&buffer_id].buffer.clone(); .buffer_id
buffer .and_then(|buffer_id| {
.read(cx) let buffer = self.buffers.borrow().get(&buffer_id)?.buffer.clone();
.completion_triggers() Some(
.iter() buffer
.any(|string| string == text) .read(cx)
}) .completion_triggers()
.iter()
.any(|string| string == text),
)
})
.unwrap_or(false)
} }
pub fn language<'a>(&self, cx: &'a AppContext) -> Option<&'a Arc<Language>> { pub fn language<'a>(&self, cx: &'a AppContext) -> Option<&'a Arc<Language>> {
@ -1765,7 +1773,7 @@ impl MultiBufferSnapshot {
let mut position = D::from_text_summary(&cursor.start().text); let mut position = D::from_text_summary(&cursor.start().text);
if let Some(excerpt) = cursor.item() { 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::<D>(&excerpt.buffer); let excerpt_buffer_start = excerpt.range.start.summary::<D>(&excerpt.buffer);
let excerpt_buffer_end = excerpt.range.end.summary::<D>(&excerpt.buffer); let excerpt_buffer_end = excerpt.range.end.summary::<D>(&excerpt.buffer);
let buffer_position = cmp::min( let buffer_position = cmp::min(
@ -1796,10 +1804,9 @@ impl MultiBufferSnapshot {
let mut summaries = Vec::new(); let mut summaries = Vec::new();
while let Some(anchor) = anchors.peek() { while let Some(anchor) = anchors.peek() {
let excerpt_id = &anchor.excerpt_id; let excerpt_id = &anchor.excerpt_id;
let buffer_id = anchor.buffer_id;
let excerpt_anchors = iter::from_fn(|| { let excerpt_anchors = iter::from_fn(|| {
let anchor = anchors.peek()?; 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) Some(&anchors.next().unwrap().text_anchor)
} else { } else {
None None
@ -1813,7 +1820,7 @@ impl MultiBufferSnapshot {
let position = D::from_text_summary(&cursor.start().text); let position = D::from_text_summary(&cursor.start().text);
if let Some(excerpt) = cursor.item() { 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::<D>(&excerpt.buffer); let excerpt_buffer_start = excerpt.range.start.summary::<D>(&excerpt.buffer);
let excerpt_buffer_end = excerpt.range.end.summary::<D>(&excerpt.buffer); let excerpt_buffer_end = excerpt.range.end.summary::<D>(&excerpt.buffer);
summaries.extend( summaries.extend(
@ -2007,8 +2014,7 @@ impl MultiBufferSnapshot {
if anchor.excerpt_id == ExcerptId::min() || anchor.excerpt_id == ExcerptId::max() { if anchor.excerpt_id == ExcerptId::min() || anchor.excerpt_id == ExcerptId::max() {
true true
} else if let Some(excerpt) = self.excerpt(&anchor.excerpt_id) { } 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 { } else {
false false
} }

View file

@ -41,22 +41,7 @@ impl Anchor {
if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() { if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() {
Ok(Ordering::Equal) Ok(Ordering::Equal)
} else if let Some(excerpt) = snapshot.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 self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer)
// 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)
}
} else { } else {
Ok(Ordering::Equal) Ok(Ordering::Equal)
} }
@ -68,13 +53,11 @@ impl Anchor {
pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor { pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
if self.text_anchor.bias != Bias::Left { if self.text_anchor.bias != Bias::Left {
if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) { if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) {
if self.buffer_id == Some(excerpt.buffer_id) { return Self {
return Self { buffer_id: self.buffer_id,
buffer_id: self.buffer_id, excerpt_id: self.excerpt_id.clone(),
excerpt_id: self.excerpt_id.clone(), text_anchor: self.text_anchor.bias_left(&excerpt.buffer),
text_anchor: self.text_anchor.bias_left(&excerpt.buffer), };
};
}
} }
} }
self.clone() self.clone()
@ -83,13 +66,11 @@ impl Anchor {
pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor { pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
if self.text_anchor.bias != Bias::Right { if self.text_anchor.bias != Bias::Right {
if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) { if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) {
if self.buffer_id == Some(excerpt.buffer_id) { return Self {
return Self { buffer_id: self.buffer_id,
buffer_id: self.buffer_id, excerpt_id: self.excerpt_id.clone(),
excerpt_id: self.excerpt_id.clone(), text_anchor: self.text_anchor.bias_right(&excerpt.buffer),
text_anchor: self.text_anchor.bias_right(&excerpt.buffer), };
};
}
} }
} }
self.clone() self.clone()