aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/algo/walk.rs
blob: 9afa86401bf82e3394a0f7b11f245d77954e01ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use crate::{algo::generate, SyntaxNodeRef};

#[derive(Debug, Copy, Clone)]
pub enum WalkEvent<'a> {
    Enter(SyntaxNodeRef<'a>),
    Exit(SyntaxNodeRef<'a>),
}

pub fn walk<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator<Item = WalkEvent<'a>> {
    generate(Some(WalkEvent::Enter(root)), move |pos| {
        let next = match *pos {
            WalkEvent::Enter(node) => match node.first_child() {
                Some(child) => WalkEvent::Enter(child),
                None => WalkEvent::Exit(node),
            },
            WalkEvent::Exit(node) => {
                if node == root {
                    return None;
                }
                match node.next_sibling() {
                    Some(sibling) => WalkEvent::Enter(sibling),
                    None => WalkEvent::Exit(node.parent().unwrap()),
                }
            }
        };
        Some(next)
    })
}