Make anchor_in_excerpt Optional (#8975)

We were seeing panics due to callers assuming they had valid
excerpt_ids, but that cannot easily be guaranteed across await points as
anyone may remove an excerpt.

Release Notes:

- Fixed a panic when hovering in a multibuffer
This commit is contained in:
Conrad Irwin 2024-03-06 18:55:36 -07:00 committed by GitHub
parent ae5ec9224c
commit 0b87be71e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 101 additions and 73 deletions

View file

@ -13,7 +13,7 @@ use project::{
};
use std::ops::Range;
use theme::ActiveTheme as _;
use util::TryFutureExt;
use util::{maybe, TryFutureExt};
#[derive(Debug)]
pub struct HoveredLinkState {
@ -424,12 +424,13 @@ pub fn show_link_definition(
TriggerPoint::Text(_) => {
if let Some((url_range, url)) = find_url(&buffer, buffer_position, cx.clone()) {
this.update(&mut cx, |_, _| {
let start = snapshot.anchor_in_excerpt(excerpt_id, url_range.start);
let end = snapshot.anchor_in_excerpt(excerpt_id, url_range.end);
(
Some(RangeInEditor::Text(start..end)),
vec![HoverLink::Url(url)],
)
let range = maybe!({
let start =
snapshot.anchor_in_excerpt(excerpt_id, url_range.start)?;
let end = snapshot.anchor_in_excerpt(excerpt_id, url_range.end)?;
Some(RangeInEditor::Text(start..end))
});
(range, vec![HoverLink::Url(url)])
})
.ok()
} else if let Some(project) = project {
@ -449,12 +450,14 @@ pub fn show_link_definition(
.map(|definition_result| {
(
definition_result.iter().find_map(|link| {
link.origin.as_ref().map(|origin| {
let start = snapshot
.anchor_in_excerpt(excerpt_id, origin.range.start);
link.origin.as_ref().and_then(|origin| {
let start = snapshot.anchor_in_excerpt(
excerpt_id,
origin.range.start,
)?;
let end = snapshot
.anchor_in_excerpt(excerpt_id, origin.range.end);
RangeInEditor::Text(start..end)
.anchor_in_excerpt(excerpt_id, origin.range.end)?;
Some(RangeInEditor::Text(start..end))
})
}),
definition_result.into_iter().map(HoverLink::Text).collect(),