aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_ide/src/goto_definition.rs18
-rw-r--r--crates/ra_ide/src/hover.rs24
-rw-r--r--crates/ra_ide/src/references.rs32
-rw-r--r--crates/ra_ide/src/references/classify.rs25
-rw-r--r--crates/ra_ide/src/references/search_scope.rs4
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs44
-rw-r--r--crates/ra_ide_db/src/defs.rs55
-rw-r--r--crates/ra_ide_db/src/imports_locator.rs8
8 files changed, 91 insertions, 119 deletions
diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs
index de5551a4c..cce539e56 100644
--- a/crates/ra_ide/src/goto_definition.rs
+++ b/crates/ra_ide/src/goto_definition.rs
@@ -1,7 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::{db::AstDatabase, InFile, SourceBinder}; 3use hir::{db::AstDatabase, InFile, SourceBinder};
4use ra_ide_db::{symbol_index, RootDatabase}; 4use ra_ide_db::{defs::NameDefinition, symbol_index, RootDatabase};
5use ra_syntax::{ 5use ra_syntax::{
6 ast::{self, DocCommentsOwner}, 6 ast::{self, DocCommentsOwner},
7 match_ast, AstNode, 7 match_ast, AstNode,
@@ -12,7 +12,7 @@ use ra_syntax::{
12use crate::{ 12use crate::{
13 display::{ShortLabel, ToNav}, 13 display::{ShortLabel, ToNav},
14 expand::descend_into_macros, 14 expand::descend_into_macros,
15 references::{classify_name_ref, NameKind::*}, 15 references::classify_name_ref,
16 FilePosition, NavigationTarget, RangeInfo, 16 FilePosition, NavigationTarget, RangeInfo,
17}; 17};
18 18
@@ -73,17 +73,17 @@ pub(crate) fn reference_definition(
73) -> ReferenceResult { 73) -> ReferenceResult {
74 use self::ReferenceResult::*; 74 use self::ReferenceResult::*;
75 75
76 let name_kind = classify_name_ref(sb, name_ref).map(|d| d.kind); 76 let name_kind = classify_name_ref(sb, name_ref);
77 match name_kind { 77 match name_kind {
78 Some(Macro(it)) => return Exact(it.to_nav(sb.db)), 78 Some(NameDefinition::Macro(it)) => return Exact(it.to_nav(sb.db)),
79 Some(StructField(it)) => return Exact(it.to_nav(sb.db)), 79 Some(NameDefinition::StructField(it)) => return Exact(it.to_nav(sb.db)),
80 Some(TypeParam(it)) => return Exact(it.to_nav(sb.db)), 80 Some(NameDefinition::TypeParam(it)) => return Exact(it.to_nav(sb.db)),
81 Some(Local(it)) => return Exact(it.to_nav(sb.db)), 81 Some(NameDefinition::Local(it)) => return Exact(it.to_nav(sb.db)),
82 Some(ModuleDef(def)) => match NavigationTarget::from_def(sb.db, def) { 82 Some(NameDefinition::ModuleDef(def)) => match NavigationTarget::from_def(sb.db, def) {
83 Some(nav) => return Exact(nav), 83 Some(nav) => return Exact(nav),
84 None => return Approximate(vec![]), 84 None => return Approximate(vec![]),
85 }, 85 },
86 Some(SelfType(imp)) => { 86 Some(NameDefinition::SelfType(imp)) => {
87 // FIXME: ideally, this should point to the type in the impl, and 87 // FIXME: ideally, this should point to the type in the impl, and
88 // not at the whole impl. And goto **type** definition should bring 88 // not at the whole impl. And goto **type** definition should bring
89 // us to the actual type 89 // us to the actual type
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index 6d4416c0b..1c6ca36df 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -2,7 +2,7 @@
2 2
3use hir::{db::AstDatabase, Adt, HasSource, HirDisplay, SourceBinder}; 3use hir::{db::AstDatabase, Adt, HasSource, HirDisplay, SourceBinder};
4use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
5use ra_ide_db::RootDatabase; 5use ra_ide_db::{defs::NameDefinition, RootDatabase};
6use ra_syntax::{ 6use ra_syntax::{
7 algo::find_covering_element, 7 algo::find_covering_element,
8 ast::{self, DocCommentsOwner}, 8 ast::{self, DocCommentsOwner},
@@ -14,7 +14,7 @@ use ra_syntax::{
14use crate::{ 14use crate::{
15 display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel}, 15 display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel},
16 expand::{descend_into_macros, original_range}, 16 expand::{descend_into_macros, original_range},
17 references::{classify_name, classify_name_ref, NameKind, NameKind::*}, 17 references::{classify_name, classify_name_ref},
18 FilePosition, FileRange, RangeInfo, 18 FilePosition, FileRange, RangeInfo,
19}; 19};
20 20
@@ -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, name_kind: NameKind) -> Option<String> { 95fn hover_text_from_name_kind(db: &RootDatabase, def: NameDefinition) -> Option<String> {
96 return match name_kind { 96 return match def {
97 Macro(it) => { 97 NameDefinition::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 StructField(it) => { 101 NameDefinition::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 ModuleDef(it) => match it { 108 NameDefinition::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,8 +123,10 @@ fn hover_text_from_name_kind(db: &RootDatabase, name_kind: NameKind) -> 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 Local(it) => Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string())), 126 NameDefinition::Local(it) => {
127 TypeParam(_) | SelfType(_) => { 127 Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string()))
128 }
129 NameDefinition::TypeParam(_) | NameDefinition::SelfType(_) => {
128 // FIXME: Hover for generic param 130 // FIXME: Hover for generic param
129 None 131 None
130 } 132 }
@@ -151,10 +153,10 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
151 if let Some((node, name_kind)) = match_ast! { 153 if let Some((node, name_kind)) = match_ast! {
152 match (token.value.parent()) { 154 match (token.value.parent()) {
153 ast::NameRef(name_ref) => { 155 ast::NameRef(name_ref) => {
154 classify_name_ref(&mut sb, token.with_value(&name_ref)).map(|d| (name_ref.syntax().clone(), d.kind)) 156 classify_name_ref(&mut sb, token.with_value(&name_ref)).map(|d| (name_ref.syntax().clone(), d))
155 }, 157 },
156 ast::Name(name) => { 158 ast::Name(name) => {
157 classify_name(&mut sb, token.with_value(&name)).map(|d| (name.syntax().clone(), d.kind)) 159 classify_name(&mut sb, token.with_value(&name)).map(|d| (name.syntax().clone(), d))
158 }, 160 },
159 _ => None, 161 _ => None,
160 } 162 }
diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs
index 97c08ade5..7f790a62d 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, classify_name_ref}, 31 classify::{classify_name, classify_name_ref},
32 rename::rename, 32 rename::rename,
33}; 33};
34pub(crate) use ra_ide_db::defs::{NameDefinition, NameKind}; 34pub(crate) use ra_ide_db::defs::NameDefinition;
35 35
36pub use self::search_scope::SearchScope; 36pub use self::search_scope::SearchScope;
37 37
@@ -126,13 +126,13 @@ pub(crate) fn find_all_refs(
126 126
127 let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position, opt_name)?; 127 let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position, opt_name)?;
128 128
129 let declaration = match def.kind { 129 let declaration = match def {
130 NameKind::Macro(mac) => mac.to_nav(db), 130 NameDefinition::Macro(mac) => mac.to_nav(db),
131 NameKind::StructField(field) => field.to_nav(db), 131 NameDefinition::StructField(field) => field.to_nav(db),
132 NameKind::ModuleDef(def) => NavigationTarget::from_def(db, def)?, 132 NameDefinition::ModuleDef(def) => NavigationTarget::from_def(db, def)?,
133 NameKind::SelfType(imp) => imp.to_nav(db), 133 NameDefinition::SelfType(imp) => imp.to_nav(db),
134 NameKind::Local(local) => local.to_nav(db), 134 NameDefinition::Local(local) => local.to_nav(db),
135 NameKind::TypeParam(_) => return None, 135 NameDefinition::TypeParam(_) => return None,
136 }; 136 };
137 137
138 let search_scope = { 138 let search_scope = {
@@ -148,7 +148,7 @@ pub(crate) fn find_all_refs(
148 let declaration = Declaration { 148 let declaration = Declaration {
149 nav: declaration, 149 nav: declaration,
150 kind: ReferenceKind::Other, 150 kind: ReferenceKind::Other,
151 access: decl_access(&def.kind, &name, &syntax, decl_range), 151 access: decl_access(&def, &name, &syntax, decl_range),
152 }; 152 };
153 153
154 let references = process_definition(db, def, name, search_scope) 154 let references = process_definition(db, def, name, search_scope)
@@ -247,7 +247,7 @@ fn process_definition(
247 refs.push(Reference { 247 refs.push(Reference {
248 file_range: FileRange { file_id, range }, 248 file_range: FileRange { file_id, range },
249 kind, 249 kind,
250 access: reference_access(&d.kind, &name_ref.value), 250 access: reference_access(&d, &name_ref.value),
251 }); 251 });
252 } 252 }
253 } 253 }
@@ -257,13 +257,13 @@ fn process_definition(
257} 257}
258 258
259fn decl_access( 259fn decl_access(
260 kind: &NameKind, 260 def: &NameDefinition,
261 name: &str, 261 name: &str,
262 syntax: &SyntaxNode, 262 syntax: &SyntaxNode,
263 range: TextRange, 263 range: TextRange,
264) -> Option<ReferenceAccess> { 264) -> Option<ReferenceAccess> {
265 match kind { 265 match def {
266 NameKind::Local(_) | NameKind::StructField(_) => {} 266 NameDefinition::Local(_) | NameDefinition::StructField(_) => {}
267 _ => return None, 267 _ => return None,
268 }; 268 };
269 269
@@ -280,10 +280,10 @@ fn decl_access(
280 None 280 None
281} 281}
282 282
283fn reference_access(kind: &NameKind, name_ref: &ast::NameRef) -> Option<ReferenceAccess> { 283fn reference_access(def: &NameDefinition, name_ref: &ast::NameRef) -> Option<ReferenceAccess> {
284 // Only Locals and Fields have accesses for now. 284 // Only Locals and Fields have accesses for now.
285 match kind { 285 match def {
286 NameKind::Local(_) | NameKind::StructField(_) => {} 286 NameDefinition::Local(_) | NameDefinition::StructField(_) => {}
287 _ => return None, 287 _ => return None,
288 }; 288 };
289 289
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs
index ca5750521..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;
5use ra_syntax::{ast, AstNode}; 5use ra_syntax::{ast, AstNode};
6use test_utils::tested_by; 6use test_utils::tested_by;
7 7
8use super::{NameDefinition, NameKind}; 8use super::NameDefinition;
9use ra_ide_db::RootDatabase; 9use ra_ide_db::RootDatabase;
10 10
11pub use ra_ide_db::defs::{classify_name, from_module_def, from_struct_field}; 11pub use ra_ide_db::defs::{classify_name, from_module_def, from_struct_field};
@@ -46,8 +46,7 @@ pub(crate) fn classify_name_ref(
46 if let Some(macro_def) = 46 if let Some(macro_def) =
47 analyzer.resolve_macro_call(sb.db, name_ref.with_value(&macro_call)) 47 analyzer.resolve_macro_call(sb.db, name_ref.with_value(&macro_call))
48 { 48 {
49 let kind = NameKind::Macro(macro_def); 49 return Some(NameDefinition::Macro(macro_def));
50 return Some(NameDefinition { kind });
51 } 50 }
52 } 51 }
53 52
@@ -63,22 +62,10 @@ pub(crate) fn classify_name_ref(
63 }; 62 };
64 from_module_def(def) 63 from_module_def(def)
65 } 64 }
66 PathResolution::Local(local) => { 65 PathResolution::Local(local) => NameDefinition::Local(local),
67 let kind = NameKind::Local(local); 66 PathResolution::TypeParam(par) => NameDefinition::TypeParam(par),
68 NameDefinition { kind } 67 PathResolution::Macro(def) => NameDefinition::Macro(def),
69 } 68 PathResolution::SelfType(impl_block) => NameDefinition::SelfType(impl_block),
70 PathResolution::TypeParam(par) => {
71 let kind = NameKind::TypeParam(par);
72 NameDefinition { kind }
73 }
74 PathResolution::Macro(def) => {
75 let kind = NameKind::Macro(def);
76 NameDefinition { kind }
77 }
78 PathResolution::SelfType(impl_block) => {
79 let kind = NameKind::SelfType(impl_block);
80 NameDefinition { kind }
81 }
82 }; 69 };
83 Some(res) 70 Some(res)
84} 71}
diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs
index e5ac12044..27d483233 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, NameKind}; 15use super::NameDefinition;
16 16
17pub struct SearchScope { 17pub struct SearchScope {
18 entries: FxHashMap<FileId, Option<TextRange>>, 18 entries: FxHashMap<FileId, Option<TextRange>>,
@@ -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 NameKind::Local(var) = def.kind { 35 if let NameDefinition::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 8e793e479..d873f153e 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -2,7 +2,7 @@
2 2
3use hir::{HirFileId, InFile, Name, SourceAnalyzer, SourceBinder}; 3use hir::{HirFileId, InFile, Name, SourceAnalyzer, SourceBinder};
4use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
5use ra_ide_db::RootDatabase; 5use ra_ide_db::{defs::NameDefinition, RootDatabase};
6use ra_prof::profile; 6use ra_prof::profile;
7use ra_syntax::{ 7use ra_syntax::{
8 ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxToken, TextRange, 8 ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxToken, TextRange,
@@ -12,7 +12,7 @@ use rustc_hash::FxHashMap;
12 12
13use crate::{ 13use crate::{
14 expand::descend_into_macros_with_analyzer, 14 expand::descend_into_macros_with_analyzer,
15 references::{classify_name, classify_name_ref, NameKind}, 15 references::{classify_name, classify_name_ref},
16 FileId, 16 FileId,
17}; 17};
18 18
@@ -186,10 +186,10 @@ fn highlight_node(
186 NAME_REF if node.value.ancestors().any(|it| it.kind() == ATTR) => return None, 186 NAME_REF if node.value.ancestors().any(|it| it.kind() == ATTR) => return None,
187 NAME_REF => { 187 NAME_REF => {
188 let name_ref = node.value.as_node().cloned().and_then(ast::NameRef::cast).unwrap(); 188 let name_ref = node.value.as_node().cloned().and_then(ast::NameRef::cast).unwrap();
189 let name_kind = classify_name_ref(sb, node.with_value(&name_ref)).map(|d| d.kind); 189 let name_kind = classify_name_ref(sb, node.with_value(&name_ref));
190 match name_kind { 190 match name_kind {
191 Some(name_kind) => { 191 Some(name_kind) => {
192 if let NameKind::Local(local) = &name_kind { 192 if let NameDefinition::Local(local) = &name_kind {
193 if let Some(name) = local.name(db) { 193 if let Some(name) = local.name(db) {
194 let shadow_count = 194 let shadow_count =
195 bindings_shadow_count.entry(name.clone()).or_default(); 195 bindings_shadow_count.entry(name.clone()).or_default();
@@ -205,9 +205,9 @@ fn highlight_node(
205 } 205 }
206 NAME => { 206 NAME => {
207 let name = node.value.as_node().cloned().and_then(ast::Name::cast).unwrap(); 207 let name = node.value.as_node().cloned().and_then(ast::Name::cast).unwrap();
208 let name_kind = classify_name(sb, node.with_value(&name)).map(|d| d.kind); 208 let name_kind = classify_name(sb, node.with_value(&name));
209 209
210 if let Some(NameKind::Local(local)) = &name_kind { 210 if let Some(NameDefinition::Local(local)) = &name_kind {
211 if let Some(name) = local.name(db) { 211 if let Some(name) = local.name(db) {
212 let shadow_count = bindings_shadow_count.entry(name.clone()).or_default(); 212 let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
213 *shadow_count += 1; 213 *shadow_count += 1;
@@ -310,22 +310,22 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
310 buf 310 buf
311} 311}
312 312
313fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { 313fn highlight_name(db: &RootDatabase, def: NameDefinition) -> &'static str {
314 match name_kind { 314 match def {
315 NameKind::Macro(_) => tags::MACRO, 315 NameDefinition::Macro(_) => tags::MACRO,
316 NameKind::StructField(_) => tags::FIELD, 316 NameDefinition::StructField(_) => tags::FIELD,
317 NameKind::ModuleDef(hir::ModuleDef::Module(_)) => tags::MODULE, 317 NameDefinition::ModuleDef(hir::ModuleDef::Module(_)) => tags::MODULE,
318 NameKind::ModuleDef(hir::ModuleDef::Function(_)) => tags::FUNCTION, 318 NameDefinition::ModuleDef(hir::ModuleDef::Function(_)) => tags::FUNCTION,
319 NameKind::ModuleDef(hir::ModuleDef::Adt(_)) => tags::TYPE, 319 NameDefinition::ModuleDef(hir::ModuleDef::Adt(_)) => tags::TYPE,
320 NameKind::ModuleDef(hir::ModuleDef::EnumVariant(_)) => tags::CONSTANT, 320 NameDefinition::ModuleDef(hir::ModuleDef::EnumVariant(_)) => tags::CONSTANT,
321 NameKind::ModuleDef(hir::ModuleDef::Const(_)) => tags::CONSTANT, 321 NameDefinition::ModuleDef(hir::ModuleDef::Const(_)) => tags::CONSTANT,
322 NameKind::ModuleDef(hir::ModuleDef::Static(_)) => tags::CONSTANT, 322 NameDefinition::ModuleDef(hir::ModuleDef::Static(_)) => tags::CONSTANT,
323 NameKind::ModuleDef(hir::ModuleDef::Trait(_)) => tags::TYPE, 323 NameDefinition::ModuleDef(hir::ModuleDef::Trait(_)) => tags::TYPE,
324 NameKind::ModuleDef(hir::ModuleDef::TypeAlias(_)) => tags::TYPE, 324 NameDefinition::ModuleDef(hir::ModuleDef::TypeAlias(_)) => tags::TYPE,
325 NameKind::ModuleDef(hir::ModuleDef::BuiltinType(_)) => tags::TYPE_BUILTIN, 325 NameDefinition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => tags::TYPE_BUILTIN,
326 NameKind::SelfType(_) => tags::TYPE_SELF, 326 NameDefinition::SelfType(_) => tags::TYPE_SELF,
327 NameKind::TypeParam(_) => tags::TYPE_PARAM, 327 NameDefinition::TypeParam(_) => tags::TYPE_PARAM,
328 NameKind::Local(local) => { 328 NameDefinition::Local(local) => {
329 if local.is_mut(db) || local.ty(db).is_mutable_reference() { 329 if local.is_mut(db) || local.ty(db).is_mutable_reference() {
330 tags::VARIABLE_MUT 330 tags::VARIABLE_MUT
331 } else { 331 } else {
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index aec748abf..04c214624 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -18,7 +18,7 @@ use ra_syntax::{
18use crate::RootDatabase; 18use crate::RootDatabase;
19 19
20#[derive(Debug, PartialEq, Eq)] 20#[derive(Debug, PartialEq, Eq)]
21pub enum NameKind { 21pub enum NameDefinition {
22 Macro(MacroDef), 22 Macro(MacroDef),
23 StructField(StructField), 23 StructField(StructField),
24 ModuleDef(ModuleDef), 24 ModuleDef(ModuleDef),
@@ -27,33 +27,26 @@ pub enum NameKind {
27 TypeParam(TypeParam), 27 TypeParam(TypeParam),
28} 28}
29 29
30#[derive(PartialEq, Eq)]
31pub struct NameDefinition {
32 /// FIXME: this doesn't really make sense. For example, builtin types don't
33 /// really have a module.
34 pub kind: NameKind,
35}
36
37impl NameDefinition { 30impl NameDefinition {
38 pub fn module(&self, db: &RootDatabase) -> Option<Module> { 31 pub fn module(&self, db: &RootDatabase) -> Option<Module> {
39 match self.kind { 32 match self {
40 NameKind::Macro(it) => it.module(db), 33 NameDefinition::Macro(it) => it.module(db),
41 NameKind::StructField(it) => Some(it.parent_def(db).module(db)), 34 NameDefinition::StructField(it) => Some(it.parent_def(db).module(db)),
42 NameKind::ModuleDef(it) => it.module(db), 35 NameDefinition::ModuleDef(it) => it.module(db),
43 NameKind::SelfType(it) => Some(it.module(db)), 36 NameDefinition::SelfType(it) => Some(it.module(db)),
44 NameKind::Local(it) => Some(it.module(db)), 37 NameDefinition::Local(it) => Some(it.module(db)),
45 NameKind::TypeParam(it) => Some(it.module(db)), 38 NameDefinition::TypeParam(it) => Some(it.module(db)),
46 } 39 }
47 } 40 }
48 41
49 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> { 42 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
50 match self.kind { 43 match self {
51 NameKind::Macro(_) => None, 44 NameDefinition::Macro(_) => None,
52 NameKind::StructField(sf) => match sf.source(db).value { 45 NameDefinition::StructField(sf) => match sf.source(db).value {
53 FieldSource::Named(it) => it.visibility(), 46 FieldSource::Named(it) => it.visibility(),
54 FieldSource::Pos(it) => it.visibility(), 47 FieldSource::Pos(it) => it.visibility(),
55 }, 48 },
56 NameKind::ModuleDef(def) => match def { 49 NameDefinition::ModuleDef(def) => match def {
57 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(), 50 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(),
58 ModuleDef::Function(it) => it.source(db).value.visibility(), 51 ModuleDef::Function(it) => it.source(db).value.visibility(),
59 ModuleDef::Adt(adt) => match adt { 52 ModuleDef::Adt(adt) => match adt {
@@ -68,9 +61,9 @@ impl NameDefinition {
68 ModuleDef::EnumVariant(_) => None, 61 ModuleDef::EnumVariant(_) => None,
69 ModuleDef::BuiltinType(_) => None, 62 ModuleDef::BuiltinType(_) => None,
70 }, 63 },
71 NameKind::SelfType(_) => None, 64 NameDefinition::SelfType(_) => None,
72 NameKind::Local(_) => None, 65 NameDefinition::Local(_) => None,
73 NameKind::TypeParam(_) => None, 66 NameDefinition::TypeParam(_) => None,
74 } 67 }
75 } 68 }
76} 69}
@@ -87,9 +80,7 @@ pub fn classify_name(
87 ast::BindPat(it) => { 80 ast::BindPat(it) => {
88 let src = name.with_value(it); 81 let src = name.with_value(it);
89 let local = sb.to_def(src)?; 82 let local = sb.to_def(src)?;
90 Some(NameDefinition { 83 Some(NameDefinition::Local(local))
91 kind: NameKind::Local(local),
92 })
93 }, 84 },
94 ast::RecordFieldDef(it) => { 85 ast::RecordFieldDef(it) => {
95 let src = name.with_value(it); 86 let src = name.with_value(it);
@@ -144,16 +135,12 @@ pub fn classify_name(
144 let src = name.with_value(it); 135 let src = name.with_value(it);
145 let def = sb.to_def(src.clone())?; 136 let def = sb.to_def(src.clone())?;
146 137
147 Some(NameDefinition { 138 Some(NameDefinition::Macro(def))
148 kind: NameKind::Macro(def),
149 })
150 }, 139 },
151 ast::TypeParam(it) => { 140 ast::TypeParam(it) => {
152 let src = name.with_value(it); 141 let src = name.with_value(it);
153 let def = sb.to_def(src)?; 142 let def = sb.to_def(src)?;
154 Some(NameDefinition { 143 Some(NameDefinition::TypeParam(def))
155 kind: NameKind::TypeParam(def),
156 })
157 }, 144 },
158 _ => None, 145 _ => None,
159 } 146 }
@@ -161,11 +148,9 @@ pub fn classify_name(
161} 148}
162 149
163pub fn from_struct_field(field: StructField) -> NameDefinition { 150pub fn from_struct_field(field: StructField) -> NameDefinition {
164 let kind = NameKind::StructField(field); 151 NameDefinition::StructField(field)
165 NameDefinition { kind }
166} 152}
167 153
168pub fn from_module_def(def: ModuleDef) -> NameDefinition { 154pub fn from_module_def(def: ModuleDef) -> NameDefinition {
169 let kind = NameKind::ModuleDef(def); 155 NameDefinition::ModuleDef(def)
170 NameDefinition { kind }
171} 156}
diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs
index 86383bcd0..b8dd358a9 100644
--- a/crates/ra_ide_db/src/imports_locator.rs
+++ b/crates/ra_ide_db/src/imports_locator.rs
@@ -6,8 +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, 9 defs::{classify_name, NameDefinition},
10 defs::NameKind,
11 symbol_index::{self, FileSymbol, Query}, 10 symbol_index::{self, FileSymbol, Query},
12 RootDatabase, 11 RootDatabase,
13}; 12};
@@ -44,7 +43,7 @@ impl<'a> ImportsLocator<'a> {
44 .chain(lib_results.into_iter()) 43 .chain(lib_results.into_iter())
45 .filter_map(|import_candidate| self.get_name_definition(db, &import_candidate)) 44 .filter_map(|import_candidate| self.get_name_definition(db, &import_candidate))
46 .filter_map(|name_definition_to_import| match name_definition_to_import { 45 .filter_map(|name_definition_to_import| match name_definition_to_import {
47 NameKind::ModuleDef(module_def) => Some(module_def), 46 NameDefinition::ModuleDef(module_def) => Some(module_def),
48 _ => None, 47 _ => None,
49 }) 48 })
50 .collect() 49 .collect()
@@ -54,7 +53,7 @@ impl<'a> ImportsLocator<'a> {
54 &mut self, 53 &mut self,
55 db: &impl HirDatabase, 54 db: &impl HirDatabase,
56 import_candidate: &FileSymbol, 55 import_candidate: &FileSymbol,
57 ) -> Option<NameKind> { 56 ) -> Option<NameDefinition> {
58 let _p = profile("get_name_definition"); 57 let _p = profile("get_name_definition");
59 let file_id = import_candidate.file_id.into(); 58 let file_id = import_candidate.file_id.into();
60 let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?); 59 let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?);
@@ -67,6 +66,5 @@ impl<'a> ImportsLocator<'a> {
67 &mut self.source_binder, 66 &mut self.source_binder,
68 hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? }, 67 hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? },
69 ) 68 )
70 .map(|it| it.kind)
71 } 69 }
72} 70}