diff options
Diffstat (limited to 'crates/ra_hir/src/from_source.rs')
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 93 |
1 files changed, 59 insertions, 34 deletions
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 7abb4bd75..6314be8d4 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs | |||
@@ -1,24 +1,28 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! Finds a corresponding hir data structure for a syntax node in a specific |
2 | //! file. | ||
3 | |||
2 | use hir_def::{ | 4 | use hir_def::{ |
3 | child_by_source::ChildBySource, dyn_map::DynMap, keys, keys::Key, nameres::ModuleSource, | 5 | child_by_source::ChildBySource, dyn_map::DynMap, keys, keys::Key, nameres::ModuleSource, |
4 | ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, StaticId, StructId, | 6 | ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, |
5 | TraitId, TypeAliasId, UnionId, VariantId, | 7 | StaticId, StructId, TraitId, TypeAliasId, UnionId, VariantId, |
6 | }; | 8 | }; |
7 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; | 9 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; |
10 | use ra_db::FileId; | ||
11 | use ra_prof::profile; | ||
8 | use ra_syntax::{ | 12 | use ra_syntax::{ |
9 | ast::{self, AstNode, NameOwner}, | 13 | ast::{self, AstNode, NameOwner}, |
10 | match_ast, SyntaxNode, | 14 | match_ast, SyntaxNode, |
11 | }; | 15 | }; |
12 | 16 | ||
13 | use crate::{ | 17 | use crate::{ |
14 | db::{AstDatabase, DefDatabase, HirDatabase}, | 18 | db::{DefDatabase, HirDatabase}, |
15 | Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, | 19 | Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, |
16 | MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, | 20 | MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, |
17 | }; | 21 | }; |
18 | 22 | ||
19 | pub trait FromSource: Sized { | 23 | pub trait FromSource: Sized { |
20 | type Ast; | 24 | type Ast; |
21 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self>; | 25 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self>; |
22 | } | 26 | } |
23 | 27 | ||
24 | pub trait FromSourceByContainer: Sized { | 28 | pub trait FromSourceByContainer: Sized { |
@@ -32,7 +36,7 @@ where | |||
32 | T: From<<T as FromSourceByContainer>::Id>, | 36 | T: From<<T as FromSourceByContainer>::Id>, |
33 | { | 37 | { |
34 | type Ast = <T as FromSourceByContainer>::Ast; | 38 | type Ast = <T as FromSourceByContainer>::Ast; |
35 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { | 39 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> { |
36 | analyze_container(db, src.as_ref().map(|it| it.syntax()))[T::KEY] | 40 | analyze_container(db, src.as_ref().map(|it| it.syntax()))[T::KEY] |
37 | .get(&src) | 41 | .get(&src) |
38 | .copied() | 42 | .copied() |
@@ -64,7 +68,7 @@ from_source_by_container_impls![ | |||
64 | 68 | ||
65 | impl FromSource for MacroDef { | 69 | impl FromSource for MacroDef { |
66 | type Ast = ast::MacroCall; | 70 | type Ast = ast::MacroCall; |
67 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { | 71 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> { |
68 | let kind = MacroDefKind::Declarative; | 72 | let kind = MacroDefKind::Declarative; |
69 | 73 | ||
70 | let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); | 74 | let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); |
@@ -80,7 +84,7 @@ impl FromSource for MacroDef { | |||
80 | 84 | ||
81 | impl FromSource for EnumVariant { | 85 | impl FromSource for EnumVariant { |
82 | type Ast = ast::EnumVariant; | 86 | type Ast = ast::EnumVariant; |
83 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { | 87 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> { |
84 | let parent_enum = src.value.parent_enum(); | 88 | let parent_enum = src.value.parent_enum(); |
85 | let src_enum = InFile { file_id: src.file_id, value: parent_enum }; | 89 | let src_enum = InFile { file_id: src.file_id, value: parent_enum }; |
86 | let parent_enum = Enum::from_source(db, src_enum)?; | 90 | let parent_enum = Enum::from_source(db, src_enum)?; |
@@ -93,7 +97,7 @@ impl FromSource for EnumVariant { | |||
93 | 97 | ||
94 | impl FromSource for StructField { | 98 | impl FromSource for StructField { |
95 | type Ast = FieldSource; | 99 | type Ast = FieldSource; |
96 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { | 100 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> { |
97 | let src = src.as_ref(); | 101 | let src = src.as_ref(); |
98 | 102 | ||
99 | // FIXME this is buggy | 103 | // FIXME this is buggy |
@@ -167,6 +171,7 @@ impl TypeParam { | |||
167 | 171 | ||
168 | impl Module { | 172 | impl Module { |
169 | pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> { | 173 | pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> { |
174 | let _p = profile("Module::from_declaration"); | ||
170 | let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); | 175 | let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); |
171 | 176 | ||
172 | let parent_module = match parent_declaration { | 177 | let parent_module = match parent_declaration { |
@@ -189,6 +194,7 @@ impl Module { | |||
189 | } | 194 | } |
190 | 195 | ||
191 | pub fn from_definition(db: &impl DefDatabase, src: InFile<ModuleSource>) -> Option<Self> { | 196 | pub fn from_definition(db: &impl DefDatabase, src: InFile<ModuleSource>) -> Option<Self> { |
197 | let _p = profile("Module::from_definition"); | ||
192 | match src.value { | 198 | match src.value { |
193 | ModuleSource::Module(ref module) => { | 199 | ModuleSource::Module(ref module) => { |
194 | assert!(!module.has_semi()); | 200 | assert!(!module.has_semi()); |
@@ -201,10 +207,14 @@ impl Module { | |||
201 | }; | 207 | }; |
202 | 208 | ||
203 | let original_file = src.file_id.original_file(db); | 209 | let original_file = src.file_id.original_file(db); |
210 | Module::from_file(db, original_file) | ||
211 | } | ||
204 | 212 | ||
205 | let (krate, local_id) = db.relevant_crates(original_file).iter().find_map(|&crate_id| { | 213 | fn from_file(db: &impl DefDatabase, file: FileId) -> Option<Self> { |
214 | let _p = profile("Module::from_file"); | ||
215 | let (krate, local_id) = db.relevant_crates(file).iter().find_map(|&crate_id| { | ||
206 | let crate_def_map = db.crate_def_map(crate_id); | 216 | let crate_def_map = db.crate_def_map(crate_id); |
207 | let local_id = crate_def_map.modules_for_file(original_file).next()?; | 217 | let local_id = crate_def_map.modules_for_file(file).next()?; |
208 | Some((crate_id, local_id)) | 218 | Some((crate_id, local_id)) |
209 | })?; | 219 | })?; |
210 | Some(Module { id: ModuleId { krate, local_id } }) | 220 | Some(Module { id: ModuleId { krate, local_id } }) |
@@ -212,29 +222,44 @@ impl Module { | |||
212 | } | 222 | } |
213 | 223 | ||
214 | fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap { | 224 | fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap { |
215 | _analyze_container(db, src).unwrap_or_default() | 225 | let _p = profile("analyze_container"); |
216 | } | 226 | return child_by_source(db, src).unwrap_or_default(); |
217 | 227 | ||
218 | fn _analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<DynMap> { | 228 | fn child_by_source(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<DynMap> { |
219 | // FIXME: this doesn't try to handle nested declarations | 229 | for container in src.value.ancestors().skip(1) { |
220 | for container in src.value.ancestors().skip(1) { | 230 | let res = match_ast! { |
221 | let res = match_ast! { | 231 | match container { |
222 | match container { | 232 | ast::TraitDef(it) => { |
223 | ast::TraitDef(it) => { | 233 | let def = Trait::from_source(db, src.with_value(it))?; |
224 | let c = Trait::from_source(db, src.with_value(it))?; | 234 | def.id.child_by_source(db) |
225 | c.id.child_by_source(db) | 235 | }, |
226 | }, | 236 | ast::ImplBlock(it) => { |
227 | ast::ImplBlock(it) => { | 237 | let def = ImplBlock::from_source(db, src.with_value(it))?; |
228 | let c = ImplBlock::from_source(db, src.with_value(it))?; | 238 | def.id.child_by_source(db) |
229 | c.id.child_by_source(db) | 239 | }, |
230 | }, | 240 | ast::FnDef(it) => { |
231 | _ => { continue }, | 241 | let def = Function::from_source(db, src.with_value(it))?; |
232 | } | 242 | DefWithBodyId::from(def.id) |
233 | }; | 243 | .child_by_source(db) |
234 | return Some(res); | 244 | }, |
235 | } | 245 | ast::StaticDef(it) => { |
246 | let def = Static::from_source(db, src.with_value(it))?; | ||
247 | DefWithBodyId::from(def.id) | ||
248 | .child_by_source(db) | ||
249 | }, | ||
250 | ast::ConstDef(it) => { | ||
251 | let def = Const::from_source(db, src.with_value(it))?; | ||
252 | DefWithBodyId::from(def.id) | ||
253 | .child_by_source(db) | ||
254 | }, | ||
255 | _ => { continue }, | ||
256 | } | ||
257 | }; | ||
258 | return Some(res); | ||
259 | } | ||
236 | 260 | ||
237 | let module_source = ModuleSource::from_child_node(db, src); | 261 | let module_source = ModuleSource::from_child_node(db, src); |
238 | let c = Module::from_definition(db, src.with_value(module_source))?; | 262 | let c = Module::from_definition(db, src.with_value(module_source))?; |
239 | Some(c.id.child_by_source(db)) | 263 | Some(c.id.child_by_source(db)) |
264 | } | ||
240 | } | 265 | } |