aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-03 17:36:39 +0000
committerAleksey Kladov <[email protected]>2020-03-03 17:40:27 +0000
commitd49a4d1863c30e2dc6e744bf1f1dc92411912bb5 (patch)
tree66a368988b4f3e698c6ec71309ffb4155a725331
parent13b25d73b56ede36d1680efc19f5c11b0669b96c (diff)
Rename NameDefinition -> Definition
-rw-r--r--crates/ra_ide/src/display/navigation_target.rs16
-rw-r--r--crates/ra_ide/src/hover.rs14
-rw-r--r--crates/ra_ide/src/references.rs20
-rw-r--r--crates/ra_ide/src/references/classify.rs36
-rw-r--r--crates/ra_ide/src/references/search_scope.rs6
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs24
-rw-r--r--crates/ra_ide_db/src/defs.rs87
-rw-r--r--crates/ra_ide_db/src/imports_locator.rs6
8 files changed, 103 insertions, 106 deletions
diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs
index 4d3dd477e..c9c14561a 100644
--- a/crates/ra_ide/src/display/navigation_target.rs
+++ b/crates/ra_ide/src/display/navigation_target.rs
@@ -13,7 +13,7 @@ use ra_syntax::{
13 13
14use crate::{ 14use crate::{
15 // expand::original_range, 15 // expand::original_range,
16 references::NameDefinition, 16 references::Definition,
17 FileSymbol, 17 FileSymbol,
18}; 18};
19 19
@@ -189,15 +189,15 @@ impl ToNav for FileSymbol {
189 } 189 }
190} 190}
191 191
192impl TryToNav for NameDefinition { 192impl TryToNav for Definition {
193 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { 193 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
194 match self { 194 match self {
195 NameDefinition::Macro(it) => Some(it.to_nav(db)), 195 Definition::Macro(it) => Some(it.to_nav(db)),
196 NameDefinition::StructField(it) => Some(it.to_nav(db)), 196 Definition::StructField(it) => Some(it.to_nav(db)),
197 NameDefinition::ModuleDef(it) => it.try_to_nav(db), 197 Definition::ModuleDef(it) => it.try_to_nav(db),
198 NameDefinition::SelfType(it) => Some(it.to_nav(db)), 198 Definition::SelfType(it) => Some(it.to_nav(db)),
199 NameDefinition::Local(it) => Some(it.to_nav(db)), 199 Definition::Local(it) => Some(it.to_nav(db)),
200 NameDefinition::TypeParam(it) => Some(it.to_nav(db)), 200 Definition::TypeParam(it) => Some(it.to_nav(db)),
201 } 201 }
202 } 202 }
203} 203}
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index b31956626..26c8996bc 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -2,7 +2,7 @@
2 2
3use hir::{Adt, HasSource, HirDisplay, Semantics}; 3use hir::{Adt, HasSource, HirDisplay, Semantics};
4use ra_ide_db::{ 4use ra_ide_db::{
5 defs::{classify_name, NameDefinition}, 5 defs::{classify_name, Definition},
6 RootDatabase, 6 RootDatabase,
7}; 7};
8use ra_syntax::{ 8use ra_syntax::{
@@ -92,20 +92,20 @@ fn hover_text(docs: Option<String>, desc: Option<String>) -> Option<String> {
92 } 92 }
93} 93}
94 94
95fn hover_text_from_name_kind(db: &RootDatabase, def: NameDefinition) -> Option<String> { 95fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<String> {
96 return match def { 96 return match def {
97 NameDefinition::Macro(it) => { 97 Definition::Macro(it) => {
98 let src = it.source(db); 98 let src = it.source(db);
99 hover_text(src.value.doc_comment_text(), Some(macro_label(&src.value))) 99 hover_text(src.value.doc_comment_text(), Some(macro_label(&src.value)))
100 } 100 }
101 NameDefinition::StructField(it) => { 101 Definition::StructField(it) => {
102 let src = it.source(db); 102 let src = it.source(db);
103 match src.value { 103 match src.value {
104 hir::FieldSource::Named(it) => hover_text(it.doc_comment_text(), it.short_label()), 104 hir::FieldSource::Named(it) => hover_text(it.doc_comment_text(), it.short_label()),
105 _ => None, 105 _ => None,
106 } 106 }
107 } 107 }
108 NameDefinition::ModuleDef(it) => match it { 108 Definition::ModuleDef(it) => match it {
109 hir::ModuleDef::Module(it) => match it.definition_source(db).value { 109 hir::ModuleDef::Module(it) => match it.definition_source(db).value {
110 hir::ModuleSource::Module(it) => { 110 hir::ModuleSource::Module(it) => {
111 hover_text(it.doc_comment_text(), it.short_label()) 111 hover_text(it.doc_comment_text(), it.short_label())
@@ -123,10 +123,10 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: NameDefinition) -> Option<S
123 hir::ModuleDef::TypeAlias(it) => from_def_source(db, it), 123 hir::ModuleDef::TypeAlias(it) => from_def_source(db, it),
124 hir::ModuleDef::BuiltinType(it) => Some(it.to_string()), 124 hir::ModuleDef::BuiltinType(it) => Some(it.to_string()),
125 }, 125 },
126 NameDefinition::Local(it) => { 126 Definition::Local(it) => {
127 Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string())) 127 Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string()))
128 } 128 }
129 NameDefinition::TypeParam(_) | NameDefinition::SelfType(_) => { 129 Definition::TypeParam(_) | Definition::SelfType(_) => {
130 // FIXME: Hover for generic param 130 // FIXME: Hover for generic param
131 None 131 None
132 } 132 }
diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs
index c9c9c6483..95a5c1914 100644
--- a/crates/ra_ide/src/references.rs
+++ b/crates/ra_ide/src/references.rs
@@ -31,7 +31,7 @@ pub(crate) use self::{
31 classify::{classify_name_ref, NameRefClass}, 31 classify::{classify_name_ref, NameRefClass},
32 rename::rename, 32 rename::rename,
33}; 33};
34pub(crate) use ra_ide_db::defs::{classify_name, NameDefinition}; 34pub(crate) use ra_ide_db::defs::{classify_name, Definition};
35 35
36pub use self::search_scope::SearchScope; 36pub use self::search_scope::SearchScope;
37 37
@@ -145,7 +145,7 @@ pub(crate) fn find_all_refs(
145 145
146pub(crate) fn find_refs_to_def( 146pub(crate) fn find_refs_to_def(
147 db: &RootDatabase, 147 db: &RootDatabase,
148 def: &NameDefinition, 148 def: &Definition,
149 search_scope: Option<SearchScope>, 149 search_scope: Option<SearchScope>,
150) -> Vec<Reference> { 150) -> Vec<Reference> {
151 let search_scope = { 151 let search_scope = {
@@ -169,7 +169,7 @@ fn find_name(
169 syntax: &SyntaxNode, 169 syntax: &SyntaxNode,
170 position: FilePosition, 170 position: FilePosition,
171 opt_name: Option<ast::Name>, 171 opt_name: Option<ast::Name>,
172) -> Option<RangeInfo<NameDefinition>> { 172) -> Option<RangeInfo<Definition>> {
173 if let Some(name) = opt_name { 173 if let Some(name) = opt_name {
174 let def = classify_name(sema, &name)?.definition(); 174 let def = classify_name(sema, &name)?.definition();
175 let range = name.syntax().text_range(); 175 let range = name.syntax().text_range();
@@ -183,7 +183,7 @@ fn find_name(
183 183
184fn process_definition( 184fn process_definition(
185 db: &RootDatabase, 185 db: &RootDatabase,
186 def: &NameDefinition, 186 def: &Definition,
187 name: String, 187 name: String,
188 scope: SearchScope, 188 scope: SearchScope,
189) -> Vec<Reference> { 189) -> Vec<Reference> {
@@ -250,13 +250,9 @@ fn process_definition(
250 refs 250 refs
251} 251}
252 252
253fn decl_access( 253fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> {
254 def: &NameDefinition,
255 syntax: &SyntaxNode,
256 range: TextRange,
257) -> Option<ReferenceAccess> {
258 match def { 254 match def {
259 NameDefinition::Local(_) | NameDefinition::StructField(_) => {} 255 Definition::Local(_) | Definition::StructField(_) => {}
260 _ => return None, 256 _ => return None,
261 }; 257 };
262 258
@@ -273,10 +269,10 @@ fn decl_access(
273 None 269 None
274} 270}
275 271
276fn reference_access(def: &NameDefinition, name_ref: &ast::NameRef) -> Option<ReferenceAccess> { 272fn reference_access(def: &Definition, name_ref: &ast::NameRef) -> Option<ReferenceAccess> {
277 // Only Locals and Fields have accesses for now. 273 // Only Locals and Fields have accesses for now.
278 match def { 274 match def {
279 NameDefinition::Local(_) | NameDefinition::StructField(_) => {} 275 Definition::Local(_) | Definition::StructField(_) => {}
280 _ => return None, 276 _ => return None,
281 }; 277 };
282 278
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs
index 571236fdc..0bbf893f8 100644
--- a/crates/ra_ide/src/references/classify.rs
+++ b/crates/ra_ide/src/references/classify.rs
@@ -1,22 +1,22 @@
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
3use hir::{Local, PathResolution, Semantics}; 3use hir::{Local, PathResolution, Semantics};
4use ra_ide_db::defs::NameDefinition; 4use ra_ide_db::defs::Definition;
5use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_prof::profile; 6use ra_prof::profile;
7use ra_syntax::{ast, AstNode}; 7use ra_syntax::{ast, AstNode};
8use test_utils::tested_by; 8use test_utils::tested_by;
9 9
10pub enum NameRefClass { 10pub enum NameRefClass {
11 NameDefinition(NameDefinition), 11 Definition(Definition),
12 FieldShorthand { local: Local, field: NameDefinition }, 12 FieldShorthand { local: Local, field: Definition },
13} 13}
14 14
15impl NameRefClass { 15impl NameRefClass {
16 pub fn definition(self) -> NameDefinition { 16 pub fn definition(self) -> Definition {
17 match self { 17 match self {
18 NameRefClass::NameDefinition(def) => def, 18 NameRefClass::Definition(def) => def,
19 NameRefClass::FieldShorthand { local, field: _ } => NameDefinition::Local(local), 19 NameRefClass::FieldShorthand { local, field: _ } => Definition::Local(local),
20 } 20 }
21 } 21 }
22} 22}
@@ -32,14 +32,14 @@ pub(crate) fn classify_name_ref(
32 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { 32 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
33 tested_by!(goto_def_for_methods); 33 tested_by!(goto_def_for_methods);
34 if let Some(func) = sema.resolve_method_call(&method_call) { 34 if let Some(func) = sema.resolve_method_call(&method_call) {
35 return Some(NameRefClass::NameDefinition(NameDefinition::ModuleDef(func.into()))); 35 return Some(NameRefClass::Definition(Definition::ModuleDef(func.into())));
36 } 36 }
37 } 37 }
38 38
39 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { 39 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
40 tested_by!(goto_def_for_fields); 40 tested_by!(goto_def_for_fields);
41 if let Some(field) = sema.resolve_field(&field_expr) { 41 if let Some(field) = sema.resolve_field(&field_expr) {
42 return Some(NameRefClass::NameDefinition(NameDefinition::StructField(field))); 42 return Some(NameRefClass::Definition(Definition::StructField(field)));
43 } 43 }
44 } 44 }
45 45
@@ -47,9 +47,9 @@ pub(crate) fn classify_name_ref(
47 tested_by!(goto_def_for_record_fields); 47 tested_by!(goto_def_for_record_fields);
48 tested_by!(goto_def_for_field_init_shorthand); 48 tested_by!(goto_def_for_field_init_shorthand);
49 if let Some((field, local)) = sema.resolve_record_field(&record_field) { 49 if let Some((field, local)) = sema.resolve_record_field(&record_field) {
50 let field = NameDefinition::StructField(field); 50 let field = Definition::StructField(field);
51 let res = match local { 51 let res = match local {
52 None => NameRefClass::NameDefinition(field), 52 None => NameRefClass::Definition(field),
53 Some(local) => NameRefClass::FieldShorthand { field, local }, 53 Some(local) => NameRefClass::FieldShorthand { field, local },
54 }; 54 };
55 return Some(res); 55 return Some(res);
@@ -59,26 +59,26 @@ pub(crate) fn classify_name_ref(
59 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { 59 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
60 tested_by!(goto_def_for_macros); 60 tested_by!(goto_def_for_macros);
61 if let Some(macro_def) = sema.resolve_macro_call(&macro_call) { 61 if let Some(macro_def) = sema.resolve_macro_call(&macro_call) {
62 return Some(NameRefClass::NameDefinition(NameDefinition::Macro(macro_def))); 62 return Some(NameRefClass::Definition(Definition::Macro(macro_def)));
63 } 63 }
64 } 64 }
65 65
66 let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; 66 let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
67 let resolved = sema.resolve_path(&path)?; 67 let resolved = sema.resolve_path(&path)?;
68 let res = match resolved { 68 let res = match resolved {
69 PathResolution::Def(def) => NameDefinition::ModuleDef(def), 69 PathResolution::Def(def) => Definition::ModuleDef(def),
70 PathResolution::AssocItem(item) => { 70 PathResolution::AssocItem(item) => {
71 let def = match item { 71 let def = match item {
72 hir::AssocItem::Function(it) => it.into(), 72 hir::AssocItem::Function(it) => it.into(),
73 hir::AssocItem::Const(it) => it.into(), 73 hir::AssocItem::Const(it) => it.into(),
74 hir::AssocItem::TypeAlias(it) => it.into(), 74 hir::AssocItem::TypeAlias(it) => it.into(),
75 }; 75 };
76 NameDefinition::ModuleDef(def) 76 Definition::ModuleDef(def)
77 } 77 }
78 PathResolution::Local(local) => NameDefinition::Local(local), 78 PathResolution::Local(local) => Definition::Local(local),
79 PathResolution::TypeParam(par) => NameDefinition::TypeParam(par), 79 PathResolution::TypeParam(par) => Definition::TypeParam(par),
80 PathResolution::Macro(def) => NameDefinition::Macro(def), 80 PathResolution::Macro(def) => Definition::Macro(def),
81 PathResolution::SelfType(impl_def) => NameDefinition::SelfType(impl_def), 81 PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
82 }; 82 };
83 Some(NameRefClass::NameDefinition(res)) 83 Some(NameRefClass::Definition(res))
84} 84}
diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs
index 27d483233..d98c84d91 100644
--- a/crates/ra_ide/src/references/search_scope.rs
+++ b/crates/ra_ide/src/references/search_scope.rs
@@ -12,7 +12,7 @@ use rustc_hash::FxHashMap;
12 12
13use ra_ide_db::RootDatabase; 13use ra_ide_db::RootDatabase;
14 14
15use super::NameDefinition; 15use super::Definition;
16 16
17pub struct SearchScope { 17pub struct SearchScope {
18 entries: FxHashMap<FileId, Option<TextRange>>, 18 entries: FxHashMap<FileId, Option<TextRange>>,
@@ -23,7 +23,7 @@ impl SearchScope {
23 SearchScope { entries: FxHashMap::default() } 23 SearchScope { entries: FxHashMap::default() }
24 } 24 }
25 25
26 pub(crate) fn for_def(def: &NameDefinition, db: &RootDatabase) -> SearchScope { 26 pub(crate) fn for_def(def: &Definition, db: &RootDatabase) -> SearchScope {
27 let _p = profile("search_scope"); 27 let _p = profile("search_scope");
28 let module = match def.module(db) { 28 let module = match def.module(db) {
29 Some(it) => it, 29 Some(it) => it,
@@ -32,7 +32,7 @@ impl SearchScope {
32 let module_src = module.definition_source(db); 32 let module_src = module.definition_source(db);
33 let file_id = module_src.file_id.original_file(db); 33 let file_id = module_src.file_id.original_file(db);
34 34
35 if let NameDefinition::Local(var) = def { 35 if let Definition::Local(var) = def {
36 let range = match var.parent(db) { 36 let range = match var.parent(db) {
37 DefWithBody::Function(f) => f.source(db).value.syntax().text_range(), 37 DefWithBody::Function(f) => f.source(db).value.syntax().text_range(),
38 DefWithBody::Const(c) => c.source(db).value.syntax().text_range(), 38 DefWithBody::Const(c) => c.source(db).value.syntax().text_range(),
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index 7e41db530..9e0ee2158 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -7,7 +7,7 @@ mod tests;
7 7
8use hir::{Name, Semantics}; 8use hir::{Name, Semantics};
9use ra_ide_db::{ 9use ra_ide_db::{
10 defs::{classify_name, NameClass, NameDefinition}, 10 defs::{classify_name, Definition, NameClass},
11 RootDatabase, 11 RootDatabase,
12}; 12};
13use ra_prof::profile; 13use ra_prof::profile;
@@ -172,7 +172,7 @@ fn highlight_element(
172 let name = element.into_node().and_then(ast::Name::cast).unwrap(); 172 let name = element.into_node().and_then(ast::Name::cast).unwrap();
173 let name_kind = classify_name(sema, &name); 173 let name_kind = classify_name(sema, &name);
174 174
175 if let Some(NameClass::NameDefinition(NameDefinition::Local(local))) = &name_kind { 175 if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
176 if let Some(name) = local.name(db) { 176 if let Some(name) = local.name(db) {
177 let shadow_count = bindings_shadow_count.entry(name.clone()).or_default(); 177 let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
178 *shadow_count += 1; 178 *shadow_count += 1;
@@ -181,7 +181,7 @@ fn highlight_element(
181 }; 181 };
182 182
183 match name_kind { 183 match name_kind {
184 Some(NameClass::NameDefinition(def)) => { 184 Some(NameClass::Definition(def)) => {
185 highlight_name(db, def) | HighlightModifier::Definition 185 highlight_name(db, def) | HighlightModifier::Definition
186 } 186 }
187 Some(NameClass::ConstReference(def)) => highlight_name(db, def), 187 Some(NameClass::ConstReference(def)) => highlight_name(db, def),
@@ -196,8 +196,8 @@ fn highlight_element(
196 let name_kind = classify_name_ref(sema, &name_ref)?; 196 let name_kind = classify_name_ref(sema, &name_ref)?;
197 197
198 match name_kind { 198 match name_kind {
199 NameRefClass::NameDefinition(def) => { 199 NameRefClass::Definition(def) => {
200 if let NameDefinition::Local(local) = &def { 200 if let Definition::Local(local) = &def {
201 if let Some(name) = local.name(db) { 201 if let Some(name) = local.name(db) {
202 let shadow_count = 202 let shadow_count =
203 bindings_shadow_count.entry(name.clone()).or_default(); 203 bindings_shadow_count.entry(name.clone()).or_default();
@@ -260,11 +260,11 @@ fn highlight_element(
260 } 260 }
261} 261}
262 262
263fn highlight_name(db: &RootDatabase, def: NameDefinition) -> Highlight { 263fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
264 match def { 264 match def {
265 NameDefinition::Macro(_) => HighlightTag::Macro, 265 Definition::Macro(_) => HighlightTag::Macro,
266 NameDefinition::StructField(_) => HighlightTag::Field, 266 Definition::StructField(_) => HighlightTag::Field,
267 NameDefinition::ModuleDef(def) => match def { 267 Definition::ModuleDef(def) => match def {
268 hir::ModuleDef::Module(_) => HighlightTag::Module, 268 hir::ModuleDef::Module(_) => HighlightTag::Module,
269 hir::ModuleDef::Function(_) => HighlightTag::Function, 269 hir::ModuleDef::Function(_) => HighlightTag::Function,
270 hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct, 270 hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct,
@@ -277,10 +277,10 @@ fn highlight_name(db: &RootDatabase, def: NameDefinition) -> Highlight {
277 hir::ModuleDef::TypeAlias(_) => HighlightTag::TypeAlias, 277 hir::ModuleDef::TypeAlias(_) => HighlightTag::TypeAlias,
278 hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType, 278 hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
279 }, 279 },
280 NameDefinition::SelfType(_) => HighlightTag::SelfType, 280 Definition::SelfType(_) => HighlightTag::SelfType,
281 NameDefinition::TypeParam(_) => HighlightTag::TypeParam, 281 Definition::TypeParam(_) => HighlightTag::TypeParam,
282 // FIXME: distinguish between locals and parameters 282 // FIXME: distinguish between locals and parameters
283 NameDefinition::Local(local) => { 283 Definition::Local(local) => {
284 let mut h = Highlight::new(HighlightTag::Local); 284 let mut h = Highlight::new(HighlightTag::Local);
285 if local.is_mut(db) || local.ty(db).is_mutable_reference() { 285 if local.is_mut(db) || local.ty(db).is_mutable_reference() {
286 h |= HighlightModifier::Mutable; 286 h |= HighlightModifier::Mutable;
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index 8a5161dfe..215daa441 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -17,8 +17,9 @@ use ra_syntax::{
17 17
18use crate::RootDatabase; 18use crate::RootDatabase;
19 19
20// FIXME: a more precise name would probably be `Symbol`?
20#[derive(Debug, PartialEq, Eq)] 21#[derive(Debug, PartialEq, Eq)]
21pub enum NameDefinition { 22pub enum Definition {
22 Macro(MacroDef), 23 Macro(MacroDef),
23 StructField(StructField), 24 StructField(StructField),
24 ModuleDef(ModuleDef), 25 ModuleDef(ModuleDef),
@@ -27,26 +28,26 @@ pub enum NameDefinition {
27 TypeParam(TypeParam), 28 TypeParam(TypeParam),
28} 29}
29 30
30impl NameDefinition { 31impl Definition {
31 pub fn module(&self, db: &RootDatabase) -> Option<Module> { 32 pub fn module(&self, db: &RootDatabase) -> Option<Module> {
32 match self { 33 match self {
33 NameDefinition::Macro(it) => it.module(db), 34 Definition::Macro(it) => it.module(db),
34 NameDefinition::StructField(it) => Some(it.parent_def(db).module(db)), 35 Definition::StructField(it) => Some(it.parent_def(db).module(db)),
35 NameDefinition::ModuleDef(it) => it.module(db), 36 Definition::ModuleDef(it) => it.module(db),
36 NameDefinition::SelfType(it) => Some(it.module(db)), 37 Definition::SelfType(it) => Some(it.module(db)),
37 NameDefinition::Local(it) => Some(it.module(db)), 38 Definition::Local(it) => Some(it.module(db)),
38 NameDefinition::TypeParam(it) => Some(it.module(db)), 39 Definition::TypeParam(it) => Some(it.module(db)),
39 } 40 }
40 } 41 }
41 42
42 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> { 43 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
43 match self { 44 match self {
44 NameDefinition::Macro(_) => None, 45 Definition::Macro(_) => None,
45 NameDefinition::StructField(sf) => match sf.source(db).value { 46 Definition::StructField(sf) => match sf.source(db).value {
46 FieldSource::Named(it) => it.visibility(), 47 FieldSource::Named(it) => it.visibility(),
47 FieldSource::Pos(it) => it.visibility(), 48 FieldSource::Pos(it) => it.visibility(),
48 }, 49 },
49 NameDefinition::ModuleDef(def) => match def { 50 Definition::ModuleDef(def) => match def {
50 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(), 51 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(),
51 ModuleDef::Function(it) => it.source(db).value.visibility(), 52 ModuleDef::Function(it) => it.source(db).value.visibility(),
52 ModuleDef::Adt(adt) => match adt { 53 ModuleDef::Adt(adt) => match adt {
@@ -61,17 +62,17 @@ impl NameDefinition {
61 ModuleDef::EnumVariant(_) => None, 62 ModuleDef::EnumVariant(_) => None,
62 ModuleDef::BuiltinType(_) => None, 63 ModuleDef::BuiltinType(_) => None,
63 }, 64 },
64 NameDefinition::SelfType(_) => None, 65 Definition::SelfType(_) => None,
65 NameDefinition::Local(_) => None, 66 Definition::Local(_) => None,
66 NameDefinition::TypeParam(_) => None, 67 Definition::TypeParam(_) => None,
67 } 68 }
68 } 69 }
69 70
70 pub fn name(&self, db: &RootDatabase) -> Option<Name> { 71 pub fn name(&self, db: &RootDatabase) -> Option<Name> {
71 let name = match self { 72 let name = match self {
72 NameDefinition::Macro(it) => it.name(db)?, 73 Definition::Macro(it) => it.name(db)?,
73 NameDefinition::StructField(it) => it.name(db), 74 Definition::StructField(it) => it.name(db),
74 NameDefinition::ModuleDef(def) => match def { 75 Definition::ModuleDef(def) => match def {
75 hir::ModuleDef::Module(it) => it.name(db)?, 76 hir::ModuleDef::Module(it) => it.name(db)?,
76 hir::ModuleDef::Function(it) => it.name(db), 77 hir::ModuleDef::Function(it) => it.name(db),
77 hir::ModuleDef::Adt(def) => match def { 78 hir::ModuleDef::Adt(def) => match def {
@@ -86,31 +87,31 @@ impl NameDefinition {
86 hir::ModuleDef::TypeAlias(it) => it.name(db), 87 hir::ModuleDef::TypeAlias(it) => it.name(db),
87 hir::ModuleDef::BuiltinType(_) => return None, 88 hir::ModuleDef::BuiltinType(_) => return None,
88 }, 89 },
89 NameDefinition::SelfType(_) => return None, 90 Definition::SelfType(_) => return None,
90 NameDefinition::Local(it) => it.name(db)?, 91 Definition::Local(it) => it.name(db)?,
91 NameDefinition::TypeParam(it) => it.name(db), 92 Definition::TypeParam(it) => it.name(db),
92 }; 93 };
93 Some(name) 94 Some(name)
94 } 95 }
95} 96}
96 97
97pub enum NameClass { 98pub enum NameClass {
98 NameDefinition(NameDefinition), 99 Definition(Definition),
99 /// `None` in `if let None = Some(82) {}` 100 /// `None` in `if let None = Some(82) {}`
100 ConstReference(NameDefinition), 101 ConstReference(Definition),
101} 102}
102 103
103impl NameClass { 104impl NameClass {
104 pub fn into_definition(self) -> Option<NameDefinition> { 105 pub fn into_definition(self) -> Option<Definition> {
105 match self { 106 match self {
106 NameClass::NameDefinition(it) => Some(it), 107 NameClass::Definition(it) => Some(it),
107 NameClass::ConstReference(_) => None, 108 NameClass::ConstReference(_) => None,
108 } 109 }
109 } 110 }
110 111
111 pub fn definition(self) -> NameDefinition { 112 pub fn definition(self) -> Definition {
112 match self { 113 match self {
113 NameClass::NameDefinition(it) | NameClass::ConstReference(it) => it, 114 NameClass::Definition(it) | NameClass::ConstReference(it) => it,
114 } 115 }
115 } 116 }
116} 117}
@@ -118,14 +119,14 @@ impl NameClass {
118pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameClass> { 119pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameClass> {
119 if let Some(bind_pat) = name.syntax().parent().and_then(ast::BindPat::cast) { 120 if let Some(bind_pat) = name.syntax().parent().and_then(ast::BindPat::cast) {
120 if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) { 121 if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) {
121 return Some(NameClass::ConstReference(NameDefinition::ModuleDef(def))); 122 return Some(NameClass::ConstReference(Definition::ModuleDef(def)));
122 } 123 }
123 } 124 }
124 125
125 classify_name_inner(sema, name).map(NameClass::NameDefinition) 126 classify_name_inner(sema, name).map(NameClass::Definition)
126} 127}
127 128
128fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameDefinition> { 129fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<Definition> {
129 let _p = profile("classify_name"); 130 let _p = profile("classify_name");
130 let parent = name.syntax().parent()?; 131 let parent = name.syntax().parent()?;
131 132
@@ -133,59 +134,59 @@ fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Opti
133 match parent { 134 match parent {
134 ast::BindPat(it) => { 135 ast::BindPat(it) => {
135 let local = sema.to_def(&it)?; 136 let local = sema.to_def(&it)?;
136 Some(NameDefinition::Local(local)) 137 Some(Definition::Local(local))
137 }, 138 },
138 ast::RecordFieldDef(it) => { 139 ast::RecordFieldDef(it) => {
139 let field: hir::StructField = sema.to_def(&it)?; 140 let field: hir::StructField = sema.to_def(&it)?;
140 Some(NameDefinition::StructField(field)) 141 Some(Definition::StructField(field))
141 }, 142 },
142 ast::Module(it) => { 143 ast::Module(it) => {
143 let def = sema.to_def(&it)?; 144 let def = sema.to_def(&it)?;
144 Some(NameDefinition::ModuleDef(def.into())) 145 Some(Definition::ModuleDef(def.into()))
145 }, 146 },
146 ast::StructDef(it) => { 147 ast::StructDef(it) => {
147 let def: hir::Struct = sema.to_def(&it)?; 148 let def: hir::Struct = sema.to_def(&it)?;
148 Some(NameDefinition::ModuleDef(def.into())) 149 Some(Definition::ModuleDef(def.into()))
149 }, 150 },
150 ast::UnionDef(it) => { 151 ast::UnionDef(it) => {
151 let def: hir::Union = sema.to_def(&it)?; 152 let def: hir::Union = sema.to_def(&it)?;
152 Some(NameDefinition::ModuleDef(def.into())) 153 Some(Definition::ModuleDef(def.into()))
153 }, 154 },
154 ast::EnumDef(it) => { 155 ast::EnumDef(it) => {
155 let def: hir::Enum = sema.to_def(&it)?; 156 let def: hir::Enum = sema.to_def(&it)?;
156 Some(NameDefinition::ModuleDef(def.into())) 157 Some(Definition::ModuleDef(def.into()))
157 }, 158 },
158 ast::TraitDef(it) => { 159 ast::TraitDef(it) => {
159 let def: hir::Trait = sema.to_def(&it)?; 160 let def: hir::Trait = sema.to_def(&it)?;
160 Some(NameDefinition::ModuleDef(def.into())) 161 Some(Definition::ModuleDef(def.into()))
161 }, 162 },
162 ast::StaticDef(it) => { 163 ast::StaticDef(it) => {
163 let def: hir::Static = sema.to_def(&it)?; 164 let def: hir::Static = sema.to_def(&it)?;
164 Some(NameDefinition::ModuleDef(def.into())) 165 Some(Definition::ModuleDef(def.into()))
165 }, 166 },
166 ast::EnumVariant(it) => { 167 ast::EnumVariant(it) => {
167 let def: hir::EnumVariant = sema.to_def(&it)?; 168 let def: hir::EnumVariant = sema.to_def(&it)?;
168 Some(NameDefinition::ModuleDef(def.into())) 169 Some(Definition::ModuleDef(def.into()))
169 }, 170 },
170 ast::FnDef(it) => { 171 ast::FnDef(it) => {
171 let def: hir::Function = sema.to_def(&it)?; 172 let def: hir::Function = sema.to_def(&it)?;
172 Some(NameDefinition::ModuleDef(def.into())) 173 Some(Definition::ModuleDef(def.into()))
173 }, 174 },
174 ast::ConstDef(it) => { 175 ast::ConstDef(it) => {
175 let def: hir::Const = sema.to_def(&it)?; 176 let def: hir::Const = sema.to_def(&it)?;
176 Some(NameDefinition::ModuleDef(def.into())) 177 Some(Definition::ModuleDef(def.into()))
177 }, 178 },
178 ast::TypeAliasDef(it) => { 179 ast::TypeAliasDef(it) => {
179 let def: hir::TypeAlias = sema.to_def(&it)?; 180 let def: hir::TypeAlias = sema.to_def(&it)?;
180 Some(NameDefinition::ModuleDef(def.into())) 181 Some(Definition::ModuleDef(def.into()))
181 }, 182 },
182 ast::MacroCall(it) => { 183 ast::MacroCall(it) => {
183 let def = sema.to_def(&it)?; 184 let def = sema.to_def(&it)?;
184 Some(NameDefinition::Macro(def)) 185 Some(Definition::Macro(def))
185 }, 186 },
186 ast::TypeParam(it) => { 187 ast::TypeParam(it) => {
187 let def = sema.to_def(&it)?; 188 let def = sema.to_def(&it)?;
188 Some(NameDefinition::TypeParam(def)) 189 Some(Definition::TypeParam(def))
189 }, 190 },
190 _ => None, 191 _ => None,
191 } 192 }
diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs
index e5fc3c470..c96351982 100644
--- a/crates/ra_ide_db/src/imports_locator.rs
+++ b/crates/ra_ide_db/src/imports_locator.rs
@@ -6,7 +6,7 @@ use ra_prof::profile;
6use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; 6use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
7 7
8use crate::{ 8use crate::{
9 defs::{classify_name, NameDefinition}, 9 defs::{classify_name, Definition},
10 symbol_index::{self, FileSymbol, Query}, 10 symbol_index::{self, FileSymbol, Query},
11 RootDatabase, 11 RootDatabase,
12}; 12};
@@ -43,13 +43,13 @@ impl<'a> ImportsLocator<'a> {
43 .chain(lib_results.into_iter()) 43 .chain(lib_results.into_iter())
44 .filter_map(|import_candidate| self.get_name_definition(&import_candidate)) 44 .filter_map(|import_candidate| self.get_name_definition(&import_candidate))
45 .filter_map(|name_definition_to_import| match name_definition_to_import { 45 .filter_map(|name_definition_to_import| match name_definition_to_import {
46 NameDefinition::ModuleDef(module_def) => Some(module_def), 46 Definition::ModuleDef(module_def) => Some(module_def),
47 _ => None, 47 _ => None,
48 }) 48 })
49 .collect() 49 .collect()
50 } 50 }
51 51
52 fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<NameDefinition> { 52 fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<Definition> {
53 let _p = profile("get_name_definition"); 53 let _p = profile("get_name_definition");
54 let file_id = import_candidate.file_id; 54 let file_id = import_candidate.file_id;
55 55