Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(typst): move parbreak replacer into function and call in nested elements
Grant Lemons6909ad7cbf26
diff --git a/harper-typst/src/lib.rs b/harper-typst/src/lib.rsindex 3b9ca5d14..a67eb9ea4 100644--- a/harper-typst/src/lib.rs+++ b/harper-typst/src/lib.rs@@ -67,6 +67,9 @@ impl Parser for Typst {// Recurse through AST to create tokenslet parse_helper = TypstTranslator::new(&typst_document);+ let mut buf = Vec::new();+ let exprs = typst_tree.exprs().collect_vec();+ let exprs = convert_parbreaks(&mut buf, &exprs);exprs.into_iter().filter_map(|ex| parse_helper.parse_expr(ex, OffsetCursor::new(&typst_document)))@@ -75,6 +78,50 @@ impl Parser for Typst {}}+fn convert_parbreaks<'a>(buf: &'a mut Vec<SyntaxNode>, exprs: &'a [Expr]) -> Vec<Expr<'a>> {+ // Owned collection of nodes forcibly casted to paragraph breaks+ *buf = exprs+ .iter()+ .map(|e| {+ let mut node = SyntaxNode::placeholder(typst_syntax::SyntaxKind::Parbreak);+ node.synthesize(e.span());+ node+ })+ .collect_vec();++ // Converts newlines after certain elements to paragraph breaks+ // This is accomplished here instead of in the translating module because at this point there is+ // still semantic information associated with the elements.+ //+ // Newlines are separate expressions in the parse tree (as the Space variant)+ let should_parbreak = |e1, e2, e3| {+ matches!(e2, Expr::Space(_))+ && (matches!(e1, Expr::Heading(_) | Expr::List(_))+ || matches!(e3, Expr::Heading(_) | Expr::List(_)))+ };++ let mut res: Vec<Expr> = Vec::new();+ let mut last_element: Option<Expr> = None;+ for ((i, expr), (_, next_expr)) in exprs.iter().enumerate().tuple_windows() {+ let mut current_expr = expr.clone();+ if let Some(last_element) = last_element {+ if should_parbreak(last_element, *expr, *next_expr) {+ let pbreak = typst_syntax::ast::Parbreak::from_untyped(&buf[i])+ .expect("Unable to convert expression to Parbreak");+ current_expr = Expr::Parbreak(pbreak);+ }+ }+ res.push(current_expr);+ last_element = Some(expr.clone())+ }+ // Push last element because it will be excluded by tuple_windows() above+ if let Some(last) = exprs.iter().last() {+ res.push(last.clone());+ }++ res+}+#[cfg(test)]mod tests {use itertools::Itertools;diff --git a/harper-typst/src/typst_translator.rs b/harper-typst/src/typst_translator.rsindex 6d335803c..6ba167606 100644--- a/harper-typst/src/typst_translator.rs+++ b/harper-typst/src/typst_translator.rs@@ -149,7 +149,16 @@ impl<'a> TypstTranslator<'a> {// Recurse on each element of an iteratorlet iter_recurse = |exprs: &mut dyn Iterator<Item = Expr>| {- Some(exprs.filter_map(|e| recurse!(e)).flatten().collect_vec())+ let mut buf = Vec::new();+ let exprs = exprs.collect_vec();+ let exprs = super::convert_parbreaks(&mut buf, &exprs);+ Some(+ exprs+ .into_iter()+ .filter_map(|e| recurse!(e))+ .flatten()+ .collect_vec(),+ )};// Parse the parameters of a function or closure