aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/scope/mod_scope.rs
blob: aa8dd93a7f1fe85b30cb995ddbbca154832a477c (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
use libsyntax2::{
    AstNode, SyntaxNode, SmolStr, ast
};

pub struct ModuleScope {
    entries: Vec<Entry>,
}

impl ModuleScope {
    pub fn new(m: ast::Root) -> ModuleScope {
        let entries = m.items().filter_map(|item| {
            match item {
                ast::ModuleItem::StructDef(item) => Entry::new(item),
                ast::ModuleItem::EnumDef(item) => Entry::new(item),
                ast::ModuleItem::FnDef(item) => Entry::new(item),
                ast::ModuleItem::ConstDef(item) => Entry::new(item),
                ast::ModuleItem::StaticDef(item) => Entry::new(item),
                ast::ModuleItem::TraitDef(item) => Entry::new(item),
                ast::ModuleItem::ExternCrateItem(_) |
                ast::ModuleItem::ImplItem(_) |
                ast::ModuleItem::UseItem(_) => None
            }
        }).collect();

        ModuleScope { entries }
    }

    pub fn entries(&self) -> &[Entry] {
        self.entries.as_slice()
    }
}

pub struct Entry {
    name: SyntaxNode,
}

impl Entry {
    fn new<'a>(item: impl ast::NameOwner<'a>) -> Option<Entry> {
        let name = item.name()?;
        Some(Entry { name: name.syntax().owned() })
    }
    pub fn name(&self) -> SmolStr {
        self.ast().text()
    }
    fn ast(&self) -> ast::Name {
        ast::Name::cast(self.name.borrowed()).unwrap()
    }
}