Fix crash for jump to outdated span

This commit is contained in:
Laurenz 2023-03-21 12:55:39 +01:00
parent 69136b74dc
commit c913271b29
2 changed files with 12 additions and 12 deletions

View File

@ -20,10 +20,10 @@ pub enum Jump {
}
impl Jump {
fn from_span(world: &dyn World, span: Span) -> Self {
fn from_span(world: &dyn World, span: Span) -> Option<Self> {
let source = world.source(span.source());
let node = source.find(span);
Self::Source(source.id(), node.offset())
let node = source.find(span)?;
Some(Self::Source(source.id(), node.offset()))
}
}
@ -78,7 +78,7 @@ pub fn jump_from_click(
click,
) {
let source = world.source(glyph.span.source());
let node = source.find(glyph.span);
let node = source.find(glyph.span)?;
let pos = if node.kind() == SyntaxKind::Text {
let range = node.range();
let mut offset = range.start + usize::from(glyph.offset);
@ -99,12 +99,12 @@ pub fn jump_from_click(
FrameItem::Shape(shape, span) => {
let Geometry::Rect(size) = shape.geometry else { continue };
if is_in_rect(pos, size, click) {
return Some(Jump::from_span(world, *span));
return Jump::from_span(world, *span);
}
}
FrameItem::Image(_, size, span) if is_in_rect(pos, *size, click) => {
return Some(Jump::from_span(world, *span));
return Jump::from_span(world, *span);
}
_ => {}

View File

@ -152,18 +152,18 @@ impl Source {
/// Find the node with the given span.
///
/// Panics if the span does not point into this source file.
pub fn find(&self, span: Span) -> LinkedNode<'_> {
LinkedNode::new(&self.root)
.find(span)
.expect("span does not point into this source file")
/// Returns `None` if the span does not point into this source file.
pub fn find(&self, span: Span) -> Option<LinkedNode<'_>> {
LinkedNode::new(&self.root).find(span)
}
/// Map a span that points into this source file to a byte range.
///
/// Panics if the span does not point into this source file.
pub fn range(&self, span: Span) -> Range<usize> {
self.find(span).range()
self.find(span)
.expect("span does not point into this source file")
.range()
}
/// Return the index of the UTF-16 code unit at the byte index.