aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs65
-rw-r--r--crates/ra_hir/src/code_model/src.rs9
-rw-r--r--crates/ra_hir/src/from_source.rs15
-rw-r--r--crates/ra_hir/src/lib.rs5
-rw-r--r--crates/ra_hir/src/source_binder.rs3
-rw-r--r--crates/ra_hir_def/src/nameres.rs132
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs7
-rw-r--r--crates/ra_hir_ty/src/test_db.rs2
-rw-r--r--crates/ra_ide/src/display/navigation_target.rs42
-rw-r--r--crates/ra_ide/src/parent_module.rs5
10 files changed, 149 insertions, 136 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 5877afefa..7706399ae 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -11,6 +11,7 @@ use hir_def::{
11 builtin_type::BuiltinType, 11 builtin_type::BuiltinType,
12 docs::Documentation, 12 docs::Documentation,
13 expr::{BindingAnnotation, Pat, PatId}, 13 expr::{BindingAnnotation, Pat, PatId},
14 nameres::ModuleSource,
14 per_ns::PerNs, 15 per_ns::PerNs,
15 resolver::HasResolver, 16 resolver::HasResolver,
16 type_ref::{Mutability, TypeRef}, 17 type_ref::{Mutability, TypeRef},
@@ -21,11 +22,11 @@ use hir_def::{
21use hir_expand::{ 22use hir_expand::{
22 diagnostics::DiagnosticSink, 23 diagnostics::DiagnosticSink,
23 name::{self, AsName}, 24 name::{self, AsName},
24 AstId, MacroDefId, 25 MacroDefId,
25}; 26};
26use hir_ty::expr::ExprValidator; 27use hir_ty::expr::ExprValidator;
27use ra_db::{CrateId, Edition, FileId, FilePosition}; 28use ra_db::{CrateId, Edition};
28use ra_syntax::{ast, AstNode, SyntaxNode}; 29use ra_syntax::ast;
29 30
30use crate::{ 31use crate::{
31 db::{DefDatabase, HirDatabase}, 32 db::{DefDatabase, HirDatabase},
@@ -79,64 +80,6 @@ impl Crate {
79 } 80 }
80} 81}
81 82
82pub enum ModuleSource {
83 SourceFile(ast::SourceFile),
84 Module(ast::Module),
85}
86
87impl ModuleSource {
88 pub fn new(
89 db: &impl DefDatabase,
90 file_id: Option<FileId>,
91 decl_id: Option<AstId<ast::Module>>,
92 ) -> ModuleSource {
93 match (file_id, decl_id) {
94 (Some(file_id), _) => {
95 let source_file = db.parse(file_id).tree();
96 ModuleSource::SourceFile(source_file)
97 }
98 (None, Some(item_id)) => {
99 let module = item_id.to_node(db);
100 assert!(module.item_list().is_some(), "expected inline module");
101 ModuleSource::Module(module)
102 }
103 (None, None) => panic!(),
104 }
105 }
106
107 // FIXME: this methods do not belong here
108 pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource {
109 let parse = db.parse(position.file_id);
110 match &ra_syntax::algo::find_node_at_offset::<ast::Module>(
111 parse.tree().syntax(),
112 position.offset,
113 ) {
114 Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
115 _ => {
116 let source_file = parse.tree();
117 ModuleSource::SourceFile(source_file)
118 }
119 }
120 }
121
122 pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource {
123 if let Some(m) =
124 child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi())
125 {
126 ModuleSource::Module(m)
127 } else {
128 let file_id = child.file_id.original_file(db);
129 let source_file = db.parse(file_id).tree();
130 ModuleSource::SourceFile(source_file)
131 }
132 }
133
134 pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource {
135 let source_file = db.parse(file_id).tree();
136 ModuleSource::SourceFile(source_file)
137 }
138}
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
141pub struct Module { 84pub struct Module {
142 pub(crate) id: ModuleId, 85 pub(crate) id: ModuleId,
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index 36cfbc8f1..d9bccd902 100644
--- a/crates/ra_hir/src/code_model/src.rs
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -2,6 +2,7 @@
2 2
3use either::Either; 3use either::Either;
4use hir_def::{ 4use hir_def::{
5 nameres::ModuleSource,
5 src::{HasChildSource, HasSource as _}, 6 src::{HasChildSource, HasSource as _},
6 AstItemDef, Lookup, VariantId, 7 AstItemDef, Lookup, VariantId,
7}; 8};
@@ -9,7 +10,7 @@ use ra_syntax::ast;
9 10
10use crate::{ 11use crate::{
11 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, 12 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef,
12 Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, 13 Module, Static, Struct, StructField, Trait, TypeAlias, Union,
13}; 14};
14 15
15pub use hir_expand::InFile; 16pub use hir_expand::InFile;
@@ -25,11 +26,7 @@ impl Module {
25 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. 26 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
26 pub fn definition_source(self, db: &impl DefDatabase) -> InFile<ModuleSource> { 27 pub fn definition_source(self, db: &impl DefDatabase) -> InFile<ModuleSource> {
27 let def_map = db.crate_def_map(self.id.krate); 28 let def_map = db.crate_def_map(self.id.krate);
28 let src = def_map[self.id.local_id].definition_source(db); 29 def_map[self.id.local_id].definition_source(db)
29 src.map(|it| match it {
30 Either::Left(it) => ModuleSource::SourceFile(it),
31 Either::Right(it) => ModuleSource::Module(it),
32 })
33 } 30 }
34 31
35 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. 32 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 82bf641dc..18d87f6d7 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -1,6 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir_def::{AstItemDef, LocationCtx, ModuleId}; 3use hir_def::{nameres::ModuleSource, AstItemDef, LocationCtx, ModuleId};
4use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; 4use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
5use ra_syntax::{ 5use ra_syntax::{
6 ast::{self, AstNode, NameOwner}, 6 ast::{self, AstNode, NameOwner},
@@ -10,8 +10,8 @@ use ra_syntax::{
10use crate::{ 10use crate::{
11 db::{AstDatabase, DefDatabase, HirDatabase}, 11 db::{AstDatabase, DefDatabase, HirDatabase},
12 AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, 12 AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
13 InFile, Local, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, 13 InFile, Local, MacroDef, Module, ModuleDef, Static, Struct, StructField, Trait, TypeAlias,
14 TypeAlias, Union, VariantDef, 14 Union, VariantDef,
15}; 15};
16 16
17pub trait FromSource: Sized { 17pub trait FromSource: Sized {
@@ -235,11 +235,10 @@ impl Module {
235 let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; 235 let src_parent = InFile { file_id: src.file_id, value: parent_declaration };
236 Module::from_declaration(db, src_parent) 236 Module::from_declaration(db, src_parent)
237 } 237 }
238 _ => { 238 None => {
239 let src_parent = InFile { 239 let source_file = db.parse(src.file_id.original_file(db)).tree();
240 file_id: src.file_id, 240 let src_parent =
241 value: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), 241 InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) };
242 };
243 Module::from_definition(db, src_parent) 242 Module::from_definition(db, src_parent)
244 } 243 }
245 }?; 244 }?;
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 853760cb1..f12e4ca3f 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -43,8 +43,8 @@ pub use crate::{
43 code_model::{ 43 code_model::{
44 src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, 44 src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
45 DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, 45 DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam,
46 HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, 46 HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
47 Static, Struct, StructField, Trait, Type, TypeAlias, Union, VariantDef, 47 StructField, Trait, Type, TypeAlias, Union, VariantDef,
48 }, 48 },
49 from_source::FromSource, 49 from_source::FromSource,
50 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, 50 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
@@ -59,6 +59,7 @@ pub use hir_def::{
59 body::scope::ExprScopes, 59 body::scope::ExprScopes,
60 builtin_type::BuiltinType, 60 builtin_type::BuiltinType,
61 docs::Documentation, 61 docs::Documentation,
62 nameres::ModuleSource,
62 path::{Path, PathKind}, 63 path::{Path, PathKind},
63 type_ref::Mutability, 64 type_ref::Mutability,
64}; 65};
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 28d41b647..db0451059 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -14,6 +14,7 @@ use hir_def::{
14 BodySourceMap, 14 BodySourceMap,
15 }, 15 },
16 expr::{ExprId, PatId}, 16 expr::{ExprId, PatId},
17 nameres::ModuleSource,
17 path::known, 18 path::known,
18 resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, 19 resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
19 AssocItemId, DefWithBodyId, 20 AssocItemId, DefWithBodyId,
@@ -46,7 +47,7 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -
46 Some(crate::Module::from_declaration(db, src)?.id.resolver(db)) 47 Some(crate::Module::from_declaration(db, src)?.id.resolver(db))
47 }, 48 },
48 ast::SourceFile(it) => { 49 ast::SourceFile(it) => {
49 let src = node.with_value(crate::ModuleSource::SourceFile(it)); 50 let src = node.with_value(ModuleSource::SourceFile(it));
50 Some(crate::Module::from_definition(db, src)?.id.resolver(db)) 51 Some(crate::Module::from_definition(db, src)?.id.resolver(db))
51 }, 52 },
52 ast::StructDef(it) => { 53 ast::StructDef(it) => {
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index faf3566f4..bd237a7b3 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -57,15 +57,17 @@ mod tests;
57 57
58use std::sync::Arc; 58use std::sync::Arc;
59 59
60use either::Either;
61use hir_expand::{ 60use hir_expand::{
62 ast_id_map::FileAstId, diagnostics::DiagnosticSink, name::Name, InFile, MacroDefId, 61 ast_id_map::FileAstId, diagnostics::DiagnosticSink, name::Name, InFile, MacroDefId,
63}; 62};
64use once_cell::sync::Lazy; 63use once_cell::sync::Lazy;
65use ra_arena::Arena; 64use ra_arena::Arena;
66use ra_db::{CrateId, Edition, FileId}; 65use ra_db::{CrateId, Edition, FileId, FilePosition};
67use ra_prof::profile; 66use ra_prof::profile;
68use ra_syntax::ast; 67use ra_syntax::{
68 ast::{self, AstNode},
69 SyntaxNode,
70};
69use rustc_hash::FxHashMap; 71use rustc_hash::FxHashMap;
70 72
71use crate::{ 73use crate::{
@@ -100,19 +102,76 @@ impl std::ops::Index<LocalModuleId> for CrateDefMap {
100 } 102 }
101} 103}
102 104
105#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
106pub enum ModuleOrigin {
107 CrateRoot {
108 definition: FileId,
109 },
110 /// Note that non-inline modules, by definition, live inside non-macro file.
111 File {
112 declaration: AstId<ast::Module>,
113 definition: FileId,
114 },
115 Inline {
116 definition: AstId<ast::Module>,
117 },
118}
119
120impl Default for ModuleOrigin {
121 fn default() -> Self {
122 ModuleOrigin::CrateRoot { definition: FileId(0) }
123 }
124}
125
126impl ModuleOrigin {
127 pub(crate) fn not_sure_file(file: Option<FileId>, declaration: AstId<ast::Module>) -> Self {
128 match file {
129 None => ModuleOrigin::Inline { definition: declaration },
130 Some(definition) => ModuleOrigin::File { declaration, definition },
131 }
132 }
133
134 fn declaration(&self) -> Option<AstId<ast::Module>> {
135 match self {
136 ModuleOrigin::File { declaration: module, .. }
137 | ModuleOrigin::Inline { definition: module, .. } => Some(*module),
138 ModuleOrigin::CrateRoot { .. } => None,
139 }
140 }
141
142 pub fn file_id(&self) -> Option<FileId> {
143 match self {
144 ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
145 Some(*definition)
146 }
147 _ => None,
148 }
149 }
150
151 /// Returns a node which defines this module.
152 /// That is, a file or a `mod foo {}` with items.
153 fn definition_source(&self, db: &impl DefDatabase) -> InFile<ModuleSource> {
154 match self {
155 ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
156 let file_id = *definition;
157 let sf = db.parse(file_id).tree();
158 return InFile::new(file_id.into(), ModuleSource::SourceFile(sf));
159 }
160 ModuleOrigin::Inline { definition } => {
161 InFile::new(definition.file_id, ModuleSource::Module(definition.to_node(db)))
162 }
163 }
164 }
165}
166
103#[derive(Default, Debug, PartialEq, Eq)] 167#[derive(Default, Debug, PartialEq, Eq)]
104pub struct ModuleData { 168pub struct ModuleData {
105 pub parent: Option<LocalModuleId>, 169 pub parent: Option<LocalModuleId>,
106 pub children: FxHashMap<Name, LocalModuleId>, 170 pub children: FxHashMap<Name, LocalModuleId>,
107 pub scope: ModuleScope, 171 pub scope: ModuleScope,
108 172
109 // FIXME: these can't be both null, we need a three-state enum here. 173 /// Where does this module come from?
110 /// None for root 174 pub origin: ModuleOrigin,
111 pub declaration: Option<AstId<ast::Module>>,
112 /// None for inline modules.
113 ///
114 /// Note that non-inline modules, by definition, live inside non-macro file.
115 pub definition: Option<FileId>,
116 175
117 pub impls: Vec<ImplId>, 176 pub impls: Vec<ImplId>,
118} 177}
@@ -262,7 +321,7 @@ impl CrateDefMap {
262 pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ { 321 pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ {
263 self.modules 322 self.modules
264 .iter() 323 .iter()
265 .filter(move |(_id, data)| data.definition == Some(file_id)) 324 .filter(move |(_id, data)| data.origin.file_id() == Some(file_id))
266 .map(|(id, _data)| id) 325 .map(|(id, _data)| id)
267 } 326 }
268 327
@@ -281,27 +340,54 @@ impl CrateDefMap {
281 340
282impl ModuleData { 341impl ModuleData {
283 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. 342 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
284 pub fn definition_source( 343 pub fn definition_source(&self, db: &impl DefDatabase) -> InFile<ModuleSource> {
285 &self, 344 self.origin.definition_source(db)
286 db: &impl DefDatabase,
287 ) -> InFile<Either<ast::SourceFile, ast::Module>> {
288 if let Some(file_id) = self.definition {
289 let sf = db.parse(file_id).tree();
290 return InFile::new(file_id.into(), Either::Left(sf));
291 }
292 let decl = self.declaration.unwrap();
293 InFile::new(decl.file_id, Either::Right(decl.to_node(db)))
294 } 345 }
295 346
296 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. 347 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
297 /// `None` for the crate root. 348 /// `None` for the crate root or block.
298 pub fn declaration_source(&self, db: &impl DefDatabase) -> Option<InFile<ast::Module>> { 349 pub fn declaration_source(&self, db: &impl DefDatabase) -> Option<InFile<ast::Module>> {
299 let decl = self.declaration?; 350 let decl = self.origin.declaration()?;
300 let value = decl.to_node(db); 351 let value = decl.to_node(db);
301 Some(InFile { file_id: decl.file_id, value }) 352 Some(InFile { file_id: decl.file_id, value })
302 } 353 }
303} 354}
304 355
356#[derive(Debug, Clone, PartialEq, Eq)]
357pub enum ModuleSource {
358 SourceFile(ast::SourceFile),
359 Module(ast::Module),
360}
361
362impl ModuleSource {
363 // FIXME: this methods do not belong here
364 pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource {
365 let parse = db.parse(position.file_id);
366 match &ra_syntax::algo::find_node_at_offset::<ast::Module>(
367 parse.tree().syntax(),
368 position.offset,
369 ) {
370 Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
371 _ => {
372 let source_file = parse.tree();
373 ModuleSource::SourceFile(source_file)
374 }
375 }
376 }
377
378 pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource {
379 if let Some(m) =
380 child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi())
381 {
382 ModuleSource::Module(m)
383 } else {
384 let file_id = child.file_id.original_file(db);
385 let source_file = db.parse(file_id).tree();
386 ModuleSource::SourceFile(source_file)
387 }
388 }
389}
390
305mod diagnostics { 391mod diagnostics {
306 use hir_expand::diagnostics::DiagnosticSink; 392 use hir_expand::diagnostics::DiagnosticSink;
307 use ra_db::RelativePathBuf; 393 use ra_db::RelativePathBuf;
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index d4bfcae1d..9d948d4f4 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -19,7 +19,7 @@ use crate::{
19 db::DefDatabase, 19 db::DefDatabase,
20 nameres::{ 20 nameres::{
21 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, 21 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint,
22 raw, BuiltinShadowMode, CrateDefMap, ModuleData, Resolution, ResolveMode, 22 raw, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, Resolution, ResolveMode,
23 }, 23 },
24 path::{Path, PathKind}, 24 path::{Path, PathKind},
25 per_ns::PerNs, 25 per_ns::PerNs,
@@ -131,7 +131,7 @@ where
131 let file_id = crate_graph.crate_root(self.def_map.krate); 131 let file_id = crate_graph.crate_root(self.def_map.krate);
132 let raw_items = self.db.raw_items(file_id.into()); 132 let raw_items = self.db.raw_items(file_id.into());
133 let module_id = self.def_map.root; 133 let module_id = self.def_map.root;
134 self.def_map.modules[module_id].definition = Some(file_id); 134 self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
135 ModCollector { 135 ModCollector {
136 def_collector: &mut *self, 136 def_collector: &mut *self,
137 module_id, 137 module_id,
@@ -669,8 +669,7 @@ where
669 let modules = &mut self.def_collector.def_map.modules; 669 let modules = &mut self.def_collector.def_map.modules;
670 let res = modules.alloc(ModuleData::default()); 670 let res = modules.alloc(ModuleData::default());
671 modules[res].parent = Some(self.module_id); 671 modules[res].parent = Some(self.module_id);
672 modules[res].declaration = Some(declaration); 672 modules[res].origin = ModuleOrigin::not_sure_file(definition, declaration);
673 modules[res].definition = definition;
674 modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone(); 673 modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone();
675 modules[self.module_id].children.insert(name.clone(), res); 674 modules[self.module_id].children.insert(name.clone(), res);
676 let resolution = Resolution { 675 let resolution = Resolution {
diff --git a/crates/ra_hir_ty/src/test_db.rs b/crates/ra_hir_ty/src/test_db.rs
index 1dc9793f9..476f6df52 100644
--- a/crates/ra_hir_ty/src/test_db.rs
+++ b/crates/ra_hir_ty/src/test_db.rs
@@ -74,7 +74,7 @@ impl TestDB {
74 for &krate in self.relevant_crates(file_id).iter() { 74 for &krate in self.relevant_crates(file_id).iter() {
75 let crate_def_map = self.crate_def_map(krate); 75 let crate_def_map = self.crate_def_map(krate);
76 for (local_id, data) in crate_def_map.modules.iter() { 76 for (local_id, data) in crate_def_map.modules.iter() {
77 if data.definition == Some(file_id) { 77 if data.origin.file_id() == Some(file_id) {
78 return ModuleId { krate, local_id }; 78 return ModuleId { krate, local_id };
79 } 79 }
80 } 80 }
diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs
index f920d3db6..add11fbc3 100644
--- a/crates/ra_ide/src/display/navigation_target.rs
+++ b/crates/ra_ide/src/display/navigation_target.rs
@@ -231,34 +231,20 @@ impl ToNav for hir::Module {
231 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 231 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
232 let src = self.definition_source(db); 232 let src = self.definition_source(db);
233 let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default(); 233 let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
234 match &src.value { 234 let syntax = match &src.value {
235 ModuleSource::SourceFile(node) => { 235 ModuleSource::SourceFile(node) => node.syntax(),
236 let frange = original_range(db, src.with_value(node.syntax())); 236 ModuleSource::Module(node) => node.syntax(),
237 237 };
238 NavigationTarget::from_syntax( 238 let frange = original_range(db, src.with_value(syntax));
239 frange.file_id, 239 NavigationTarget::from_syntax(
240 name, 240 frange.file_id,
241 None, 241 name,
242 frange.range, 242 None,
243 node.syntax().kind(), 243 frange.range,
244 None, 244 syntax.kind(),
245 None, 245 None,
246 ) 246 None,
247 } 247 )
248 ModuleSource::Module(node) => {
249 let frange = original_range(db, src.with_value(node.syntax()));
250
251 NavigationTarget::from_syntax(
252 frange.file_id,
253 name,
254 None,
255 frange.range,
256 node.syntax().kind(),
257 node.doc_comment_text(),
258 node.short_label(),
259 )
260 }
261 }
262 } 248 }
263} 249}
264 250
diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs
index 616d69fce..aef3fa3df 100644
--- a/crates/ra_ide/src/parent_module.rs
+++ b/crates/ra_ide/src/parent_module.rs
@@ -1,6 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use ra_db::{CrateId, FileId, FilePosition}; 3use ra_db::{CrateId, FileId, FilePosition, SourceDatabase};
4 4
5use crate::{db::RootDatabase, NavigationTarget}; 5use crate::{db::RootDatabase, NavigationTarget};
6 6
@@ -21,7 +21,8 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
21 21
22/// Returns `Vec` for the same reason as `parent_module` 22/// Returns `Vec` for the same reason as `parent_module`
23pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> { 23pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
24 let src = hir::ModuleSource::from_file_id(db, file_id); 24 let source_file = db.parse(file_id).tree();
25 let src = hir::ModuleSource::SourceFile(source_file);
25 let module = 26 let module =
26 match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src }) 27 match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src })
27 { 28 {