aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/source_binder.rs8
-rw-r--r--crates/ra_ide/src/references.rs6
-rw-r--r--crates/ra_ide/src/references/classify.rs109
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs12
4 files changed, 78 insertions, 57 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index ca003576a..00541dbe1 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -8,8 +8,8 @@ use hir_def::{
8 dyn_map::DynMap, 8 dyn_map::DynMap,
9 keys::{self, Key}, 9 keys::{self, Key},
10 resolver::{HasResolver, Resolver}, 10 resolver::{HasResolver, Resolver},
11 ConstId, DefWithBodyId, EnumId, FunctionId, ImplId, ModuleId, StaticId, StructId, TraitId, 11 ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ImplId, ModuleId, StaticId,
12 UnionId, VariantId, 12 StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId,
13}; 13};
14use hir_expand::InFile; 14use hir_expand::InFile;
15use ra_prof::profile; 15use ra_prof::profile;
@@ -166,6 +166,8 @@ to_id_impls![
166 (FunctionId, ast::FnDef, keys::FUNCTION), 166 (FunctionId, ast::FnDef, keys::FUNCTION),
167 (StaticId, ast::StaticDef, keys::STATIC), 167 (StaticId, ast::StaticDef, keys::STATIC),
168 (ConstId, ast::ConstDef, keys::CONST), 168 (ConstId, ast::ConstDef, keys::CONST),
169 // (TypeAlias, TypeAliasId, ast::TypeAliasDef, keys::TYPE_ALIAS), 169 (TypeAliasId, ast::TypeAliasDef, keys::TYPE_ALIAS),
170 (ImplId, ast::ImplBlock, keys::IMPL), 170 (ImplId, ast::ImplBlock, keys::IMPL),
171 (StructFieldId, ast::RecordFieldDef, keys::RECORD_FIELD),
172 (EnumVariantId, ast::EnumVariant, keys::ENUM_VARIANT),
171]; 173];
diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs
index 2c753dade..2b74d7653 100644
--- a/crates/ra_ide/src/references.rs
+++ b/crates/ra_ide/src/references.rs
@@ -29,7 +29,7 @@ use crate::{
29}; 29};
30 30
31pub(crate) use self::{ 31pub(crate) use self::{
32 classify::{classify_name, classify_name_ref}, 32 classify::{classify_name, classify_name2, classify_name_ref, classify_name_ref2},
33 name_definition::{NameDefinition, NameKind}, 33 name_definition::{NameDefinition, NameKind},
34 rename::rename, 34 rename::rename,
35}; 35};
@@ -309,7 +309,7 @@ mod tests {
309 } 309 }
310 impl Foo { 310 impl Foo {
311 fn f() -> i32 { 42 } 311 fn f() -> i32 { 42 }
312 } 312 }
313 fn main() { 313 fn main() {
314 let f: Foo; 314 let f: Foo;
315 f = Foo {a: Foo::f()}; 315 f = Foo {a: Foo::f()};
@@ -319,7 +319,7 @@ mod tests {
319 check_result( 319 check_result(
320 refs, 320 refs,
321 "Foo STRUCT_DEF FileId(1) [5; 39) [12; 15) Other", 321 "Foo STRUCT_DEF FileId(1) [5; 39) [12; 15) Other",
322 &["FileId(1) [142; 145) StructLiteral"], 322 &["FileId(1) [138; 141) StructLiteral"],
323 ); 323 );
324 } 324 }
325 325
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs
index 3483a7176..9778ca536 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
3use hir::{FromSource, InFile, Module, ModuleSource, PathResolution, SourceAnalyzer}; 3use hir::{FromSource, InFile, Module, ModuleSource, PathResolution, SourceBinder};
4use ra_prof::profile; 4use ra_prof::profile;
5use ra_syntax::{ast, match_ast, AstNode}; 5use ra_syntax::{ast, match_ast, AstNode};
6use test_utils::tested_by; 6use test_utils::tested_by;
@@ -12,6 +12,14 @@ use super::{
12use crate::db::RootDatabase; 12use crate::db::RootDatabase;
13 13
14pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Option<NameDefinition> { 14pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Option<NameDefinition> {
15 let mut sb = SourceBinder::new(db);
16 classify_name2(&mut sb, name)
17}
18
19pub(crate) fn classify_name2(
20 sb: &mut SourceBinder<RootDatabase>,
21 name: InFile<&ast::Name>,
22) -> Option<NameDefinition> {
15 let _p = profile("classify_name"); 23 let _p = profile("classify_name");
16 let parent = name.value.syntax().parent()?; 24 let parent = name.value.syntax().parent()?;
17 25
@@ -19,90 +27,89 @@ pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Opti
19 match parent { 27 match parent {
20 ast::BindPat(it) => { 28 ast::BindPat(it) => {
21 let src = name.with_value(it); 29 let src = name.with_value(it);
22 let local = hir::Local::from_source(db, src)?; 30 let local = hir::Local::from_source(sb.db, src)?;
23 Some(NameDefinition { 31 Some(NameDefinition {
24 visibility: None, 32 visibility: None,
25 container: local.module(db), 33 container: local.module(sb.db),
26 kind: NameKind::Local(local), 34 kind: NameKind::Local(local),
27 }) 35 })
28 }, 36 },
29 ast::RecordFieldDef(it) => { 37 ast::RecordFieldDef(it) => {
30 let ast = hir::FieldSource::Named(it); 38 let src = name.with_value(it);
31 let src = name.with_value(ast); 39 let field: hir::StructField = sb.to_def(src)?;
32 let field = hir::StructField::from_source(db, src)?; 40 Some(from_struct_field(sb.db, field))
33 Some(from_struct_field(db, field))
34 }, 41 },
35 ast::Module(it) => { 42 ast::Module(it) => {
36 let def = { 43 let def = {
37 if !it.has_semi() { 44 if !it.has_semi() {
38 let ast = hir::ModuleSource::Module(it); 45 let ast = hir::ModuleSource::Module(it);
39 let src = name.with_value(ast); 46 let src = name.with_value(ast);
40 hir::Module::from_definition(db, src) 47 hir::Module::from_definition(sb.db, src)
41 } else { 48 } else {
42 let src = name.with_value(it); 49 let src = name.with_value(it);
43 hir::Module::from_declaration(db, src) 50 hir::Module::from_declaration(sb.db, src)
44 } 51 }
45 }?; 52 }?;
46 Some(from_module_def(db, def.into(), None)) 53 Some(from_module_def(sb.db, def.into(), None))
47 }, 54 },
48 ast::StructDef(it) => { 55 ast::StructDef(it) => {
49 let src = name.with_value(it); 56 let src = name.with_value(it);
50 let def = hir::Struct::from_source(db, src)?; 57 let def: hir::Struct = sb.to_def(src)?;
51 Some(from_module_def(db, def.into(), None)) 58 Some(from_module_def(sb.db, def.into(), None))
52 }, 59 },
53 ast::EnumDef(it) => { 60 ast::EnumDef(it) => {
54 let src = name.with_value(it); 61 let src = name.with_value(it);
55 let def = hir::Enum::from_source(db, src)?; 62 let def: hir::Enum = sb.to_def(src)?;
56 Some(from_module_def(db, def.into(), None)) 63 Some(from_module_def(sb.db, def.into(), None))
57 }, 64 },
58 ast::TraitDef(it) => { 65 ast::TraitDef(it) => {
59 let src = name.with_value(it); 66 let src = name.with_value(it);
60 let def = hir::Trait::from_source(db, src)?; 67 let def: hir::Trait = sb.to_def(src)?;
61 Some(from_module_def(db, def.into(), None)) 68 Some(from_module_def(sb.db, def.into(), None))
62 }, 69 },
63 ast::StaticDef(it) => { 70 ast::StaticDef(it) => {
64 let src = name.with_value(it); 71 let src = name.with_value(it);
65 let def = hir::Static::from_source(db, src)?; 72 let def: hir::Static = sb.to_def(src)?;
66 Some(from_module_def(db, def.into(), None)) 73 Some(from_module_def(sb.db, def.into(), None))
67 }, 74 },
68 ast::EnumVariant(it) => { 75 ast::EnumVariant(it) => {
69 let src = name.with_value(it); 76 let src = name.with_value(it);
70 let def = hir::EnumVariant::from_source(db, src)?; 77 let def: hir::EnumVariant = sb.to_def(src)?;
71 Some(from_module_def(db, def.into(), None)) 78 Some(from_module_def(sb.db, def.into(), None))
72 }, 79 },
73 ast::FnDef(it) => { 80 ast::FnDef(it) => {
74 let src = name.with_value(it); 81 let src = name.with_value(it);
75 let def = hir::Function::from_source(db, src)?; 82 let def: hir::Function = sb.to_def(src)?;
76 if parent.parent().and_then(ast::ItemList::cast).is_some() { 83 if parent.parent().and_then(ast::ItemList::cast).is_some() {
77 Some(from_assoc_item(db, def.into())) 84 Some(from_assoc_item(sb.db, def.into()))
78 } else { 85 } else {
79 Some(from_module_def(db, def.into(), None)) 86 Some(from_module_def(sb.db, def.into(), None))
80 } 87 }
81 }, 88 },
82 ast::ConstDef(it) => { 89 ast::ConstDef(it) => {
83 let src = name.with_value(it); 90 let src = name.with_value(it);
84 let def = hir::Const::from_source(db, src)?; 91 let def: hir::Const = sb.to_def(src)?;
85 if parent.parent().and_then(ast::ItemList::cast).is_some() { 92 if parent.parent().and_then(ast::ItemList::cast).is_some() {
86 Some(from_assoc_item(db, def.into())) 93 Some(from_assoc_item(sb.db, def.into()))
87 } else { 94 } else {
88 Some(from_module_def(db, def.into(), None)) 95 Some(from_module_def(sb.db, def.into(), None))
89 } 96 }
90 }, 97 },
91 ast::TypeAliasDef(it) => { 98 ast::TypeAliasDef(it) => {
92 let src = name.with_value(it); 99 let src = name.with_value(it);
93 let def = hir::TypeAlias::from_source(db, src)?; 100 let def: hir::TypeAlias = sb.to_def(src)?;
94 if parent.parent().and_then(ast::ItemList::cast).is_some() { 101 if parent.parent().and_then(ast::ItemList::cast).is_some() {
95 Some(from_assoc_item(db, def.into())) 102 Some(from_assoc_item(sb.db, def.into()))
96 } else { 103 } else {
97 Some(from_module_def(db, def.into(), None)) 104 Some(from_module_def(sb.db, def.into(), None))
98 } 105 }
99 }, 106 },
100 ast::MacroCall(it) => { 107 ast::MacroCall(it) => {
101 let src = name.with_value(it); 108 let src = name.with_value(it);
102 let def = hir::MacroDef::from_source(db, src.clone())?; 109 let def = hir::MacroDef::from_source(sb.db, src.clone())?;
103 110
104 let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); 111 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))?; 112 let module = Module::from_definition(sb.db, src.with_value(module_src))?;
106 113
107 Some(NameDefinition { 114 Some(NameDefinition {
108 visibility: None, 115 visibility: None,
@@ -112,10 +119,10 @@ pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Opti
112 }, 119 },
113 ast::TypeParam(it) => { 120 ast::TypeParam(it) => {
114 let src = name.with_value(it); 121 let src = name.with_value(it);
115 let def = hir::TypeParam::from_source(db, src)?; 122 let def = hir::TypeParam::from_source(sb.db, src)?;
116 Some(NameDefinition { 123 Some(NameDefinition {
117 visibility: None, 124 visibility: None,
118 container: def.module(db), 125 container: def.module(sb.db),
119 kind: NameKind::TypeParam(def), 126 kind: NameKind::TypeParam(def),
120 }) 127 })
121 }, 128 },
@@ -128,22 +135,30 @@ pub(crate) fn classify_name_ref(
128 db: &RootDatabase, 135 db: &RootDatabase,
129 name_ref: InFile<&ast::NameRef>, 136 name_ref: InFile<&ast::NameRef>,
130) -> Option<NameDefinition> { 137) -> Option<NameDefinition> {
138 let mut sb = SourceBinder::new(db);
139 classify_name_ref2(&mut sb, name_ref)
140}
141
142pub(crate) fn classify_name_ref2(
143 sb: &mut SourceBinder<RootDatabase>,
144 name_ref: InFile<&ast::NameRef>,
145) -> Option<NameDefinition> {
131 let _p = profile("classify_name_ref"); 146 let _p = profile("classify_name_ref");
132 147
133 let parent = name_ref.value.syntax().parent()?; 148 let parent = name_ref.value.syntax().parent()?;
134 let analyzer = SourceAnalyzer::new(db, name_ref.map(|it| it.syntax()), None); 149 let analyzer = sb.analyze(name_ref.map(|it| it.syntax()), None);
135 150
136 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { 151 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
137 tested_by!(goto_def_for_methods); 152 tested_by!(goto_def_for_methods);
138 if let Some(func) = analyzer.resolve_method_call(&method_call) { 153 if let Some(func) = analyzer.resolve_method_call(&method_call) {
139 return Some(from_assoc_item(db, func.into())); 154 return Some(from_assoc_item(sb.db, func.into()));
140 } 155 }
141 } 156 }
142 157
143 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { 158 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
144 tested_by!(goto_def_for_fields); 159 tested_by!(goto_def_for_fields);
145 if let Some(field) = analyzer.resolve_field(&field_expr) { 160 if let Some(field) = analyzer.resolve_field(&field_expr) {
146 return Some(from_struct_field(db, field)); 161 return Some(from_struct_field(sb.db, field));
147 } 162 }
148 } 163 }
149 164
@@ -151,30 +166,32 @@ pub(crate) fn classify_name_ref(
151 tested_by!(goto_def_for_record_fields); 166 tested_by!(goto_def_for_record_fields);
152 tested_by!(goto_def_for_field_init_shorthand); 167 tested_by!(goto_def_for_field_init_shorthand);
153 if let Some(field_def) = analyzer.resolve_record_field(&record_field) { 168 if let Some(field_def) = analyzer.resolve_record_field(&record_field) {
154 return Some(from_struct_field(db, field_def)); 169 return Some(from_struct_field(sb.db, field_def));
155 } 170 }
156 } 171 }
157 172
158 let ast = ModuleSource::from_child_node(db, name_ref.with_value(&parent)); 173 let ast = ModuleSource::from_child_node(sb.db, name_ref.with_value(&parent));
159 // FIXME: find correct container and visibility for each case 174 // FIXME: find correct container and visibility for each case
160 let container = Module::from_definition(db, name_ref.with_value(ast))?; 175 let container = Module::from_definition(sb.db, name_ref.with_value(ast))?;
161 let visibility = None; 176 let visibility = None;
162 177
163 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { 178 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
164 tested_by!(goto_def_for_macros); 179 tested_by!(goto_def_for_macros);
165 if let Some(macro_def) = analyzer.resolve_macro_call(db, name_ref.with_value(&macro_call)) { 180 if let Some(macro_def) =
181 analyzer.resolve_macro_call(sb.db, name_ref.with_value(&macro_call))
182 {
166 let kind = NameKind::Macro(macro_def); 183 let kind = NameKind::Macro(macro_def);
167 return Some(NameDefinition { kind, container, visibility }); 184 return Some(NameDefinition { kind, container, visibility });
168 } 185 }
169 } 186 }
170 187
171 let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?; 188 let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?;
172 let resolved = analyzer.resolve_path(db, &path)?; 189 let resolved = analyzer.resolve_path(sb.db, &path)?;
173 match resolved { 190 match resolved {
174 PathResolution::Def(def) => Some(from_module_def(db, def, Some(container))), 191 PathResolution::Def(def) => Some(from_module_def(sb.db, def, Some(container))),
175 PathResolution::AssocItem(item) => Some(from_assoc_item(db, item)), 192 PathResolution::AssocItem(item) => Some(from_assoc_item(sb.db, item)),
176 PathResolution::Local(local) => { 193 PathResolution::Local(local) => {
177 let container = local.module(db); 194 let container = local.module(sb.db);
178 let kind = NameKind::Local(local); 195 let kind = NameKind::Local(local);
179 Some(NameDefinition { kind, container, visibility: None }) 196 Some(NameDefinition { kind, container, visibility: None })
180 } 197 }
@@ -188,7 +205,7 @@ pub(crate) fn classify_name_ref(
188 } 205 }
189 PathResolution::SelfType(impl_block) => { 206 PathResolution::SelfType(impl_block) => {
190 let kind = NameKind::SelfType(impl_block); 207 let kind = NameKind::SelfType(impl_block);
191 let container = impl_block.module(db); 208 let container = impl_block.module(sb.db);
192 Some(NameDefinition { kind, container, visibility }) 209 Some(NameDefinition { kind, container, visibility })
193 } 210 }
194 } 211 }
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index f06a8933e..fd422d07c 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -2,7 +2,7 @@
2 2
3use rustc_hash::{FxHashMap, FxHashSet}; 3use rustc_hash::{FxHashMap, FxHashSet};
4 4
5use hir::{InFile, Name}; 5use hir::{InFile, Name, SourceBinder};
6use ra_db::SourceDatabase; 6use ra_db::SourceDatabase;
7use ra_prof::profile; 7use ra_prof::profile;
8use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T}; 8use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T};
@@ -10,7 +10,7 @@ use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::
10use crate::{ 10use crate::{
11 db::RootDatabase, 11 db::RootDatabase,
12 references::{ 12 references::{
13 classify_name, classify_name_ref, 13 classify_name2, classify_name_ref2,
14 NameKind::{self, *}, 14 NameKind::{self, *},
15 }, 15 },
16 FileId, 16 FileId,
@@ -84,6 +84,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
84 hash((file_id, name, shadow_count)) 84 hash((file_id, name, shadow_count))
85 } 85 }
86 86
87 let mut sb = SourceBinder::new(db);
88
87 // Visited nodes to handle highlighting priorities 89 // Visited nodes to handle highlighting priorities
88 // FIXME: retain only ranges here 90 // FIXME: retain only ranges here
89 let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default(); 91 let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default();
@@ -108,8 +110,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
108 NAME_REF if node.ancestors().any(|it| it.kind() == ATTR) => continue, 110 NAME_REF if node.ancestors().any(|it| it.kind() == ATTR) => continue,
109 NAME_REF => { 111 NAME_REF => {
110 let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap(); 112 let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap();
111 let name_kind = 113 let name_kind = classify_name_ref2(&mut sb, InFile::new(file_id.into(), &name_ref))
112 classify_name_ref(db, InFile::new(file_id.into(), &name_ref)).map(|d| d.kind); 114 .map(|d| d.kind);
113 match name_kind { 115 match name_kind {
114 Some(name_kind) => { 116 Some(name_kind) => {
115 if let Local(local) = &name_kind { 117 if let Local(local) = &name_kind {
@@ -129,7 +131,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
129 NAME => { 131 NAME => {
130 let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); 132 let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap();
131 let name_kind = 133 let name_kind =
132 classify_name(db, InFile::new(file_id.into(), &name)).map(|d| d.kind); 134 classify_name2(&mut sb, InFile::new(file_id.into(), &name)).map(|d| d.kind);
133 135
134 if let Some(Local(local)) = &name_kind { 136 if let Some(Local(local)) = &name_kind {
135 if let Some(name) = local.name(db) { 137 if let Some(name) = local.name(db) {