diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs index d13fc5278..9c36b2e8f 100644 --- a/crates/typst-syntax/src/lexer.rs +++ b/crates/typst-syntax/src/lexer.rs @@ -271,9 +271,9 @@ impl Lexer<'_> { // parenthesis) or newline. We have to check the newline before eating // (through '.peek()') to ensure it is not considered part of the // annotation. - let mut current_start = self.s.cursor(); let mut found_closing_paren = false; while !self.s.at(is_newline) { + let current_start = self.s.cursor(); let token = match self.s.eat() { Some(c) if c.is_whitespace() => { self.s.eat_while(is_inline_whitespace); @@ -325,8 +325,6 @@ impl Lexer<'_> { let node = self.emit_token(token, current_start); subtree.push(node); - - current_start = self.s.cursor(); } // Right parenthesis (covered above) diff --git a/crates/typst-syntax/src/node.rs b/crates/typst-syntax/src/node.rs index 9a55ca5c7..7c66d1d6d 100644 --- a/crates/typst-syntax/src/node.rs +++ b/crates/typst-syntax/src/node.rs @@ -825,19 +825,23 @@ impl<'a> LinkedNode<'a> { pub fn prev_attached_annotation(&self) -> Option { let mut cursor = self.prev_sibling_inner()?; let mut newlines = cursor.capped_newlines(); - while newlines < 2 { - if cursor.kind() == SyntaxKind::Annotation { - return Some(cursor); + while cursor.kind() != SyntaxKind::Annotation { + if newlines >= 2 { + // Annotations are attached if they're in the previous line. + // If we counted at least two newlines without finding an + // annotation, no annotations are attached to this node. + // + // Note that this check is only run if the latest node was not + // an annotation, otherwise annotations with multiple lines + // would always be rejected. + return None; } cursor = cursor.prev_sibling_inner()?; newlines += cursor.capped_newlines(); } - // Annotations are attached if they're in the previous line. - // If we counted at least two newlines, no annotations are attached to - // this node. - None + Some(cursor) } /// Get the next non-trivia sibling node.