From b5021411a84822cb3f1e3aeffad9550dd15bdeb6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 16 Sep 2018 12:54:24 +0300 Subject: rename all things --- crates/ra_editor/src/scope/mod_scope.rs | 115 ++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 crates/ra_editor/src/scope/mod_scope.rs (limited to 'crates/ra_editor/src/scope/mod_scope.rs') diff --git a/crates/ra_editor/src/scope/mod_scope.rs b/crates/ra_editor/src/scope/mod_scope.rs new file mode 100644 index 000000000..d2a3e7c58 --- /dev/null +++ b/crates/ra_editor/src/scope/mod_scope.rs @@ -0,0 +1,115 @@ +use ra_syntax::{ + AstNode, SyntaxNode, SyntaxNodeRef, SmolStr, + ast::{self, AstChildren}, +}; + +pub struct ModuleScope { + entries: Vec, +} + +pub struct Entry { + node: SyntaxNode, + kind: EntryKind, +} + +enum EntryKind { + Item, Import, +} + +impl ModuleScope { + pub fn new(items: AstChildren) -> ModuleScope { + let mut entries = Vec::new(); + for item in items { + let entry = 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::TypeDef(item) => Entry::new(item), + ast::ModuleItem::Module(item) => Entry::new(item), + ast::ModuleItem::UseItem(item) => { + if let Some(tree) = item.use_tree() { + collect_imports(tree, &mut entries); + } + continue; + }, + ast::ModuleItem::ExternCrateItem(_) | + ast::ModuleItem::ImplItem(_) => continue, + }; + entries.extend(entry) + } + + ModuleScope { entries } + } + + pub fn entries(&self) -> &[Entry] { + self.entries.as_slice() + } +} + +impl Entry { + fn new<'a>(item: impl ast::NameOwner<'a>) -> Option { + let name = item.name()?; + Some(Entry { node: name.syntax().owned(), kind: EntryKind::Item }) + } + fn new_import(path: ast::Path) -> Option { + let name_ref = path.segment()?.name_ref()?; + Some(Entry { node: name_ref.syntax().owned(), kind: EntryKind::Import }) + } + pub fn name(&self) -> SmolStr { + match self.kind { + EntryKind::Item => + ast::Name::cast(self.node.borrowed()).unwrap() + .text(), + EntryKind::Import => + ast::NameRef::cast(self.node.borrowed()).unwrap() + .text(), + } + } + pub fn syntax(&self) -> SyntaxNodeRef { + self.node.borrowed() + } +} + +fn collect_imports(tree: ast::UseTree, acc: &mut Vec) { + if let Some(use_tree_list) = tree.use_tree_list() { + return use_tree_list.use_trees().for_each(|it| collect_imports(it, acc)); + } + if let Some(path) = tree.path() { + acc.extend(Entry::new_import(path)); + } +} + + +#[cfg(test)] +mod tests { + use super::*; + use ra_syntax::{File, ast::ModuleItemOwner}; + + fn do_check(code: &str, expected: &[&str]) { + let file = File::parse(&code); + let scope = ModuleScope::new(file.ast().items()); + let actual = scope.entries + .iter() + .map(|it| it.name()) + .collect::>(); + assert_eq!(expected, actual.as_slice()); + } + + #[test] + fn test_module_scope() { + do_check(" + struct Foo; + enum Bar {} + mod baz {} + fn quux() {} + use x::{ + y::z, + t, + }; + type T = (); + ", &["Foo", "Bar", "baz", "quux", "z", "t", "T"]) + } +} -- cgit v1.2.3