Fix crash with empty raw block (#3619)

This commit is contained in:
Laurenz 2024-03-11 12:12:41 +01:00 committed by GitHub
parent 54f6ee0e53
commit 671b67d43f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 3 deletions

View File

@ -350,14 +350,16 @@ impl Packed<RawElem> {
LinkedNode::new(&root), LinkedNode::new(&root),
synt::Highlighter::new(theme), synt::Highlighter::new(theme),
&mut |i, _, range, style| { &mut |i, _, range, style| {
// Find start of line. // Find span and start of line.
// Note: Dedent is already applied to the text // Note: Dedent is already applied to the text
let span = lines.get(i).map_or_else(Span::detached, |l| l.1);
let span_offset = text[..range.start] let span_offset = text[..range.start]
.rfind('\n') .rfind('\n')
.map_or(0, |i| range.start - (i + 1)); .map_or(0, |i| range.start - (i + 1));
styled(&text[range], foreground, style, lines[i].1, span_offset) styled(&text[range], foreground, style, span, span_offset)
}, },
&mut |i, range, line| { &mut |i, range, line| {
let span = lines.get(i).map_or_else(Span::detached, |l| l.1);
seq.push( seq.push(
Packed::new(RawLine::new( Packed::new(RawLine::new(
(i + 1) as i64, (i + 1) as i64,
@ -365,7 +367,7 @@ impl Packed<RawElem> {
EcoString::from(&text[range]), EcoString::from(&text[range]),
Content::sequence(line.drain(..)), Content::sequence(line.drain(..)),
)) ))
.spanned(lines[i].1), .spanned(span),
); );
}, },
) )

View File

@ -0,0 +1,7 @@
// Test that empty raw block with `typ` language doesn't cause a crash.
// https://github.com/typst/typst/issues/3601
// Ref: false
---
```typ
```