diff options
Diffstat (limited to 'crates/ra_ide/src/references')
-rw-r--r-- | crates/ra_ide/src/references/classify.rs | 42 | ||||
-rw-r--r-- | crates/ra_ide/src/references/search_scope.rs | 21 |
2 files changed, 25 insertions, 38 deletions
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index d0f03d8a8..478e18871 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs | |||
@@ -5,7 +5,7 @@ use ra_prof::profile; | |||
5 | use ra_syntax::{ast, AstNode}; | 5 | use ra_syntax::{ast, AstNode}; |
6 | use test_utils::tested_by; | 6 | use test_utils::tested_by; |
7 | 7 | ||
8 | use super::{NameDefinition, NameKind}; | 8 | use super::NameDefinition; |
9 | use ra_ide_db::RootDatabase; | 9 | use ra_ide_db::RootDatabase; |
10 | 10 | ||
11 | pub use ra_ide_db::defs::{classify_name, from_module_def, from_struct_field}; | 11 | pub use ra_ide_db::defs::{classify_name, from_module_def, from_struct_field}; |
@@ -22,14 +22,14 @@ pub(crate) fn classify_name_ref( | |||
22 | if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { | 22 | if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { |
23 | tested_by!(goto_def_for_methods); | 23 | tested_by!(goto_def_for_methods); |
24 | if let Some(func) = analyzer.resolve_method_call(&method_call) { | 24 | if let Some(func) = analyzer.resolve_method_call(&method_call) { |
25 | return Some(from_module_def(sb.db, func.into(), None)); | 25 | return Some(from_module_def(func.into())); |
26 | } | 26 | } |
27 | } | 27 | } |
28 | 28 | ||
29 | if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { | 29 | if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { |
30 | tested_by!(goto_def_for_fields); | 30 | tested_by!(goto_def_for_fields); |
31 | if let Some(field) = analyzer.resolve_field(&field_expr) { | 31 | if let Some(field) = analyzer.resolve_field(&field_expr) { |
32 | return Some(from_struct_field(sb.db, field)); | 32 | return Some(from_struct_field(field)); |
33 | } | 33 | } |
34 | } | 34 | } |
35 | 35 | ||
@@ -37,55 +37,35 @@ pub(crate) fn classify_name_ref( | |||
37 | tested_by!(goto_def_for_record_fields); | 37 | tested_by!(goto_def_for_record_fields); |
38 | tested_by!(goto_def_for_field_init_shorthand); | 38 | tested_by!(goto_def_for_field_init_shorthand); |
39 | if let Some(field_def) = analyzer.resolve_record_field(&record_field) { | 39 | if let Some(field_def) = analyzer.resolve_record_field(&record_field) { |
40 | return Some(from_struct_field(sb.db, field_def)); | 40 | return Some(from_struct_field(field_def)); |
41 | } | 41 | } |
42 | } | 42 | } |
43 | 43 | ||
44 | // FIXME: find correct container and visibility for each case | ||
45 | let visibility = None; | ||
46 | let container = sb.to_module_def(name_ref.file_id.original_file(sb.db))?; | ||
47 | |||
48 | if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { | 44 | if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { |
49 | tested_by!(goto_def_for_macros); | 45 | tested_by!(goto_def_for_macros); |
50 | if let Some(macro_def) = | 46 | if let Some(macro_def) = |
51 | analyzer.resolve_macro_call(sb.db, name_ref.with_value(¯o_call)) | 47 | analyzer.resolve_macro_call(sb.db, name_ref.with_value(¯o_call)) |
52 | { | 48 | { |
53 | let kind = NameKind::Macro(macro_def); | 49 | return Some(NameDefinition::Macro(macro_def)); |
54 | return Some(NameDefinition { kind, container, visibility }); | ||
55 | } | 50 | } |
56 | } | 51 | } |
57 | 52 | ||
58 | let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?; | 53 | let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?; |
59 | let resolved = analyzer.resolve_path(sb.db, &path)?; | 54 | let resolved = analyzer.resolve_path(sb.db, &path)?; |
60 | let res = match resolved { | 55 | let res = match resolved { |
61 | PathResolution::Def(def) => from_module_def(sb.db, def, Some(container)), | 56 | PathResolution::Def(def) => from_module_def(def), |
62 | PathResolution::AssocItem(item) => { | 57 | PathResolution::AssocItem(item) => { |
63 | let def = match item { | 58 | let def = match item { |
64 | hir::AssocItem::Function(it) => it.into(), | 59 | hir::AssocItem::Function(it) => it.into(), |
65 | hir::AssocItem::Const(it) => it.into(), | 60 | hir::AssocItem::Const(it) => it.into(), |
66 | hir::AssocItem::TypeAlias(it) => it.into(), | 61 | hir::AssocItem::TypeAlias(it) => it.into(), |
67 | }; | 62 | }; |
68 | from_module_def(sb.db, def, Some(container)) | 63 | from_module_def(def) |
69 | } | ||
70 | PathResolution::Local(local) => { | ||
71 | let kind = NameKind::Local(local); | ||
72 | let container = local.module(sb.db); | ||
73 | NameDefinition { kind, container, visibility: None } | ||
74 | } | ||
75 | PathResolution::TypeParam(par) => { | ||
76 | let kind = NameKind::TypeParam(par); | ||
77 | let container = par.module(sb.db); | ||
78 | NameDefinition { kind, container, visibility } | ||
79 | } | ||
80 | PathResolution::Macro(def) => { | ||
81 | let kind = NameKind::Macro(def); | ||
82 | NameDefinition { kind, container, visibility } | ||
83 | } | ||
84 | PathResolution::SelfType(impl_block) => { | ||
85 | let kind = NameKind::SelfType(impl_block); | ||
86 | let container = impl_block.module(sb.db); | ||
87 | NameDefinition { kind, container, visibility } | ||
88 | } | 64 | } |
65 | PathResolution::Local(local) => NameDefinition::Local(local), | ||
66 | PathResolution::TypeParam(par) => NameDefinition::TypeParam(par), | ||
67 | PathResolution::Macro(def) => NameDefinition::Macro(def), | ||
68 | PathResolution::SelfType(impl_block) => NameDefinition::SelfType(impl_block), | ||
89 | }; | 69 | }; |
90 | Some(res) | 70 | Some(res) |
91 | } | 71 | } |
diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs index 279f57be0..27d483233 100644 --- a/crates/ra_ide/src/references/search_scope.rs +++ b/crates/ra_ide/src/references/search_scope.rs | |||
@@ -12,20 +12,27 @@ use rustc_hash::FxHashMap; | |||
12 | 12 | ||
13 | use ra_ide_db::RootDatabase; | 13 | use ra_ide_db::RootDatabase; |
14 | 14 | ||
15 | use super::{NameDefinition, NameKind}; | 15 | use super::NameDefinition; |
16 | 16 | ||
17 | pub struct SearchScope { | 17 | pub struct SearchScope { |
18 | entries: FxHashMap<FileId, Option<TextRange>>, | 18 | entries: FxHashMap<FileId, Option<TextRange>>, |
19 | } | 19 | } |
20 | 20 | ||
21 | impl SearchScope { | 21 | impl SearchScope { |
22 | fn empty() -> SearchScope { | ||
23 | SearchScope { entries: FxHashMap::default() } | ||
24 | } | ||
25 | |||
22 | pub(crate) fn for_def(def: &NameDefinition, db: &RootDatabase) -> SearchScope { | 26 | pub(crate) fn for_def(def: &NameDefinition, db: &RootDatabase) -> SearchScope { |
23 | let _p = profile("search_scope"); | 27 | let _p = profile("search_scope"); |
24 | 28 | let module = match def.module(db) { | |
25 | let module_src = def.container.definition_source(db); | 29 | Some(it) => it, |
30 | None => return SearchScope::empty(), | ||
31 | }; | ||
32 | let module_src = module.definition_source(db); | ||
26 | let file_id = module_src.file_id.original_file(db); | 33 | let file_id = module_src.file_id.original_file(db); |
27 | 34 | ||
28 | if let NameKind::Local(var) = def.kind { | 35 | if let NameDefinition::Local(var) = def { |
29 | let range = match var.parent(db) { | 36 | let range = match var.parent(db) { |
30 | DefWithBody::Function(f) => f.source(db).value.syntax().text_range(), | 37 | DefWithBody::Function(f) => f.source(db).value.syntax().text_range(), |
31 | DefWithBody::Const(c) => c.source(db).value.syntax().text_range(), | 38 | DefWithBody::Const(c) => c.source(db).value.syntax().text_range(), |
@@ -36,10 +43,10 @@ impl SearchScope { | |||
36 | return SearchScope::new(res); | 43 | return SearchScope::new(res); |
37 | } | 44 | } |
38 | 45 | ||
39 | let vis = def.visibility.as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); | 46 | let vis = def.visibility(db).as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); |
40 | 47 | ||
41 | if vis.as_str() == "pub(super)" { | 48 | if vis.as_str() == "pub(super)" { |
42 | if let Some(parent_module) = def.container.parent(db) { | 49 | if let Some(parent_module) = module.parent(db) { |
43 | let mut res = FxHashMap::default(); | 50 | let mut res = FxHashMap::default(); |
44 | let parent_src = parent_module.definition_source(db); | 51 | let parent_src = parent_module.definition_source(db); |
45 | let file_id = parent_src.file_id.original_file(db); | 52 | let file_id = parent_src.file_id.original_file(db); |
@@ -72,7 +79,7 @@ impl SearchScope { | |||
72 | return SearchScope::new(res); | 79 | return SearchScope::new(res); |
73 | } | 80 | } |
74 | if vis.as_str() == "pub" { | 81 | if vis.as_str() == "pub" { |
75 | let krate = def.container.krate(); | 82 | let krate = module.krate(); |
76 | for rev_dep in krate.reverse_dependencies(db) { | 83 | for rev_dep in krate.reverse_dependencies(db) { |
77 | let root_file = rev_dep.root_file(db); | 84 | let root_file = rev_dep.root_file(db); |
78 | let source_root_id = db.file_source_root(root_file); | 85 | let source_root_id = db.file_source_root(root_file); |