diff options
Diffstat (limited to 'crates/ra_ide/src/references/classify.rs')
-rw-r--r-- | crates/ra_ide/src/references/classify.rs | 84 |
1 files changed, 0 insertions, 84 deletions
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs deleted file mode 100644 index 0bbf893f8..000000000 --- a/crates/ra_ide/src/references/classify.rs +++ /dev/null | |||
@@ -1,84 +0,0 @@ | |||
1 | //! Functions that are used to classify an element from its definition or reference. | ||
2 | |||
3 | use hir::{Local, PathResolution, Semantics}; | ||
4 | use ra_ide_db::defs::Definition; | ||
5 | use ra_ide_db::RootDatabase; | ||
6 | use ra_prof::profile; | ||
7 | use ra_syntax::{ast, AstNode}; | ||
8 | use test_utils::tested_by; | ||
9 | |||
10 | pub enum NameRefClass { | ||
11 | Definition(Definition), | ||
12 | FieldShorthand { local: Local, field: Definition }, | ||
13 | } | ||
14 | |||
15 | impl NameRefClass { | ||
16 | pub fn definition(self) -> Definition { | ||
17 | match self { | ||
18 | NameRefClass::Definition(def) => def, | ||
19 | NameRefClass::FieldShorthand { local, field: _ } => Definition::Local(local), | ||
20 | } | ||
21 | } | ||
22 | } | ||
23 | |||
24 | pub(crate) fn classify_name_ref( | ||
25 | sema: &Semantics<RootDatabase>, | ||
26 | name_ref: &ast::NameRef, | ||
27 | ) -> Option<NameRefClass> { | ||
28 | let _p = profile("classify_name_ref"); | ||
29 | |||
30 | let parent = name_ref.syntax().parent()?; | ||
31 | |||
32 | if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { | ||
33 | tested_by!(goto_def_for_methods); | ||
34 | if let Some(func) = sema.resolve_method_call(&method_call) { | ||
35 | return Some(NameRefClass::Definition(Definition::ModuleDef(func.into()))); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { | ||
40 | tested_by!(goto_def_for_fields); | ||
41 | if let Some(field) = sema.resolve_field(&field_expr) { | ||
42 | return Some(NameRefClass::Definition(Definition::StructField(field))); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | if let Some(record_field) = ast::RecordField::cast(parent.clone()) { | ||
47 | tested_by!(goto_def_for_record_fields); | ||
48 | tested_by!(goto_def_for_field_init_shorthand); | ||
49 | if let Some((field, local)) = sema.resolve_record_field(&record_field) { | ||
50 | let field = Definition::StructField(field); | ||
51 | let res = match local { | ||
52 | None => NameRefClass::Definition(field), | ||
53 | Some(local) => NameRefClass::FieldShorthand { field, local }, | ||
54 | }; | ||
55 | return Some(res); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { | ||
60 | tested_by!(goto_def_for_macros); | ||
61 | if let Some(macro_def) = sema.resolve_macro_call(¯o_call) { | ||
62 | return Some(NameRefClass::Definition(Definition::Macro(macro_def))); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; | ||
67 | let resolved = sema.resolve_path(&path)?; | ||
68 | let res = match resolved { | ||
69 | PathResolution::Def(def) => Definition::ModuleDef(def), | ||
70 | PathResolution::AssocItem(item) => { | ||
71 | let def = match item { | ||
72 | hir::AssocItem::Function(it) => it.into(), | ||
73 | hir::AssocItem::Const(it) => it.into(), | ||
74 | hir::AssocItem::TypeAlias(it) => it.into(), | ||
75 | }; | ||
76 | Definition::ModuleDef(def) | ||
77 | } | ||
78 | PathResolution::Local(local) => Definition::Local(local), | ||
79 | PathResolution::TypeParam(par) => Definition::TypeParam(par), | ||
80 | PathResolution::Macro(def) => Definition::Macro(def), | ||
81 | PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def), | ||
82 | }; | ||
83 | Some(NameRefClass::Definition(res)) | ||
84 | } | ||