Skip to main content

typst_analyzer_analysis/
node.rs

1use typst_syntax::{LinkedNode, SyntaxKind, SyntaxNode};
2
3/// Walks down the AST from current cursor position and Returns a vector of SyntaxKind.
4/// Must provide markup in vector in all cases since thas is the root.
5pub fn node_walker(pos: usize, ast: &SyntaxNode) -> Vec<SyntaxKind> {
6    let linked_root = LinkedNode::new(ast);
7    // Find the LinkedNode at the cursor position
8    let current_node = linked_root.leaf_at(pos, typst_syntax::Side::Before);
9    // lets get markup too and with kind give completions if the node contains markup we will
10    // provide normal static cmp.
11    let mut nodes: Vec<SyntaxKind> = Vec::new();
12    if let Some(node) = current_node {
13        nodes.push(node.clone().kind());
14        // Loop to find the parent and its parents
15        let mut parent = node.parent();
16        while let Some(p) = parent {
17            nodes.push(p.clone().kind());
18            parent = p.parent();
19        }
20    }
21    nodes
22}