diff options
Diffstat (limited to 'crates/ra_ide/src/references')
-rw-r--r-- | crates/ra_ide/src/references/classify.rs | 100 |
1 files changed, 52 insertions, 48 deletions
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index 3483a7176..4a6e11e27 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | //! Functions that are used to classify an element from its definition or reference. | 1 | //! Functions that are used to classify an element from its definition or reference. |
2 | 2 | ||
3 | use hir::{FromSource, InFile, Module, ModuleSource, PathResolution, SourceAnalyzer}; | 3 | use hir::{FromSource, InFile, Module, ModuleSource, PathResolution, SourceBinder}; |
4 | use ra_prof::profile; | 4 | use ra_prof::profile; |
5 | use ra_syntax::{ast, match_ast, AstNode}; | 5 | use ra_syntax::{ast, match_ast, AstNode}; |
6 | use test_utils::tested_by; | 6 | use test_utils::tested_by; |
@@ -11,7 +11,10 @@ use super::{ | |||
11 | }; | 11 | }; |
12 | use crate::db::RootDatabase; | 12 | use crate::db::RootDatabase; |
13 | 13 | ||
14 | pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Option<NameDefinition> { | 14 | pub(crate) fn classify_name( |
15 | sb: &mut SourceBinder<RootDatabase>, | ||
16 | name: InFile<&ast::Name>, | ||
17 | ) -> Option<NameDefinition> { | ||
15 | let _p = profile("classify_name"); | 18 | let _p = profile("classify_name"); |
16 | let parent = name.value.syntax().parent()?; | 19 | let parent = name.value.syntax().parent()?; |
17 | 20 | ||
@@ -19,90 +22,89 @@ pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Opti | |||
19 | match parent { | 22 | match parent { |
20 | ast::BindPat(it) => { | 23 | ast::BindPat(it) => { |
21 | let src = name.with_value(it); | 24 | let src = name.with_value(it); |
22 | let local = hir::Local::from_source(db, src)?; | 25 | let local = hir::Local::from_source(sb.db, src)?; |
23 | Some(NameDefinition { | 26 | Some(NameDefinition { |
24 | visibility: None, | 27 | visibility: None, |
25 | container: local.module(db), | 28 | container: local.module(sb.db), |
26 | kind: NameKind::Local(local), | 29 | kind: NameKind::Local(local), |
27 | }) | 30 | }) |
28 | }, | 31 | }, |
29 | ast::RecordFieldDef(it) => { | 32 | ast::RecordFieldDef(it) => { |
30 | let ast = hir::FieldSource::Named(it); | 33 | let src = name.with_value(it); |
31 | let src = name.with_value(ast); | 34 | let field: hir::StructField = sb.to_def(src)?; |
32 | let field = hir::StructField::from_source(db, src)?; | 35 | Some(from_struct_field(sb.db, field)) |
33 | Some(from_struct_field(db, field)) | ||
34 | }, | 36 | }, |
35 | ast::Module(it) => { | 37 | ast::Module(it) => { |
36 | let def = { | 38 | let def = { |
37 | if !it.has_semi() { | 39 | if !it.has_semi() { |
38 | let ast = hir::ModuleSource::Module(it); | 40 | let ast = hir::ModuleSource::Module(it); |
39 | let src = name.with_value(ast); | 41 | let src = name.with_value(ast); |
40 | hir::Module::from_definition(db, src) | 42 | hir::Module::from_definition(sb.db, src) |
41 | } else { | 43 | } else { |
42 | let src = name.with_value(it); | 44 | let src = name.with_value(it); |
43 | hir::Module::from_declaration(db, src) | 45 | hir::Module::from_declaration(sb.db, src) |
44 | } | 46 | } |
45 | }?; | 47 | }?; |
46 | Some(from_module_def(db, def.into(), None)) | 48 | Some(from_module_def(sb.db, def.into(), None)) |
47 | }, | 49 | }, |
48 | ast::StructDef(it) => { | 50 | ast::StructDef(it) => { |
49 | let src = name.with_value(it); | 51 | let src = name.with_value(it); |
50 | let def = hir::Struct::from_source(db, src)?; | 52 | let def: hir::Struct = sb.to_def(src)?; |
51 | Some(from_module_def(db, def.into(), None)) | 53 | Some(from_module_def(sb.db, def.into(), None)) |
52 | }, | 54 | }, |
53 | ast::EnumDef(it) => { | 55 | ast::EnumDef(it) => { |
54 | let src = name.with_value(it); | 56 | let src = name.with_value(it); |
55 | let def = hir::Enum::from_source(db, src)?; | 57 | let def: hir::Enum = sb.to_def(src)?; |
56 | Some(from_module_def(db, def.into(), None)) | 58 | Some(from_module_def(sb.db, def.into(), None)) |
57 | }, | 59 | }, |
58 | ast::TraitDef(it) => { | 60 | ast::TraitDef(it) => { |
59 | let src = name.with_value(it); | 61 | let src = name.with_value(it); |
60 | let def = hir::Trait::from_source(db, src)?; | 62 | let def: hir::Trait = sb.to_def(src)?; |
61 | Some(from_module_def(db, def.into(), None)) | 63 | Some(from_module_def(sb.db, def.into(), None)) |
62 | }, | 64 | }, |
63 | ast::StaticDef(it) => { | 65 | ast::StaticDef(it) => { |
64 | let src = name.with_value(it); | 66 | let src = name.with_value(it); |
65 | let def = hir::Static::from_source(db, src)?; | 67 | let def: hir::Static = sb.to_def(src)?; |
66 | Some(from_module_def(db, def.into(), None)) | 68 | Some(from_module_def(sb.db, def.into(), None)) |
67 | }, | 69 | }, |
68 | ast::EnumVariant(it) => { | 70 | ast::EnumVariant(it) => { |
69 | let src = name.with_value(it); | 71 | let src = name.with_value(it); |
70 | let def = hir::EnumVariant::from_source(db, src)?; | 72 | let def: hir::EnumVariant = sb.to_def(src)?; |
71 | Some(from_module_def(db, def.into(), None)) | 73 | Some(from_module_def(sb.db, def.into(), None)) |
72 | }, | 74 | }, |
73 | ast::FnDef(it) => { | 75 | ast::FnDef(it) => { |
74 | let src = name.with_value(it); | 76 | let src = name.with_value(it); |
75 | let def = hir::Function::from_source(db, src)?; | 77 | let def: hir::Function = sb.to_def(src)?; |
76 | if parent.parent().and_then(ast::ItemList::cast).is_some() { | 78 | if parent.parent().and_then(ast::ItemList::cast).is_some() { |
77 | Some(from_assoc_item(db, def.into())) | 79 | Some(from_assoc_item(sb.db, def.into())) |
78 | } else { | 80 | } else { |
79 | Some(from_module_def(db, def.into(), None)) | 81 | Some(from_module_def(sb.db, def.into(), None)) |
80 | } | 82 | } |
81 | }, | 83 | }, |
82 | ast::ConstDef(it) => { | 84 | ast::ConstDef(it) => { |
83 | let src = name.with_value(it); | 85 | let src = name.with_value(it); |
84 | let def = hir::Const::from_source(db, src)?; | 86 | let def: hir::Const = sb.to_def(src)?; |
85 | if parent.parent().and_then(ast::ItemList::cast).is_some() { | 87 | if parent.parent().and_then(ast::ItemList::cast).is_some() { |
86 | Some(from_assoc_item(db, def.into())) | 88 | Some(from_assoc_item(sb.db, def.into())) |
87 | } else { | 89 | } else { |
88 | Some(from_module_def(db, def.into(), None)) | 90 | Some(from_module_def(sb.db, def.into(), None)) |
89 | } | 91 | } |
90 | }, | 92 | }, |
91 | ast::TypeAliasDef(it) => { | 93 | ast::TypeAliasDef(it) => { |
92 | let src = name.with_value(it); | 94 | let src = name.with_value(it); |
93 | let def = hir::TypeAlias::from_source(db, src)?; | 95 | let def: hir::TypeAlias = sb.to_def(src)?; |
94 | if parent.parent().and_then(ast::ItemList::cast).is_some() { | 96 | if parent.parent().and_then(ast::ItemList::cast).is_some() { |
95 | Some(from_assoc_item(db, def.into())) | 97 | Some(from_assoc_item(sb.db, def.into())) |
96 | } else { | 98 | } else { |
97 | Some(from_module_def(db, def.into(), None)) | 99 | Some(from_module_def(sb.db, def.into(), None)) |
98 | } | 100 | } |
99 | }, | 101 | }, |
100 | ast::MacroCall(it) => { | 102 | ast::MacroCall(it) => { |
101 | let src = name.with_value(it); | 103 | let src = name.with_value(it); |
102 | let def = hir::MacroDef::from_source(db, src.clone())?; | 104 | let def = hir::MacroDef::from_source(sb.db, src.clone())?; |
103 | 105 | ||
104 | let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); | 106 | let module_src = ModuleSource::from_child_node(sb.db, src.as_ref().map(|it| it.syntax())); |
105 | let module = Module::from_definition(db, src.with_value(module_src))?; | 107 | let module = Module::from_definition(sb.db, src.with_value(module_src))?; |
106 | 108 | ||
107 | Some(NameDefinition { | 109 | Some(NameDefinition { |
108 | visibility: None, | 110 | visibility: None, |
@@ -112,10 +114,10 @@ pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Opti | |||
112 | }, | 114 | }, |
113 | ast::TypeParam(it) => { | 115 | ast::TypeParam(it) => { |
114 | let src = name.with_value(it); | 116 | let src = name.with_value(it); |
115 | let def = hir::TypeParam::from_source(db, src)?; | 117 | let def = hir::TypeParam::from_source(sb.db, src)?; |
116 | Some(NameDefinition { | 118 | Some(NameDefinition { |
117 | visibility: None, | 119 | visibility: None, |
118 | container: def.module(db), | 120 | container: def.module(sb.db), |
119 | kind: NameKind::TypeParam(def), | 121 | kind: NameKind::TypeParam(def), |
120 | }) | 122 | }) |
121 | }, | 123 | }, |
@@ -125,25 +127,25 @@ pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Opti | |||
125 | } | 127 | } |
126 | 128 | ||
127 | pub(crate) fn classify_name_ref( | 129 | pub(crate) fn classify_name_ref( |
128 | db: &RootDatabase, | 130 | sb: &mut SourceBinder<RootDatabase>, |
129 | name_ref: InFile<&ast::NameRef>, | 131 | name_ref: InFile<&ast::NameRef>, |
130 | ) -> Option<NameDefinition> { | 132 | ) -> Option<NameDefinition> { |
131 | let _p = profile("classify_name_ref"); | 133 | let _p = profile("classify_name_ref"); |
132 | 134 | ||
133 | let parent = name_ref.value.syntax().parent()?; | 135 | let parent = name_ref.value.syntax().parent()?; |
134 | let analyzer = SourceAnalyzer::new(db, name_ref.map(|it| it.syntax()), None); | 136 | let analyzer = sb.analyze(name_ref.map(|it| it.syntax()), None); |
135 | 137 | ||
136 | if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { | 138 | if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { |
137 | tested_by!(goto_def_for_methods); | 139 | tested_by!(goto_def_for_methods); |
138 | if let Some(func) = analyzer.resolve_method_call(&method_call) { | 140 | if let Some(func) = analyzer.resolve_method_call(&method_call) { |
139 | return Some(from_assoc_item(db, func.into())); | 141 | return Some(from_assoc_item(sb.db, func.into())); |
140 | } | 142 | } |
141 | } | 143 | } |
142 | 144 | ||
143 | if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { | 145 | if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { |
144 | tested_by!(goto_def_for_fields); | 146 | tested_by!(goto_def_for_fields); |
145 | if let Some(field) = analyzer.resolve_field(&field_expr) { | 147 | if let Some(field) = analyzer.resolve_field(&field_expr) { |
146 | return Some(from_struct_field(db, field)); | 148 | return Some(from_struct_field(sb.db, field)); |
147 | } | 149 | } |
148 | } | 150 | } |
149 | 151 | ||
@@ -151,30 +153,32 @@ pub(crate) fn classify_name_ref( | |||
151 | tested_by!(goto_def_for_record_fields); | 153 | tested_by!(goto_def_for_record_fields); |
152 | tested_by!(goto_def_for_field_init_shorthand); | 154 | tested_by!(goto_def_for_field_init_shorthand); |
153 | if let Some(field_def) = analyzer.resolve_record_field(&record_field) { | 155 | if let Some(field_def) = analyzer.resolve_record_field(&record_field) { |
154 | return Some(from_struct_field(db, field_def)); | 156 | return Some(from_struct_field(sb.db, field_def)); |
155 | } | 157 | } |
156 | } | 158 | } |
157 | 159 | ||
158 | let ast = ModuleSource::from_child_node(db, name_ref.with_value(&parent)); | 160 | let ast = ModuleSource::from_child_node(sb.db, name_ref.with_value(&parent)); |
159 | // FIXME: find correct container and visibility for each case | 161 | // FIXME: find correct container and visibility for each case |
160 | let container = Module::from_definition(db, name_ref.with_value(ast))?; | 162 | let container = Module::from_definition(sb.db, name_ref.with_value(ast))?; |
161 | let visibility = None; | 163 | let visibility = None; |
162 | 164 | ||
163 | if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { | 165 | if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { |
164 | tested_by!(goto_def_for_macros); | 166 | tested_by!(goto_def_for_macros); |
165 | if let Some(macro_def) = analyzer.resolve_macro_call(db, name_ref.with_value(¯o_call)) { | 167 | if let Some(macro_def) = |
168 | analyzer.resolve_macro_call(sb.db, name_ref.with_value(¯o_call)) | ||
169 | { | ||
166 | let kind = NameKind::Macro(macro_def); | 170 | let kind = NameKind::Macro(macro_def); |
167 | return Some(NameDefinition { kind, container, visibility }); | 171 | return Some(NameDefinition { kind, container, visibility }); |
168 | } | 172 | } |
169 | } | 173 | } |
170 | 174 | ||
171 | let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?; | 175 | let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?; |
172 | let resolved = analyzer.resolve_path(db, &path)?; | 176 | let resolved = analyzer.resolve_path(sb.db, &path)?; |
173 | match resolved { | 177 | match resolved { |
174 | PathResolution::Def(def) => Some(from_module_def(db, def, Some(container))), | 178 | PathResolution::Def(def) => Some(from_module_def(sb.db, def, Some(container))), |
175 | PathResolution::AssocItem(item) => Some(from_assoc_item(db, item)), | 179 | PathResolution::AssocItem(item) => Some(from_assoc_item(sb.db, item)), |
176 | PathResolution::Local(local) => { | 180 | PathResolution::Local(local) => { |
177 | let container = local.module(db); | 181 | let container = local.module(sb.db); |
178 | let kind = NameKind::Local(local); | 182 | let kind = NameKind::Local(local); |
179 | Some(NameDefinition { kind, container, visibility: None }) | 183 | Some(NameDefinition { kind, container, visibility: None }) |
180 | } | 184 | } |
@@ -188,7 +192,7 @@ pub(crate) fn classify_name_ref( | |||
188 | } | 192 | } |
189 | PathResolution::SelfType(impl_block) => { | 193 | PathResolution::SelfType(impl_block) => { |
190 | let kind = NameKind::SelfType(impl_block); | 194 | let kind = NameKind::SelfType(impl_block); |
191 | let container = impl_block.module(db); | 195 | let container = impl_block.module(sb.db); |
192 | Some(NameDefinition { kind, container, visibility }) | 196 | Some(NameDefinition { kind, container, visibility }) |
193 | } | 197 | } |
194 | } | 198 | } |