diff options
Diffstat (limited to 'crates/ide_db/src/defs.rs')
-rw-r--r-- | crates/ide_db/src/defs.rs | 80 |
1 files changed, 40 insertions, 40 deletions
diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index cc5078bf0..d9875ffef 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs | |||
@@ -6,11 +6,11 @@ | |||
6 | // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). | 6 | // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). |
7 | 7 | ||
8 | use hir::{ | 8 | use hir::{ |
9 | db::HirDatabase, ConstParam, Crate, Field, HasVisibility, Impl, Label, LifetimeParam, Local, | 9 | db::HirDatabase, Crate, Field, GenericParam, HasVisibility, Impl, Label, Local, MacroDef, |
10 | MacroDef, Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility, | 10 | Module, ModuleDef, Name, PathResolution, Semantics, Visibility, |
11 | }; | 11 | }; |
12 | use syntax::{ | 12 | use syntax::{ |
13 | ast::{self, AstNode}, | 13 | ast::{self, AstNode, PathSegmentKind}, |
14 | match_ast, SyntaxKind, SyntaxNode, | 14 | match_ast, SyntaxKind, SyntaxNode, |
15 | }; | 15 | }; |
16 | 16 | ||
@@ -24,9 +24,7 @@ pub enum Definition { | |||
24 | ModuleDef(ModuleDef), | 24 | ModuleDef(ModuleDef), |
25 | SelfType(Impl), | 25 | SelfType(Impl), |
26 | Local(Local), | 26 | Local(Local), |
27 | TypeParam(TypeParam), | 27 | GenericParam(GenericParam), |
28 | LifetimeParam(LifetimeParam), | ||
29 | ConstParam(ConstParam), | ||
30 | Label(Label), | 28 | Label(Label), |
31 | } | 29 | } |
32 | 30 | ||
@@ -38,9 +36,7 @@ impl Definition { | |||
38 | Definition::ModuleDef(it) => it.module(db), | 36 | Definition::ModuleDef(it) => it.module(db), |
39 | Definition::SelfType(it) => Some(it.module(db)), | 37 | Definition::SelfType(it) => Some(it.module(db)), |
40 | Definition::Local(it) => Some(it.module(db)), | 38 | Definition::Local(it) => Some(it.module(db)), |
41 | Definition::TypeParam(it) => Some(it.module(db)), | 39 | Definition::GenericParam(it) => Some(it.module(db)), |
42 | Definition::LifetimeParam(it) => Some(it.module(db)), | ||
43 | Definition::ConstParam(it) => Some(it.module(db)), | ||
44 | Definition::Label(it) => Some(it.module(db)), | 40 | Definition::Label(it) => Some(it.module(db)), |
45 | } | 41 | } |
46 | } | 42 | } |
@@ -52,9 +48,7 @@ impl Definition { | |||
52 | Definition::ModuleDef(def) => def.definition_visibility(db), | 48 | Definition::ModuleDef(def) => def.definition_visibility(db), |
53 | Definition::SelfType(_) => None, | 49 | Definition::SelfType(_) => None, |
54 | Definition::Local(_) => None, | 50 | Definition::Local(_) => None, |
55 | Definition::TypeParam(_) => None, | 51 | Definition::GenericParam(_) => None, |
56 | Definition::LifetimeParam(_) => None, | ||
57 | Definition::ConstParam(_) => None, | ||
58 | Definition::Label(_) => None, | 52 | Definition::Label(_) => None, |
59 | } | 53 | } |
60 | } | 54 | } |
@@ -80,9 +74,7 @@ impl Definition { | |||
80 | }, | 74 | }, |
81 | Definition::SelfType(_) => return None, | 75 | Definition::SelfType(_) => return None, |
82 | Definition::Local(it) => it.name(db)?, | 76 | Definition::Local(it) => it.name(db)?, |
83 | Definition::TypeParam(it) => it.name(db), | 77 | Definition::GenericParam(it) => it.name(db), |
84 | Definition::LifetimeParam(it) => it.name(db), | ||
85 | Definition::ConstParam(it) => it.name(db), | ||
86 | Definition::Label(it) => it.name(db), | 78 | Definition::Label(it) => it.name(db), |
87 | }; | 79 | }; |
88 | Some(name) | 80 | Some(name) |
@@ -143,24 +135,26 @@ impl NameClass { | |||
143 | let path = use_tree.path()?; | 135 | let path = use_tree.path()?; |
144 | let path_segment = path.segment()?; | 136 | let path_segment = path.segment()?; |
145 | let name_ref_class = path_segment | 137 | let name_ref_class = path_segment |
146 | .name_ref() | 138 | .kind() |
147 | // The rename might be from a `self` token, so fallback to the name higher | 139 | .and_then(|kind| { |
148 | // in the use tree. | 140 | match kind { |
149 | .or_else(||{ | 141 | // The rename might be from a `self` token, so fallback to the name higher |
150 | if path_segment.self_token().is_none() { | 142 | // in the use tree. |
151 | return None; | 143 | PathSegmentKind::SelfKw => { |
144 | let use_tree = use_tree | ||
145 | .syntax() | ||
146 | .parent() | ||
147 | .as_ref() | ||
148 | // Skip over UseTreeList | ||
149 | .and_then(SyntaxNode::parent) | ||
150 | .and_then(ast::UseTree::cast)?; | ||
151 | let path = use_tree.path()?; | ||
152 | let path_segment = path.segment()?; | ||
153 | path_segment.name_ref() | ||
154 | }, | ||
155 | PathSegmentKind::Name(name_ref) => Some(name_ref), | ||
156 | _ => return None, | ||
152 | } | 157 | } |
153 | |||
154 | let use_tree = use_tree | ||
155 | .syntax() | ||
156 | .parent() | ||
157 | .as_ref() | ||
158 | // Skip over UseTreeList | ||
159 | .and_then(SyntaxNode::parent) | ||
160 | .and_then(ast::UseTree::cast)?; | ||
161 | let path = use_tree.path()?; | ||
162 | let path_segment = path.segment()?; | ||
163 | path_segment.name_ref() | ||
164 | }) | 158 | }) |
165 | .and_then(|name_ref| NameRefClass::classify(sema, &name_ref))?; | 159 | .and_then(|name_ref| NameRefClass::classify(sema, &name_ref))?; |
166 | 160 | ||
@@ -185,6 +179,10 @@ impl NameClass { | |||
185 | 179 | ||
186 | Some(NameClass::Definition(Definition::Local(local))) | 180 | Some(NameClass::Definition(Definition::Local(local))) |
187 | }, | 181 | }, |
182 | ast::SelfParam(it) => { | ||
183 | let def = sema.to_def(&it)?; | ||
184 | Some(NameClass::Definition(Definition::Local(def.into()))) | ||
185 | }, | ||
188 | ast::RecordField(it) => { | 186 | ast::RecordField(it) => { |
189 | let field: hir::Field = sema.to_def(&it)?; | 187 | let field: hir::Field = sema.to_def(&it)?; |
190 | Some(NameClass::Definition(Definition::Field(field))) | 188 | Some(NameClass::Definition(Definition::Field(field))) |
@@ -235,11 +233,11 @@ impl NameClass { | |||
235 | }, | 233 | }, |
236 | ast::TypeParam(it) => { | 234 | ast::TypeParam(it) => { |
237 | let def = sema.to_def(&it)?; | 235 | let def = sema.to_def(&it)?; |
238 | Some(NameClass::Definition(Definition::TypeParam(def))) | 236 | Some(NameClass::Definition(Definition::GenericParam(def.into()))) |
239 | }, | 237 | }, |
240 | ast::ConstParam(it) => { | 238 | ast::ConstParam(it) => { |
241 | let def = sema.to_def(&it)?; | 239 | let def = sema.to_def(&it)?; |
242 | Some(NameClass::Definition(Definition::ConstParam(def))) | 240 | Some(NameClass::Definition(Definition::GenericParam(def.into()))) |
243 | }, | 241 | }, |
244 | _ => None, | 242 | _ => None, |
245 | } | 243 | } |
@@ -257,7 +255,7 @@ impl NameClass { | |||
257 | match parent { | 255 | match parent { |
258 | ast::LifetimeParam(it) => { | 256 | ast::LifetimeParam(it) => { |
259 | let def = sema.to_def(&it)?; | 257 | let def = sema.to_def(&it)?; |
260 | Some(NameClass::Definition(Definition::LifetimeParam(def))) | 258 | Some(NameClass::Definition(Definition::GenericParam(def.into()))) |
261 | }, | 259 | }, |
262 | ast::Label(it) => { | 260 | ast::Label(it) => { |
263 | let def = sema.to_def(&it)?; | 261 | let def = sema.to_def(&it)?; |
@@ -358,7 +356,7 @@ impl NameRefClass { | |||
358 | if let Some(path) = macro_call.path() { | 356 | if let Some(path) = macro_call.path() { |
359 | if path.qualifier().is_none() { | 357 | if path.qualifier().is_none() { |
360 | // Only use this to resolve single-segment macro calls like `foo!()`. Multi-segment | 358 | // Only use this to resolve single-segment macro calls like `foo!()`. Multi-segment |
361 | // paths are handled below (allowing `log<|>::info!` to resolve to the log crate). | 359 | // paths are handled below (allowing `log$0::info!` to resolve to the log crate). |
362 | if let Some(macro_def) = sema.resolve_macro_call(¯o_call) { | 360 | if let Some(macro_def) = sema.resolve_macro_call(¯o_call) { |
363 | return Some(NameRefClass::Definition(Definition::Macro(macro_def))); | 361 | return Some(NameRefClass::Definition(Definition::Macro(macro_def))); |
364 | } | 362 | } |
@@ -393,7 +391,8 @@ impl NameRefClass { | |||
393 | | SyntaxKind::WHERE_PRED | 391 | | SyntaxKind::WHERE_PRED |
394 | | SyntaxKind::REF_TYPE => sema | 392 | | SyntaxKind::REF_TYPE => sema |
395 | .resolve_lifetime_param(lifetime) | 393 | .resolve_lifetime_param(lifetime) |
396 | .map(Definition::LifetimeParam) | 394 | .map(GenericParam::LifetimeParam) |
395 | .map(Definition::GenericParam) | ||
397 | .map(NameRefClass::Definition), | 396 | .map(NameRefClass::Definition), |
398 | // lifetime bounds, as in the 'b in 'a: 'b aren't wrapped in TypeBound nodes so we gotta check | 397 | // lifetime bounds, as in the 'b in 'a: 'b aren't wrapped in TypeBound nodes so we gotta check |
399 | // if our lifetime is in a LifetimeParam without being the constrained lifetime | 398 | // if our lifetime is in a LifetimeParam without being the constrained lifetime |
@@ -401,7 +400,8 @@ impl NameRefClass { | |||
401 | != Some(lifetime) => | 400 | != Some(lifetime) => |
402 | { | 401 | { |
403 | sema.resolve_lifetime_param(lifetime) | 402 | sema.resolve_lifetime_param(lifetime) |
404 | .map(Definition::LifetimeParam) | 403 | .map(GenericParam::LifetimeParam) |
404 | .map(Definition::GenericParam) | ||
405 | .map(NameRefClass::Definition) | 405 | .map(NameRefClass::Definition) |
406 | } | 406 | } |
407 | _ => None, | 407 | _ => None, |
@@ -422,10 +422,10 @@ impl From<PathResolution> for Definition { | |||
422 | Definition::ModuleDef(def) | 422 | Definition::ModuleDef(def) |
423 | } | 423 | } |
424 | PathResolution::Local(local) => Definition::Local(local), | 424 | PathResolution::Local(local) => Definition::Local(local), |
425 | PathResolution::TypeParam(par) => Definition::TypeParam(par), | 425 | PathResolution::TypeParam(par) => Definition::GenericParam(par.into()), |
426 | PathResolution::Macro(def) => Definition::Macro(def), | 426 | PathResolution::Macro(def) => Definition::Macro(def), |
427 | PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def), | 427 | PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def), |
428 | PathResolution::ConstParam(par) => Definition::ConstParam(par), | 428 | PathResolution::ConstParam(par) => Definition::GenericParam(par.into()), |
429 | } | 429 | } |
430 | } | 430 | } |
431 | } | 431 | } |