diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-09-19 19:18:41 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-09-19 19:18:41 +0100 |
commit | cd9b222ba0555424d73d549eac43d9aaf1765d7d (patch) | |
tree | 88eea12084b65ba7b0f808cc2c626c7b57c7ac53 /crates/ra_ide_api/src/impls.rs | |
parent | 44bab3621d74f1aec922300a3b3f4476907ba2ac (diff) | |
parent | 2867c40925e8f7b440ff50a421a2d3726b9ff334 (diff) |
Merge #1853
1853: Introduce FromSource trait r=matklad a=viorina
The idea is to provide an ability to get HIR from AST in a more general way than it's possible using `source_binder`.
It also could help with #1622 fixing.
Co-authored-by: Ekaterina Babshukova <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/impls.rs')
-rw-r--r-- | crates/ra_ide_api/src/impls.rs | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs index c5620dd52..f57f9a21b 100644 --- a/crates/ra_ide_api/src/impls.rs +++ b/crates/ra_ide_api/src/impls.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use hir::{db::HirDatabase, source_binder, ApplicationTy, Ty, TypeCtor}; | 1 | use hir::{db::HirDatabase, ApplicationTy, FromSource, Ty, TypeCtor}; |
2 | use ra_db::SourceDatabase; | 2 | use ra_db::SourceDatabase; |
3 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; | 3 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; |
4 | 4 | ||
@@ -11,17 +11,21 @@ pub(crate) fn goto_implementation( | |||
11 | let parse = db.parse(position.file_id); | 11 | let parse = db.parse(position.file_id); |
12 | let syntax = parse.tree().syntax().clone(); | 12 | let syntax = parse.tree().syntax().clone(); |
13 | 13 | ||
14 | let module = source_binder::module_from_position(db, position)?; | 14 | let src = hir::ModuleSource::from_position(db, position); |
15 | let module = hir::Module::from_definition( | ||
16 | db, | ||
17 | hir::Source { file_id: position.file_id.into(), ast: src }, | ||
18 | )?; | ||
15 | 19 | ||
16 | if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&syntax, position.offset) { | 20 | if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&syntax, position.offset) { |
17 | return Some(RangeInfo::new( | 21 | return Some(RangeInfo::new( |
18 | nominal_def.syntax().text_range(), | 22 | nominal_def.syntax().text_range(), |
19 | impls_for_def(db, &nominal_def, module)?, | 23 | impls_for_def(db, position, &nominal_def, module)?, |
20 | )); | 24 | )); |
21 | } else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) { | 25 | } else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) { |
22 | return Some(RangeInfo::new( | 26 | return Some(RangeInfo::new( |
23 | trait_def.syntax().text_range(), | 27 | trait_def.syntax().text_range(), |
24 | impls_for_trait(db, &trait_def, module)?, | 28 | impls_for_trait(db, position, &trait_def, module)?, |
25 | )); | 29 | )); |
26 | } | 30 | } |
27 | 31 | ||
@@ -30,14 +34,19 @@ pub(crate) fn goto_implementation( | |||
30 | 34 | ||
31 | fn impls_for_def( | 35 | fn impls_for_def( |
32 | db: &RootDatabase, | 36 | db: &RootDatabase, |
37 | position: FilePosition, | ||
33 | node: &ast::NominalDef, | 38 | node: &ast::NominalDef, |
34 | module: hir::Module, | 39 | module: hir::Module, |
35 | ) -> Option<Vec<NavigationTarget>> { | 40 | ) -> Option<Vec<NavigationTarget>> { |
36 | let ty = match node { | 41 | let ty = match node { |
37 | ast::NominalDef::StructDef(def) => { | 42 | ast::NominalDef::StructDef(def) => { |
38 | source_binder::struct_from_module(db, module, &def).ty(db) | 43 | let src = hir::Source { file_id: position.file_id.into(), ast: def.clone() }; |
44 | hir::Struct::from_source(db, src)?.ty(db) | ||
45 | } | ||
46 | ast::NominalDef::EnumDef(def) => { | ||
47 | let src = hir::Source { file_id: position.file_id.into(), ast: def.clone() }; | ||
48 | hir::Enum::from_source(db, src)?.ty(db) | ||
39 | } | 49 | } |
40 | ast::NominalDef::EnumDef(def) => source_binder::enum_from_module(db, module, &def).ty(db), | ||
41 | }; | 50 | }; |
42 | 51 | ||
43 | let krate = module.krate(db)?; | 52 | let krate = module.krate(db)?; |
@@ -54,10 +63,12 @@ fn impls_for_def( | |||
54 | 63 | ||
55 | fn impls_for_trait( | 64 | fn impls_for_trait( |
56 | db: &RootDatabase, | 65 | db: &RootDatabase, |
66 | position: FilePosition, | ||
57 | node: &ast::TraitDef, | 67 | node: &ast::TraitDef, |
58 | module: hir::Module, | 68 | module: hir::Module, |
59 | ) -> Option<Vec<NavigationTarget>> { | 69 | ) -> Option<Vec<NavigationTarget>> { |
60 | let tr = source_binder::trait_from_module(db, module, node); | 70 | let src = hir::Source { file_id: position.file_id.into(), ast: node.clone() }; |
71 | let tr = hir::Trait::from_source(db, src)?; | ||
61 | 72 | ||
62 | let krate = module.krate(db)?; | 73 | let krate = module.krate(db)?; |
63 | let impls = db.impls_in_crate(krate); | 74 | let impls = db.impls_in_crate(krate); |