From 59e29aef633e906837f8fed604435976a46be691 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 04:09:44 +0300 Subject: Move hir to a separate crate --- crates/ra_hir/src/module/nameres.rs | 134 ++++-------------------------------- 1 file changed, 14 insertions(+), 120 deletions(-) (limited to 'crates/ra_hir/src/module/nameres.rs') diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 513a37646..837a8d5ae 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -38,20 +38,20 @@ use crate::{ /// Item map is the result of the name resolution. Item map contains, for each /// module, the set of visible items. #[derive(Default, Debug, PartialEq, Eq)] -pub(crate) struct ItemMap { - pub(crate) per_module: FxHashMap, +pub struct ItemMap { + pub per_module: FxHashMap, } #[derive(Debug, Default, PartialEq, Eq, Clone)] -pub(crate) struct ModuleScope { - items: FxHashMap, +pub struct ModuleScope { + pub items: FxHashMap, } impl ModuleScope { - pub(crate) fn entries<'a>(&'a self) -> impl Iterator + 'a { + pub fn entries<'a>(&'a self) -> impl Iterator + 'a { self.items.iter() } - pub(crate) fn get(&self, name: &SmolStr) -> Option<&Resolution> { + pub fn get(&self, name: &SmolStr) -> Option<&Resolution> { self.items.get(name) } } @@ -63,7 +63,7 @@ impl ModuleScope { /// recomputing name res: if `InputModuleItems` are the same, we can avoid /// running name resolution. #[derive(Debug, Default, PartialEq, Eq)] -pub(crate) struct InputModuleItems { +pub struct InputModuleItems { items: Vec, imports: Vec, } @@ -89,13 +89,13 @@ struct Import { } #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) struct NamedImport { - file_item_id: SourceFileItemId, - relative_range: TextRange, +pub struct NamedImport { + pub file_item_id: SourceFileItemId, + pub relative_range: TextRange, } impl NamedImport { - pub(crate) fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { + pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { let source_item_id = SourceItemId { file_id, item_id: self.file_item_id, @@ -115,11 +115,11 @@ enum ImportKind { /// Resolution is basically `DefId` atm, but it should account for stuff like /// multiple namespaces, ambiguity and errors. #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Resolution { +pub struct Resolution { /// None for unresolved - pub(crate) def_id: Option, + pub def_id: Option, /// ident by whitch this is imported into local scope. - pub(crate) import: Option, + pub import: Option, } // #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -336,109 +336,3 @@ where f(module_items) } } - -#[cfg(test)] -mod tests { - use ra_db::FilesDatabase; - use crate::{ - AnalysisChange, - mock_analysis::{MockAnalysis, analysis_and_position}, - hir::{self, HirDatabase}, -}; - use super::*; - - fn item_map(fixture: &str) -> (Arc, ModuleId) { - let (analysis, pos) = analysis_and_position(fixture); - let db = analysis.imp.db; - let source_root = db.file_source_root(pos.file_id); - let descr = hir::Module::guess_from_position(&*db, pos) - .unwrap() - .unwrap(); - let module_id = descr.module_id; - (db.item_map(source_root).unwrap(), module_id) - } - - #[test] - fn test_item_map() { - let (item_map, module_id) = item_map( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - <|> - - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - let name = SmolStr::from("Baz"); - let resolution = &item_map.per_module[&module_id].items[&name]; - assert!(resolution.def_id.is_some()); - } - - #[test] - fn typing_inside_a_function_should_not_invalidate_item_map() { - let mock_analysis = MockAnalysis::with_files( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { - 1 + 1 - } - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - - let file_id = mock_analysis.id_of("/lib.rs"); - let mut host = mock_analysis.analysis_host(); - - let source_root = host.analysis().imp.db.file_source_root(file_id); - - { - let db = host.analysis().imp.db; - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!(format!("{:?}", events).contains("item_map")) - } - - let mut change = AnalysisChange::new(); - - change.change_file( - file_id, - " - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { 92 } - " - .to_string(), - ); - - host.apply_change(change); - - { - let db = host.analysis().imp.db; - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!( - !format!("{:?}", events).contains("_item_map"), - "{:#?}", - events - ) - } - } -} -- cgit v1.2.3