From 733383446fc229a35d4432d14c295c5a01e5a87f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 6 Jan 2019 17:44:50 +0300 Subject: move submodule computationt to module_tree --- crates/ra_hir/src/code_model_impl.rs | 2 +- crates/ra_hir/src/db.rs | 2 +- crates/ra_hir/src/module_tree.rs | 34 +++++++++++++++++++++ crates/ra_hir/src/query_definitions.rs | 54 ++-------------------------------- 4 files changed, 39 insertions(+), 53 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs index d602a439d..157b0c616 100644 --- a/crates/ra_hir/src/code_model_impl.rs +++ b/crates/ra_hir/src/code_model_impl.rs @@ -1,2 +1,2 @@ mod krate; // `crate` is invalid ident :( -pub(crate) mod module; +mod module; diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 2702961ba..e4249de14 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -73,7 +73,7 @@ pub trait HirDatabase: SyntaxDatabase fn submodules(source: ModuleSource) -> Cancelable>> { type SubmodulesQuery; - use fn query_definitions::submodules; + use fn crate::module_tree::Submodule::submodules_query; } fn input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { diff --git a/crates/ra_hir/src/module_tree.rs b/crates/ra_hir/src/module_tree.rs index cd82f25db..b7912ba5e 100644 --- a/crates/ra_hir/src/module_tree.rs +++ b/crates/ra_hir/src/module_tree.rs @@ -20,6 +20,40 @@ pub enum Submodule { } impl Submodule { + pub(crate) fn submodules_query( + db: &impl HirDatabase, + source: ModuleSource, + ) -> Cancelable>> { + db.check_canceled()?; + let file_id = source.file_id(); + let submodules = match source.resolve(db) { + ModuleSourceNode::SourceFile(it) => collect_submodules(db, file_id, it.borrowed()), + ModuleSourceNode::Module(it) => it + .borrowed() + .item_list() + .map(|it| collect_submodules(db, file_id, it)) + .unwrap_or_else(Vec::new), + }; + return Ok(Arc::new(submodules)); + + fn collect_submodules<'a>( + db: &impl HirDatabase, + file_id: HirFileId, + root: impl ast::ModuleItemOwner<'a>, + ) -> Vec { + modules(root) + .map(|(name, m)| { + if m.has_semi() { + Submodule::Declaration(name) + } else { + let src = ModuleSource::new_inline(db, file_id, m); + Submodule::Definition(name, src) + } + }) + .collect() + } + } + fn name(&self) -> &Name { match self { Submodule::Declaration(name) => name, diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index b17c00e26..f4b380022 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -6,16 +6,16 @@ use std::{ use rustc_hash::FxHashMap; use ra_syntax::{ AstNode, SyntaxNode, - ast::{self, NameOwner, ModuleItemOwner} + ast::{self, ModuleItemOwner} }; use ra_db::{SourceRootId, Cancelable,}; use crate::{ - SourceFileItems, SourceItemId, DefKind, DefId, Name, AsName, HirFileId, + SourceFileItems, SourceItemId, DefKind, DefId, HirFileId, MacroCallLoc, db::HirDatabase, function::FnScopes, - module_tree::{ModuleId, Submodule, ModuleSource, ModuleSourceNode}, + module_tree::{ModuleId, ModuleSourceNode}, nameres::{InputModuleItems, ItemMap, Resolver}, adt::{StructData, EnumData}, }; @@ -58,54 +58,6 @@ pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> } } -pub(crate) fn submodules( - db: &impl HirDatabase, - source: ModuleSource, -) -> Cancelable>> { - db.check_canceled()?; - let file_id = source.file_id(); - let submodules = match source.resolve(db) { - ModuleSourceNode::SourceFile(it) => collect_submodules(db, file_id, it.borrowed()), - ModuleSourceNode::Module(it) => it - .borrowed() - .item_list() - .map(|it| collect_submodules(db, file_id, it)) - .unwrap_or_else(Vec::new), - }; - return Ok(Arc::new(submodules)); - - fn collect_submodules<'a>( - db: &impl HirDatabase, - file_id: HirFileId, - root: impl ast::ModuleItemOwner<'a>, - ) -> Vec { - modules(root) - .map(|(name, m)| { - if m.has_semi() { - Submodule::Declaration(name) - } else { - let src = ModuleSource::new_inline(db, file_id, m); - Submodule::Definition(name, src) - } - }) - .collect() - } -} - -pub(crate) fn modules<'a>( - root: impl ast::ModuleItemOwner<'a>, -) -> impl Iterator)> { - root.items() - .filter_map(|item| match item { - ast::ModuleItem::Module(m) => Some(m), - _ => None, - }) - .filter_map(|module| { - let name = module.name()?.as_name(); - Some((name, module)) - }) -} - pub(super) fn input_module_items( db: &impl HirDatabase, source_root_id: SourceRootId, -- cgit v1.2.3