From 537ea620bb2a73a5e79872f414af23cf4bf03179 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 Aug 2018 19:23:55 +0300 Subject: complete items from module scope --- crates/libeditor/src/scope/mod.rs | 7 ++++- crates/libeditor/src/scope/mod_scope.rs | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 crates/libeditor/src/scope/mod_scope.rs (limited to 'crates/libeditor/src/scope') 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 @@ mod fn_scope; +mod mod_scope; + +pub use self::{ + fn_scope::FnScopes, + mod_scope::ModuleScope, +}; -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 @@ +use libsyntax2::{ + AstNode, SyntaxNode, SmolStr, ast +}; + +pub struct ModuleScope { + entries: Vec, +} + +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::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 { + 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() + } +} + -- cgit v1.2.3