diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs index c6adce90..80e7f2b0 100644 --- a/harper-core/src/document.rs +++ b/harper-core/src/document.rs @@ -284,11 +284,17 @@ impl Document { } let child_tok = ©[cursor]; - if let TokenKind::Space(n) = child_tok.kind { - *start_count += n; - start_tok.span.end = child_tok.span.end; - remove_these.push_back(cursor); - cursor += 1; + + // Only condense adjacent spans + if start_tok.span.end == child_tok.span.start { + if let TokenKind::Space(n) = child_tok.kind { + *start_count += n; + start_tok.span.end = child_tok.span.end; + remove_these.push_back(cursor); + cursor += 1; + } else { + break; + } } else { break; }; diff --git a/harper-core/src/parsers/typst.rs b/harper-core/src/parsers/typst.rs index 0a8efce4..a58f89d8 100644 --- a/harper-core/src/parsers/typst.rs +++ b/harper-core/src/parsers/typst.rs @@ -274,10 +274,11 @@ impl<'a> ParseHelper<'a> { } Expr::Str(text) => { let offset = offset.push_to_span(text.span()).char + 1; - let text = text.to_untyped().text().to_string(); + let string = text.to_untyped().text().to_string(); + Some( self.parser - .parse_str(&text[1..text.len() - 1]) + .parse_str(&string[1..string.len() - 1]) .into_iter() .map(|mut t| { t.span.push_by(offset); @@ -562,6 +563,29 @@ mod tests { )) } + #[test] + fn non_adjacent_spaces_not_condensed() { + let source = r#"#authors_slice.join(", ", last: ", and ") "#; + + let token_kinds = Typst.parse_str(source).iter().map(|t| t.kind).collect_vec(); + dbg!(&token_kinds); + + assert!(matches!( + &token_kinds.as_slice(), + &[ + TokenKind::Unlintable, // authors_slice.join + TokenKind::Punctuation(Punctuation::Comma), + TokenKind::Space(1), + TokenKind::Unlintable, // Ident + TokenKind::Punctuation(Punctuation::Comma), + TokenKind::Space(1), + TokenKind::Word(_), // and + TokenKind::Space(1), + TokenKind::Space(1) + ] + )) + } + #[test] fn header_parsing() { let source = r"= Header