diff options
Diffstat (limited to 'crates/libeditor/src/scope/mod_scope.rs')
-rw-r--r-- | crates/libeditor/src/scope/mod_scope.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/crates/libeditor/src/scope/mod_scope.rs b/crates/libeditor/src/scope/mod_scope.rs new file mode 100644 index 000000000..0e51108d9 --- /dev/null +++ b/crates/libeditor/src/scope/mod_scope.rs | |||
@@ -0,0 +1,47 @@ | |||
1 | use libsyntax2::{ | ||
2 | AstNode, SyntaxNode, SmolStr, ast | ||
3 | }; | ||
4 | |||
5 | pub struct ModuleScope { | ||
6 | entries: Vec<Entry>, | ||
7 | } | ||
8 | |||
9 | impl ModuleScope { | ||
10 | pub fn new(m: ast::Root) -> ModuleScope { | ||
11 | let entries = m.items().filter_map(|item| { | ||
12 | match item { | ||
13 | ast::ModuleItem::StructDef(item) => Entry::new(item), | ||
14 | ast::ModuleItem::EnumDef(item) => Entry::new(item), | ||
15 | ast::ModuleItem::FnDef(item) => Entry::new(item), | ||
16 | ast::ModuleItem::TraitDef(item) => Entry::new(item), | ||
17 | ast::ModuleItem::ExternCrateItem(_) | | ||
18 | ast::ModuleItem::ImplItem(_) | | ||
19 | ast::ModuleItem::UseItem(_) => None | ||
20 | } | ||
21 | }).collect(); | ||
22 | |||
23 | ModuleScope { entries } | ||
24 | } | ||
25 | |||
26 | pub fn entries(&self) -> &[Entry] { | ||
27 | self.entries.as_slice() | ||
28 | } | ||
29 | } | ||
30 | |||
31 | pub struct Entry { | ||
32 | name: SyntaxNode, | ||
33 | } | ||
34 | |||
35 | impl Entry { | ||
36 | fn new<'a>(item: impl ast::NameOwner<'a>) -> Option<Entry> { | ||
37 | let name = item.name()?; | ||
38 | Some(Entry { name: name.syntax().owned() }) | ||
39 | } | ||
40 | pub fn name(&self) -> SmolStr { | ||
41 | self.ast().text() | ||
42 | } | ||
43 | fn ast(&self) -> ast::Name { | ||
44 | ast::Name::cast(self.name.borrowed()).unwrap() | ||
45 | } | ||
46 | } | ||
47 | |||