aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/symbols.rs
blob: 3faf96868fc6aefdfb3f6e6c64d9a2d06239e80e (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use libsyntax2::{
    SyntaxKind, SyntaxNodeRef, SyntaxRoot, AstNode,
    ast::{self, NameOwner},
    algo::{
        visit::{visitor, Visitor},
        walk::{walk, WalkEvent},
    },
};
use TextRange;

#[derive(Debug)]
pub struct FileSymbol {
    pub parent: Option<usize>,
    pub name: String,
    pub name_range: TextRange,
    pub node_range: TextRange,
    pub kind: SyntaxKind,
}


pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> {
    let mut res = Vec::new();
    let mut stack = Vec::new();
    let syntax = file.syntax();

    for event in walk(syntax.as_ref()) {
        match event {
            WalkEvent::Enter(node) => {
                match to_symbol(node) {
                    Some(mut symbol) => {
                        symbol.parent = stack.last().map(|&n| n);
                        stack.push(res.len());
                        res.push(symbol);
                    }
                    None => (),
                }
            }
            WalkEvent::Exit(node) => {
                if to_symbol(node).is_some() {
                    stack.pop().unwrap();
                }
            }
        }
    }
    res
}

fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
    fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option<FileSymbol> {
        let name = node.name()?;
        Some(FileSymbol {
            parent: None,
            name: name.text(),
            name_range: name.syntax().range(),
            node_range: node.syntax().range(),
            kind: node.syntax().kind(),
        })
    }

    visitor()
        .visit(decl::<ast::Function<_>>)
        .visit(decl::<ast::Struct<_>>)
        .visit(decl::<ast::Enum<_>>)
        .visit(decl::<ast::Trait<_>>)
        .visit(decl::<ast::Module<_>>)
        .accept(node)?
}