aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-03-03 17:41:25 +0000
committerGitHub <[email protected]>2020-03-03 17:41:25 +0000
commit074474fe00a08d394cbdcac2a136bca825d93377 (patch)
tree0be46628b67ddfaa15be54520cdc41a03977a76a /crates/ra_ide_db
parent7a322f9afff05b88507a6956a2d84a3abef0a0d6 (diff)
parent34d6e22fc12209417c15d215052857ddac79cdf4 (diff)
Merge #3427
3427: Rename NameDefinition -> Definition r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r--crates/ra_ide_db/src/defs.rs87
-rw-r--r--crates/ra_ide_db/src/imports_locator.rs6
2 files changed, 47 insertions, 46 deletions
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index 8a5161dfe..215daa441 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -17,8 +17,9 @@ use ra_syntax::{
17 17
18use crate::RootDatabase; 18use crate::RootDatabase;
19 19
20// FIXME: a more precise name would probably be `Symbol`?
20#[derive(Debug, PartialEq, Eq)] 21#[derive(Debug, PartialEq, Eq)]
21pub enum NameDefinition { 22pub enum Definition {
22 Macro(MacroDef), 23 Macro(MacroDef),
23 StructField(StructField), 24 StructField(StructField),
24 ModuleDef(ModuleDef), 25 ModuleDef(ModuleDef),
@@ -27,26 +28,26 @@ pub enum NameDefinition {
27 TypeParam(TypeParam), 28 TypeParam(TypeParam),
28} 29}
29 30
30impl NameDefinition { 31impl Definition {
31 pub fn module(&self, db: &RootDatabase) -> Option<Module> { 32 pub fn module(&self, db: &RootDatabase) -> Option<Module> {
32 match self { 33 match self {
33 NameDefinition::Macro(it) => it.module(db), 34 Definition::Macro(it) => it.module(db),
34 NameDefinition::StructField(it) => Some(it.parent_def(db).module(db)), 35 Definition::StructField(it) => Some(it.parent_def(db).module(db)),
35 NameDefinition::ModuleDef(it) => it.module(db), 36 Definition::ModuleDef(it) => it.module(db),
36 NameDefinition::SelfType(it) => Some(it.module(db)), 37 Definition::SelfType(it) => Some(it.module(db)),
37 NameDefinition::Local(it) => Some(it.module(db)), 38 Definition::Local(it) => Some(it.module(db)),
38 NameDefinition::TypeParam(it) => Some(it.module(db)), 39 Definition::TypeParam(it) => Some(it.module(db)),
39 } 40 }
40 } 41 }
41 42
42 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> { 43 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
43 match self { 44 match self {
44 NameDefinition::Macro(_) => None, 45 Definition::Macro(_) => None,
45 NameDefinition::StructField(sf) => match sf.source(db).value { 46 Definition::StructField(sf) => match sf.source(db).value {
46 FieldSource::Named(it) => it.visibility(), 47 FieldSource::Named(it) => it.visibility(),
47 FieldSource::Pos(it) => it.visibility(), 48 FieldSource::Pos(it) => it.visibility(),
48 }, 49 },
49 NameDefinition::ModuleDef(def) => match def { 50 Definition::ModuleDef(def) => match def {
50 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(), 51 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(),
51 ModuleDef::Function(it) => it.source(db).value.visibility(), 52 ModuleDef::Function(it) => it.source(db).value.visibility(),
52 ModuleDef::Adt(adt) => match adt { 53 ModuleDef::Adt(adt) => match adt {
@@ -61,17 +62,17 @@ impl NameDefinition {
61 ModuleDef::EnumVariant(_) => None, 62 ModuleDef::EnumVariant(_) => None,
62 ModuleDef::BuiltinType(_) => None, 63 ModuleDef::BuiltinType(_) => None,
63 }, 64 },
64 NameDefinition::SelfType(_) => None, 65 Definition::SelfType(_) => None,
65 NameDefinition::Local(_) => None, 66 Definition::Local(_) => None,
66 NameDefinition::TypeParam(_) => None, 67 Definition::TypeParam(_) => None,
67 } 68 }
68 } 69 }
69 70
70 pub fn name(&self, db: &RootDatabase) -> Option<Name> { 71 pub fn name(&self, db: &RootDatabase) -> Option<Name> {
71 let name = match self { 72 let name = match self {
72 NameDefinition::Macro(it) => it.name(db)?, 73 Definition::Macro(it) => it.name(db)?,
73 NameDefinition::StructField(it) => it.name(db), 74 Definition::StructField(it) => it.name(db),
74 NameDefinition::ModuleDef(def) => match def { 75 Definition::ModuleDef(def) => match def {
75 hir::ModuleDef::Module(it) => it.name(db)?, 76 hir::ModuleDef::Module(it) => it.name(db)?,
76 hir::ModuleDef::Function(it) => it.name(db), 77 hir::ModuleDef::Function(it) => it.name(db),
77 hir::ModuleDef::Adt(def) => match def { 78 hir::ModuleDef::Adt(def) => match def {
@@ -86,31 +87,31 @@ impl NameDefinition {
86 hir::ModuleDef::TypeAlias(it) => it.name(db), 87 hir::ModuleDef::TypeAlias(it) => it.name(db),
87 hir::ModuleDef::BuiltinType(_) => return None, 88 hir::ModuleDef::BuiltinType(_) => return None,
88 }, 89 },
89 NameDefinition::SelfType(_) => return None, 90 Definition::SelfType(_) => return None,
90 NameDefinition::Local(it) => it.name(db)?, 91 Definition::Local(it) => it.name(db)?,
91 NameDefinition::TypeParam(it) => it.name(db), 92 Definition::TypeParam(it) => it.name(db),
92 }; 93 };
93 Some(name) 94 Some(name)
94 } 95 }
95} 96}
96 97
97pub enum NameClass { 98pub enum NameClass {
98 NameDefinition(NameDefinition), 99 Definition(Definition),
99 /// `None` in `if let None = Some(82) {}` 100 /// `None` in `if let None = Some(82) {}`
100 ConstReference(NameDefinition), 101 ConstReference(Definition),
101} 102}
102 103
103impl NameClass { 104impl NameClass {
104 pub fn into_definition(self) -> Option<NameDefinition> { 105 pub fn into_definition(self) -> Option<Definition> {
105 match self { 106 match self {
106 NameClass::NameDefinition(it) => Some(it), 107 NameClass::Definition(it) => Some(it),
107 NameClass::ConstReference(_) => None, 108 NameClass::ConstReference(_) => None,
108 } 109 }
109 } 110 }
110 111
111 pub fn definition(self) -> NameDefinition { 112 pub fn definition(self) -> Definition {
112 match self { 113 match self {
113 NameClass::NameDefinition(it) | NameClass::ConstReference(it) => it, 114 NameClass::Definition(it) | NameClass::ConstReference(it) => it,
114 } 115 }
115 } 116 }
116} 117}
@@ -118,14 +119,14 @@ impl NameClass {
118pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameClass> { 119pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameClass> {
119 if let Some(bind_pat) = name.syntax().parent().and_then(ast::BindPat::cast) { 120 if let Some(bind_pat) = name.syntax().parent().and_then(ast::BindPat::cast) {
120 if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) { 121 if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) {
121 return Some(NameClass::ConstReference(NameDefinition::ModuleDef(def))); 122 return Some(NameClass::ConstReference(Definition::ModuleDef(def)));
122 } 123 }
123 } 124 }
124 125
125 classify_name_inner(sema, name).map(NameClass::NameDefinition) 126 classify_name_inner(sema, name).map(NameClass::Definition)
126} 127}
127 128
128fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameDefinition> { 129fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<Definition> {
129 let _p = profile("classify_name"); 130 let _p = profile("classify_name");
130 let parent = name.syntax().parent()?; 131 let parent = name.syntax().parent()?;
131 132
@@ -133,59 +134,59 @@ fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Opti
133 match parent { 134 match parent {
134 ast::BindPat(it) => { 135 ast::BindPat(it) => {
135 let local = sema.to_def(&it)?; 136 let local = sema.to_def(&it)?;
136 Some(NameDefinition::Local(local)) 137 Some(Definition::Local(local))
137 }, 138 },
138 ast::RecordFieldDef(it) => { 139 ast::RecordFieldDef(it) => {
139 let field: hir::StructField = sema.to_def(&it)?; 140 let field: hir::StructField = sema.to_def(&it)?;
140 Some(NameDefinition::StructField(field)) 141 Some(Definition::StructField(field))
141 }, 142 },
142 ast::Module(it) => { 143 ast::Module(it) => {
143 let def = sema.to_def(&it)?; 144 let def = sema.to_def(&it)?;
144 Some(NameDefinition::ModuleDef(def.into())) 145 Some(Definition::ModuleDef(def.into()))
145 }, 146 },
146 ast::StructDef(it) => { 147 ast::StructDef(it) => {
147 let def: hir::Struct = sema.to_def(&it)?; 148 let def: hir::Struct = sema.to_def(&it)?;
148 Some(NameDefinition::ModuleDef(def.into())) 149 Some(Definition::ModuleDef(def.into()))
149 }, 150 },
150 ast::UnionDef(it) => { 151 ast::UnionDef(it) => {
151 let def: hir::Union = sema.to_def(&it)?; 152 let def: hir::Union = sema.to_def(&it)?;
152 Some(NameDefinition::ModuleDef(def.into())) 153 Some(Definition::ModuleDef(def.into()))
153 }, 154 },
154 ast::EnumDef(it) => { 155 ast::EnumDef(it) => {
155 let def: hir::Enum = sema.to_def(&it)?; 156 let def: hir::Enum = sema.to_def(&it)?;
156 Some(NameDefinition::ModuleDef(def.into())) 157 Some(Definition::ModuleDef(def.into()))
157 }, 158 },
158 ast::TraitDef(it) => { 159 ast::TraitDef(it) => {
159 let def: hir::Trait = sema.to_def(&it)?; 160 let def: hir::Trait = sema.to_def(&it)?;
160 Some(NameDefinition::ModuleDef(def.into())) 161 Some(Definition::ModuleDef(def.into()))
161 }, 162 },
162 ast::StaticDef(it) => { 163 ast::StaticDef(it) => {
163 let def: hir::Static = sema.to_def(&it)?; 164 let def: hir::Static = sema.to_def(&it)?;
164 Some(NameDefinition::ModuleDef(def.into())) 165 Some(Definition::ModuleDef(def.into()))
165 }, 166 },
166 ast::EnumVariant(it) => { 167 ast::EnumVariant(it) => {
167 let def: hir::EnumVariant = sema.to_def(&it)?; 168 let def: hir::EnumVariant = sema.to_def(&it)?;
168 Some(NameDefinition::ModuleDef(def.into())) 169 Some(Definition::ModuleDef(def.into()))
169 }, 170 },
170 ast::FnDef(it) => { 171 ast::FnDef(it) => {
171 let def: hir::Function = sema.to_def(&it)?; 172 let def: hir::Function = sema.to_def(&it)?;
172 Some(NameDefinition::ModuleDef(def.into())) 173 Some(Definition::ModuleDef(def.into()))
173 }, 174 },
174 ast::ConstDef(it) => { 175 ast::ConstDef(it) => {
175 let def: hir::Const = sema.to_def(&it)?; 176 let def: hir::Const = sema.to_def(&it)?;
176 Some(NameDefinition::ModuleDef(def.into())) 177 Some(Definition::ModuleDef(def.into()))
177 }, 178 },
178 ast::TypeAliasDef(it) => { 179 ast::TypeAliasDef(it) => {
179 let def: hir::TypeAlias = sema.to_def(&it)?; 180 let def: hir::TypeAlias = sema.to_def(&it)?;
180 Some(NameDefinition::ModuleDef(def.into())) 181 Some(Definition::ModuleDef(def.into()))
181 }, 182 },
182 ast::MacroCall(it) => { 183 ast::MacroCall(it) => {
183 let def = sema.to_def(&it)?; 184 let def = sema.to_def(&it)?;
184 Some(NameDefinition::Macro(def)) 185 Some(Definition::Macro(def))
185 }, 186 },
186 ast::TypeParam(it) => { 187 ast::TypeParam(it) => {
187 let def = sema.to_def(&it)?; 188 let def = sema.to_def(&it)?;
188 Some(NameDefinition::TypeParam(def)) 189 Some(Definition::TypeParam(def))
189 }, 190 },
190 _ => None, 191 _ => None,
191 } 192 }
diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs
index e5fc3c470..c96351982 100644
--- a/crates/ra_ide_db/src/imports_locator.rs
+++ b/crates/ra_ide_db/src/imports_locator.rs
@@ -6,7 +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, NameDefinition}, 9 defs::{classify_name, Definition},
10 symbol_index::{self, FileSymbol, Query}, 10 symbol_index::{self, FileSymbol, Query},
11 RootDatabase, 11 RootDatabase,
12}; 12};
@@ -43,13 +43,13 @@ impl<'a> ImportsLocator<'a> {
43 .chain(lib_results.into_iter()) 43 .chain(lib_results.into_iter())
44 .filter_map(|import_candidate| self.get_name_definition(&import_candidate)) 44 .filter_map(|import_candidate| self.get_name_definition(&import_candidate))
45 .filter_map(|name_definition_to_import| match name_definition_to_import { 45 .filter_map(|name_definition_to_import| match name_definition_to_import {
46 NameDefinition::ModuleDef(module_def) => Some(module_def), 46 Definition::ModuleDef(module_def) => Some(module_def),
47 _ => None, 47 _ => None,
48 }) 48 })
49 .collect() 49 .collect()
50 } 50 }
51 51
52 fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<NameDefinition> { 52 fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<Definition> {
53 let _p = profile("get_name_definition"); 53 let _p = profile("get_name_definition");
54 let file_id = import_candidate.file_id; 54 let file_id = import_candidate.file_id;
55 55