diff options
author | Aleksey Kladov <[email protected]> | 2018-08-28 17:23:55 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-28 17:23:55 +0100 |
commit | 537ea620bb2a73a5e79872f414af23cf4bf03179 (patch) | |
tree | 875b5c789e027615be5466a74bf7053a6798da0d /crates/libeditor/src/scope | |
parent | d34588bf83898870d7f9b4b49ac2a5f71c77dabb (diff) |
complete items from module scope
Diffstat (limited to 'crates/libeditor/src/scope')
-rw-r--r-- | crates/libeditor/src/scope/mod.rs | 7 | ||||
-rw-r--r-- | crates/libeditor/src/scope/mod_scope.rs | 47 |
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 @@ | |||
1 | mod fn_scope; | 1 | mod fn_scope; |
2 | mod mod_scope; | ||
3 | |||
4 | pub use self::{ | ||
5 | fn_scope::FnScopes, | ||
6 | mod_scope::ModuleScope, | ||
7 | }; | ||
2 | 8 | ||
3 | pub 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 @@ | |||
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 | |||