aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/symbols.rs
blob: 6f8853770888b0d6996b43a9568b506614872fc7 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use smol_str::SmolStr;
use libsyntax2::{
    SyntaxKind, SyntaxNodeRef, SyntaxRoot, AstNode,
    ast::{self, NameOwner},
    algo::{
        visit::{visitor, Visitor},
        walk::{walk, WalkEvent, preorder},
    },
};
use TextRange;

#[derive(Debug)]
pub struct StructureNode {
    pub parent: Option<usize>,
    pub label: String,
    pub navigation_range: TextRange,
    pub node_range: TextRange,
    pub kind: SyntaxKind,
}

#[derive(Debug)]
pub struct FileSymbol {
    pub name: SmolStr,
    pub node_range: TextRange,
    pub kind: SyntaxKind,
}

pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> {
    let syntax = file.syntax();
    preorder(syntax.as_ref())
        .filter_map(to_symbol)
        .collect()
}

fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
    fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option<FileSymbol> {
        let name = node.name()?;
        Some(FileSymbol {
            name: name.text(),
            node_range: node.syntax().range(),
            kind: node.syntax().kind(),
        })
    }
    visitor()
        .visit(decl::<ast::FnDef<_>>)
        .visit(decl::<ast::StructDef<_>>)
        .visit(decl::<ast::EnumDef<_>>)
        .visit(decl::<ast::TraitDef<_>>)
        .visit(decl::<ast::Module<_>>)
        .visit(decl::<ast::TypeDef<_>>)
        .visit(decl::<ast::ConstDef<_>>)
        .visit(decl::<ast::StaticDef<_>>)
        .accept(node)?
}


pub fn file_structure(file: &ast::File) -> Vec<StructureNode> {
    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 structure_node(node) {
                    Some(mut symbol) => {
                        symbol.parent = stack.last().map(|&n| n);
                        stack.push(res.len());
                        res.push(symbol);
                    }
                    None => (),
                }
            }
            WalkEvent::Exit(node) => {
                if structure_node(node).is_some() {
                    stack.pop().unwrap();
                }
            }
        }
    }
    res
}

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

    visitor()
        .visit(decl::<ast::FnDef<_>>)
        .visit(decl::<ast::StructDef<_>>)
        .visit(decl::<ast::NamedField<_>>)
        .visit(decl::<ast::EnumDef<_>>)
        .visit(decl::<ast::TraitDef<_>>)
        .visit(decl::<ast::Module<_>>)
        .visit(decl::<ast::TypeDef<_>>)
        .visit(decl::<ast::ConstDef<_>>)
        .visit(decl::<ast::StaticDef<_>>)
        .visit(|im: ast::ImplItem<_>| {
            let target_type = im.target_type()?;
            let target_trait = im.target_trait();
            let label = match target_trait {
                None => format!("impl {}", target_type.syntax().text()),
                Some(t) => format!(
                    "impl {} for {}",
                    t.syntax().text(),
                    target_type.syntax().text(),
                ),
            };

            let node = StructureNode {
                parent: None,
                label,
                navigation_range: target_type.syntax().range(),
                node_range: im.syntax().range(),
                kind: im.syntax().kind(),
            };
            Some(node)
        })
        .accept(node)?
}