Skip to content

Commit

Permalink
fix(typst): remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
grantlemons committed Jan 21, 2025
1 parent 543a047 commit 0a6aa45
Showing 1 changed file with 0 additions and 42 deletions.
42 changes: 0 additions & 42 deletions harper-typst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,6 @@ impl Parser for Typst {
let typst_tree = Markup::from_untyped(typst_document.root())
.expect("Unable to create typst document from parsed tree!");

// Owned collection of nodes forcibly casted to paragraph breaks
let untyped_nodes = typst_tree
.exprs()
.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 exprs: Vec<Expr> = Vec::new();
let mut last_element: Option<Expr> = None;
for ((i, expr), (_, next_expr)) in typst_tree.exprs().enumerate().tuple_windows() {
let mut current_expr = expr;
if let Some(last_element) = last_element {
if should_parbreak(last_element, expr, next_expr) {
let pbreak = typst_syntax::ast::Parbreak::from_untyped(&untyped_nodes[i])
.expect("Unable to convert expression to Parbreak");
current_expr = Expr::Parbreak(pbreak);
}
}
exprs.push(current_expr);
last_element = Some(expr)
}
// Push last element because it will be excluded by tuple_windows() above
if let Some(last) = typst_tree.exprs().last() {
exprs.push(last);
}

dbg!(&exprs);

// Recurse through AST to create tokens
let parse_helper = TypstTranslator::new(&typst_document);
let mut buf = Vec::new();
Expand Down

0 comments on commit 0a6aa45

Please sign in to comment.