aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/scope
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/src/scope')
-rw-r--r--crates/libeditor/src/scope/mod.rs7
-rw-r--r--crates/libeditor/src/scope/mod_scope.rs47
2 files changed, 53 insertions, 1 deletions
diff --git a/crates/libeditor/src/scope/mod.rs b/crates/libeditor/src/scope/mod.rs
index 1a77a8b6e..2f25230f8 100644
--- a/crates/libeditor/src/scope/mod.rs
+++ b/crates/libeditor/src/scope/mod.rs
@@ -1,3 +1,8 @@
1mod fn_scope; 1mod fn_scope;
2mod mod_scope;
3
4pub use self::{
5 fn_scope::FnScopes,
6 mod_scope::ModuleScope,
7};
2 8
3pub use self::fn_scope::FnScopes;
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 @@
1use libsyntax2::{
2 AstNode, SyntaxNode, SmolStr, ast
3};
4
5pub struct ModuleScope {
6 entries: Vec<Entry>,
7}
8
9impl 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
31pub struct Entry {
32 name: SyntaxNode,
33}
34
35impl 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