aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src')
-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
6 files changed, 68 insertions, 79 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 {