From dd5c2dc5bf9e9dee863bd79105b1782b654221f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:49:05 +0300 Subject: Move ImportId --- crates/ra_hir/src/code_model.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 50c9a79fc..07c56843a 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -9,11 +9,11 @@ use hir_def::{ body::scope::ExprScopes, builtin_type::BuiltinType, docs::Documentation, - nameres::{per_ns::PerNs, raw::ImportId}, + nameres::per_ns::PerNs, resolver::{HasResolver, TypeNs}, type_ref::TypeRef, - ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, - ModuleId, UnionId, + ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalImportId, + LocalStructFieldId, Lookup, ModuleId, UnionId, }; use hir_expand::{ diagnostics::DiagnosticSink, @@ -229,7 +229,7 @@ impl Module { pub struct Import { pub(crate) parent: Module, - pub(crate) id: ImportId, + pub(crate) id: LocalImportId, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -- cgit v1.2.3 From 158b1cb524d8e07aa7a6ec2342bca2ce4667d316 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:49:53 +0300 Subject: Rename CrateModuleId --- crates/ra_hir/src/code_model.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 07c56843a..2442fb6a5 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -12,7 +12,7 @@ use hir_def::{ nameres::per_ns::PerNs, resolver::{HasResolver, TypeNs}, type_ref::TypeRef, - ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalImportId, + ContainerId, HasModule, ImplId, LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, UnionId, }; use hir_expand::{ @@ -112,7 +112,7 @@ impl_froms!( pub use hir_def::{attr::Attrs, ModuleSource}; impl Module { - pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module { + pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { Module { id: ModuleId { krate: krate.crate_id, module_id: crate_module_id } } } @@ -222,7 +222,7 @@ impl Module { def_map[self.id.module_id].impls.iter().copied().map(ImplBlock::from).collect() } - fn with_module_id(self, module_id: CrateModuleId) -> Module { + fn with_module_id(self, module_id: LocalModuleId) -> Module { Module::new(self.krate(), module_id) } } -- cgit v1.2.3 From 6bdd5fa461ba0f3f3697339ffb560c577e3b0cc6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:53:16 +0300 Subject: Privatise nameres --- crates/ra_hir/src/code_model.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 2442fb6a5..cd178bf88 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -9,7 +9,7 @@ use hir_def::{ body::scope::ExprScopes, builtin_type::BuiltinType, docs::Documentation, - nameres::per_ns::PerNs, + per_ns::PerNs, resolver::{HasResolver, TypeNs}, type_ref::TypeRef, ContainerId, HasModule, ImplId, LocalEnumVariantId, LocalImportId, LocalModuleId, -- cgit v1.2.3 From ffc2325d194d2523456484a7dec1f175c729c1b5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 17:06:04 +0300 Subject: Move ModuleSource back to hir --- crates/ra_hir/src/code_model.rs | 65 +++++++++++++++++++++++++++++++++++-- crates/ra_hir/src/code_model/src.rs | 4 ++- 2 files changed, 65 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index cd178bf88..fd7776fb7 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -18,9 +18,10 @@ use hir_def::{ use hir_expand::{ diagnostics::DiagnosticSink, name::{self, AsName}, + AstId, }; -use ra_db::{CrateId, Edition}; -use ra_syntax::ast; +use ra_db::{CrateId, Edition, FileId, FilePosition}; +use ra_syntax::{ast, AstNode, SyntaxNode}; use crate::{ db::{DefDatabase, HirDatabase}, @@ -78,6 +79,64 @@ impl Crate { } } +pub enum ModuleSource { + SourceFile(ast::SourceFile), + Module(ast::Module), +} + +impl ModuleSource { + pub fn new( + db: &impl DefDatabase, + file_id: Option, + decl_id: Option>, + ) -> ModuleSource { + match (file_id, decl_id) { + (Some(file_id), _) => { + let source_file = db.parse(file_id).tree(); + ModuleSource::SourceFile(source_file) + } + (None, Some(item_id)) => { + let module = item_id.to_node(db); + assert!(module.item_list().is_some(), "expected inline module"); + ModuleSource::Module(module) + } + (None, None) => panic!(), + } + } + + // FIXME: this methods do not belong here + pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource { + let parse = db.parse(position.file_id); + match &ra_syntax::algo::find_node_at_offset::( + parse.tree().syntax(), + position.offset, + ) { + Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), + _ => { + let source_file = parse.tree(); + ModuleSource::SourceFile(source_file) + } + } + } + + pub fn from_child_node(db: &impl DefDatabase, child: Source<&SyntaxNode>) -> ModuleSource { + if let Some(m) = + child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) + { + ModuleSource::Module(m) + } else { + let file_id = child.file_id.original_file(db); + let source_file = db.parse(file_id).tree(); + ModuleSource::SourceFile(source_file) + } + } + + pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource { + let source_file = db.parse(file_id).tree(); + ModuleSource::SourceFile(source_file) + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Module { pub(crate) id: ModuleId, @@ -109,7 +168,7 @@ impl_froms!( BuiltinType ); -pub use hir_def::{attr::Attrs, ModuleSource}; +pub use hir_def::attr::Attrs; impl Module { pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 402f821bf..b7bafe23d 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -117,7 +117,9 @@ impl HasSource for Import { fn source(self, db: &impl DefDatabase) -> Source { let src = self.parent.definition_source(db); let (_, source_map) = db.raw_items_with_source_map(src.file_id); - src.with_value(source_map.get(&src.value, self.id)) + let root = db.parse_or_expand(src.file_id).unwrap(); + let ptr = source_map.get(self.id); + src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root))) } } -- cgit v1.2.3