aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/adt.rs23
-rw-r--r--crates/ra_hir/src/code_model_api.rs73
-rw-r--r--crates/ra_hir/src/code_model_impl/module.rs4
-rw-r--r--crates/ra_hir/src/db.rs6
-rw-r--r--crates/ra_hir/src/expr.rs4
-rw-r--r--crates/ra_hir/src/ids.rs7
-rw-r--r--crates/ra_hir/src/ty.rs14
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs4
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs4
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs2
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs6
-rw-r--r--crates/ra_ide_api/src/navigation_target.rs48
-rw-r--r--crates/ra_ide_api/src/parent_module.rs2
13 files changed, 82 insertions, 115 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs
index bcb705c24..57d112f74 100644
--- a/crates/ra_hir/src/adt.rs
+++ b/crates/ra_hir/src/adt.rs
@@ -19,7 +19,7 @@ impl Struct {
19 } 19 }
20 20
21 pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> { 21 pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
22 Ok(db.struct_data(self.def_id)?.variant_data.clone()) 22 Ok(db.struct_data(self.def_id).variant_data.clone())
23 } 23 }
24} 24}
25 25
@@ -37,16 +37,13 @@ impl StructData {
37 StructData { name, variant_data } 37 StructData { name, variant_data }
38 } 38 }
39 39
40 pub(crate) fn struct_data_query( 40 pub(crate) fn struct_data_query(db: &impl HirDatabase, def_id: DefId) -> Arc<StructData> {
41 db: &impl HirDatabase,
42 def_id: DefId,
43 ) -> Cancelable<Arc<StructData>> {
44 let def_loc = def_id.loc(db); 41 let def_loc = def_id.loc(db);
45 assert!(def_loc.kind == DefKind::Struct); 42 assert!(def_loc.kind == DefKind::Struct);
46 let syntax = db.file_item(def_loc.source_item_id); 43 let syntax = db.file_item(def_loc.source_item_id);
47 let struct_def = 44 let struct_def =
48 ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node"); 45 ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node");
49 Ok(Arc::new(StructData::new(struct_def))) 46 Arc::new(StructData::new(struct_def))
50 } 47 }
51} 48}
52 49
@@ -84,10 +81,7 @@ impl EnumData {
84 EnumData { name, variants } 81 EnumData { name, variants }
85 } 82 }
86 83
87 pub(crate) fn enum_data_query( 84 pub(crate) fn enum_data_query(db: &impl HirDatabase, def_id: DefId) -> Arc<EnumData> {
88 db: &impl HirDatabase,
89 def_id: DefId,
90 ) -> Cancelable<Arc<EnumData>> {
91 let def_loc = def_id.loc(db); 85 let def_loc = def_id.loc(db);
92 assert!(def_loc.kind == DefKind::Enum); 86 assert!(def_loc.kind == DefKind::Enum);
93 let syntax = db.file_item(def_loc.source_item_id); 87 let syntax = db.file_item(def_loc.source_item_id);
@@ -107,7 +101,7 @@ impl EnumData {
107 } else { 101 } else {
108 Vec::new() 102 Vec::new()
109 }; 103 };
110 Ok(Arc::new(EnumData::new(enum_def, variants))) 104 Arc::new(EnumData::new(enum_def, variants))
111 } 105 }
112} 106}
113 107
@@ -133,7 +127,7 @@ impl EnumVariantData {
133 pub(crate) fn enum_variant_data_query( 127 pub(crate) fn enum_variant_data_query(
134 db: &impl HirDatabase, 128 db: &impl HirDatabase,
135 def_id: DefId, 129 def_id: DefId,
136 ) -> Cancelable<Arc<EnumVariantData>> { 130 ) -> Arc<EnumVariantData> {
137 let def_loc = def_id.loc(db); 131 let def_loc = def_id.loc(db);
138 assert!(def_loc.kind == DefKind::EnumVariant); 132 assert!(def_loc.kind == DefKind::EnumVariant);
139 let syntax = db.file_item(def_loc.source_item_id); 133 let syntax = db.file_item(def_loc.source_item_id);
@@ -146,10 +140,7 @@ impl EnumVariantData {
146 .expect("enum variant list should have enum ancestor"); 140 .expect("enum variant list should have enum ancestor");
147 let enum_def_id = get_def_id(db, &def_loc, enum_node, DefKind::Enum); 141 let enum_def_id = get_def_id(db, &def_loc, enum_node, DefKind::Enum);
148 142
149 Ok(Arc::new(EnumVariantData::new( 143 Arc::new(EnumVariantData::new(variant_def, Enum::new(enum_def_id)))
150 variant_def,
151 Enum::new(enum_def_id),
152 )))
153 } 144 }
154} 145}
155 146
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index db270b871..f28e077c6 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -175,13 +175,12 @@ impl Struct {
175 self.def_id 175 self.def_id
176 } 176 }
177 177
178 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { 178 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
179 Ok(db.struct_data(self.def_id)?.name.clone()) 179 db.struct_data(self.def_id).name.clone()
180 } 180 }
181 181
182 pub fn fields(&self, db: &impl HirDatabase) -> Cancelable<Vec<StructField>> { 182 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
183 let res = db 183 db.struct_data(self.def_id)
184 .struct_data(self.def_id)?
185 .variant_data 184 .variant_data
186 .fields() 185 .fields()
187 .iter() 186 .iter()
@@ -189,15 +188,11 @@ impl Struct {
189 struct_: self.clone(), 188 struct_: self.clone(),
190 name: it.name.clone(), 189 name: it.name.clone(),
191 }) 190 })
192 .collect(); 191 .collect()
193 Ok(res)
194 } 192 }
195 193
196 pub fn source( 194 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) {
197 &self, 195 def_id_to_ast(db, self.def_id)
198 db: &impl HirDatabase,
199 ) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
200 Ok(def_id_to_ast(db, self.def_id))
201 } 196 }
202} 197}
203 198
@@ -215,16 +210,16 @@ impl Enum {
215 self.def_id 210 self.def_id
216 } 211 }
217 212
218 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { 213 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
219 Ok(db.enum_data(self.def_id)?.name.clone()) 214 db.enum_data(self.def_id).name.clone()
220 } 215 }
221 216
222 pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, EnumVariant)>> { 217 pub fn variants(&self, db: &impl HirDatabase) -> Vec<(Name, EnumVariant)> {
223 Ok(db.enum_data(self.def_id)?.variants.clone()) 218 db.enum_data(self.def_id).variants.clone()
224 } 219 }
225 220
226 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> { 221 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) {
227 Ok(def_id_to_ast(db, self.def_id)) 222 def_id_to_ast(db, self.def_id)
228 } 223 }
229} 224}
230 225
@@ -242,23 +237,20 @@ impl EnumVariant {
242 self.def_id 237 self.def_id
243 } 238 }
244 239
245 pub fn parent_enum(&self, db: &impl HirDatabase) -> Cancelable<Enum> { 240 pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum {
246 Ok(db.enum_variant_data(self.def_id)?.parent_enum.clone()) 241 db.enum_variant_data(self.def_id).parent_enum.clone()
247 } 242 }
248 243
249 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { 244 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
250 Ok(db.enum_variant_data(self.def_id)?.name.clone()) 245 db.enum_variant_data(self.def_id).name.clone()
251 } 246 }
252 247
253 pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> { 248 pub fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
254 Ok(db.enum_variant_data(self.def_id)?.variant_data.clone()) 249 db.enum_variant_data(self.def_id).variant_data.clone()
255 } 250 }
256 251
257 pub fn source( 252 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
258 &self, 253 def_id_to_ast(db, self.def_id)
259 db: &impl HirDatabase,
260 ) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
261 Ok(def_id_to_ast(db, self.def_id))
262 } 254 }
263} 255}
264 256
@@ -305,8 +297,8 @@ impl Function {
305 self.def_id 297 self.def_id
306 } 298 }
307 299
308 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> { 300 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
309 Ok(def_id_to_ast(db, self.def_id)) 301 def_id_to_ast(db, self.def_id)
310 } 302 }
311 303
312 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> { 304 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
@@ -341,8 +333,8 @@ impl Const {
341 Const { def_id } 333 Const { def_id }
342 } 334 }
343 335
344 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::ConstDef>)> { 336 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
345 Ok(def_id_to_ast(db, self.def_id)) 337 def_id_to_ast(db, self.def_id)
346 } 338 }
347} 339}
348 340
@@ -356,11 +348,8 @@ impl Static {
356 Static { def_id } 348 Static { def_id }
357 } 349 }
358 350
359 pub fn source( 351 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
360 &self, 352 def_id_to_ast(db, self.def_id)
361 db: &impl HirDatabase,
362 ) -> Cancelable<(HirFileId, TreeArc<ast::StaticDef>)> {
363 Ok(def_id_to_ast(db, self.def_id))
364 } 353 }
365} 354}
366 355
@@ -374,8 +363,8 @@ impl Trait {
374 Trait { def_id } 363 Trait { def_id }
375 } 364 }
376 365
377 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TraitDef>)> { 366 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::TraitDef>) {
378 Ok(def_id_to_ast(db, self.def_id)) 367 def_id_to_ast(db, self.def_id)
379 } 368 }
380} 369}
381 370
@@ -389,7 +378,7 @@ impl Type {
389 Type { def_id } 378 Type { def_id }
390 } 379 }
391 380
392 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TypeDef>)> { 381 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::TypeDef>) {
393 Ok(def_id_to_ast(db, self.def_id)) 382 def_id_to_ast(db, self.def_id)
394 } 383 }
395} 384}
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs
index 8668d6c8a..331b0500e 100644
--- a/crates/ra_hir/src/code_model_impl/module.rs
+++ b/crates/ra_hir/src/code_model_impl/module.rs
@@ -148,13 +148,13 @@ impl Module {
148 } else { 148 } else {
149 return Ok(PerNs::none()); 149 return Ok(PerNs::none());
150 }; 150 };
151 let module = match curr.resolve(db)? { 151 let module = match curr.resolve(db) {
152 Def::Module(it) => it, 152 Def::Module(it) => it,
153 Def::Enum(e) => { 153 Def::Enum(e) => {
154 if segments.len() == idx + 1 { 154 if segments.len() == idx + 1 {
155 // enum variant 155 // enum variant
156 let matching_variant = 156 let matching_variant =
157 e.variants(db)?.into_iter().find(|(n, _variant)| n == name); 157 e.variants(db).into_iter().find(|(n, _variant)| n == name);
158 158
159 if let Some((_n, variant)) = matching_variant { 159 if let Some((_n, variant)) = matching_variant {
160 return Ok(PerNs::both(variant.def_id(), e.def_id())); 160 return Ok(PerNs::both(variant.def_id(), e.def_id()));
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 3b2498d5a..6229f9778 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -37,17 +37,17 @@ pub trait HirDatabase: SyntaxDatabase
37 use fn query_definitions::fn_scopes; 37 use fn query_definitions::fn_scopes;
38 } 38 }
39 39
40 fn struct_data(def_id: DefId) -> Cancelable<Arc<StructData>> { 40 fn struct_data(def_id: DefId) -> Arc<StructData> {
41 type StructDataQuery; 41 type StructDataQuery;
42 use fn crate::adt::StructData::struct_data_query; 42 use fn crate::adt::StructData::struct_data_query;
43 } 43 }
44 44
45 fn enum_data(def_id: DefId) -> Cancelable<Arc<EnumData>> { 45 fn enum_data(def_id: DefId) -> Arc<EnumData> {
46 type EnumDataQuery; 46 type EnumDataQuery;
47 use fn crate::adt::EnumData::enum_data_query; 47 use fn crate::adt::EnumData::enum_data_query;
48 } 48 }
49 49
50 fn enum_variant_data(def_id: DefId) -> Cancelable<Arc<EnumVariantData>> { 50 fn enum_variant_data(def_id: DefId) -> Arc<EnumVariantData> {
51 type EnumVariantDataQuery; 51 type EnumVariantDataQuery;
52 use fn crate::adt::EnumVariantData::enum_variant_data_query; 52 use fn crate::adt::EnumVariantData::enum_variant_data_query;
53 } 53 }
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 5081466a2..a1e8da348 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -832,10 +832,10 @@ pub(crate) fn body_syntax_mapping(
832 db: &impl HirDatabase, 832 db: &impl HirDatabase,
833 def_id: DefId, 833 def_id: DefId,
834) -> Cancelable<Arc<BodySyntaxMapping>> { 834) -> Cancelable<Arc<BodySyntaxMapping>> {
835 let def = def_id.resolve(db)?; 835 let def = def_id.resolve(db);
836 836
837 let body_syntax_mapping = match def { 837 let body_syntax_mapping = match def {
838 Def::Function(f) => collect_fn_body_syntax(&f.source(db)?.1), 838 Def::Function(f) => collect_fn_body_syntax(&f.source(db).1),
839 // TODO: consts, etc. 839 // TODO: consts, etc.
840 _ => panic!("Trying to get body for item type without body"), 840 _ => panic!("Trying to get body for item type without body"),
841 }; 841 };
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 7b572061a..3cbf8070f 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -159,9 +159,9 @@ impl DefId {
159 db.as_ref().id2loc(self) 159 db.as_ref().id2loc(self)
160 } 160 }
161 161
162 pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> { 162 pub fn resolve(self, db: &impl HirDatabase) -> Def {
163 let loc = self.loc(db); 163 let loc = self.loc(db);
164 let res = match loc.kind { 164 match loc.kind {
165 DefKind::Module => { 165 DefKind::Module => {
166 let module = Module::from_module_id(db, loc.source_root_id, loc.module_id); 166 let module = Module::from_module_id(db, loc.source_root_id, loc.module_id);
167 Def::Module(module) 167 Def::Module(module)
@@ -195,8 +195,7 @@ impl DefId {
195 195
196 DefKind::StructCtor => Def::Item, 196 DefKind::StructCtor => Def::Item,
197 DefKind::Item => Def::Item, 197 DefKind::Item => Def::Item,
198 }; 198 }
199 Ok(res)
200 } 199 }
201 200
202 pub(crate) fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc<SyntaxNode>) { 201 pub(crate) fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc<SyntaxNode>) {
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 03787bd89..3607969ed 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -463,25 +463,25 @@ fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable<Ty> {
463fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> { 463fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> {
464 Ok(Ty::Adt { 464 Ok(Ty::Adt {
465 def_id: s.def_id(), 465 def_id: s.def_id(),
466 name: s.name(db)?.unwrap_or_else(Name::missing), 466 name: s.name(db).unwrap_or_else(Name::missing),
467 }) 467 })
468} 468}
469 469
470pub(crate) fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> { 470pub(crate) fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> {
471 Ok(Ty::Adt { 471 Ok(Ty::Adt {
472 def_id: s.def_id(), 472 def_id: s.def_id(),
473 name: s.name(db)?.unwrap_or_else(Name::missing), 473 name: s.name(db).unwrap_or_else(Name::missing),
474 }) 474 })
475} 475}
476 476
477pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> Cancelable<Ty> { 477pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> Cancelable<Ty> {
478 let enum_parent = ev.parent_enum(db)?; 478 let enum_parent = ev.parent_enum(db);
479 479
480 type_for_enum(db, enum_parent) 480 type_for_enum(db, enum_parent)
481} 481}
482 482
483pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> { 483pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> {
484 let def = def_id.resolve(db)?; 484 let def = def_id.resolve(db);
485 match def { 485 match def {
486 Def::Module(..) => { 486 Def::Module(..) => {
487 log::debug!("trying to get type for module {:?}", def_id); 487 log::debug!("trying to get type for module {:?}", def_id);
@@ -507,10 +507,10 @@ pub(super) fn type_for_field(
507 def_id: DefId, 507 def_id: DefId,
508 field: Name, 508 field: Name,
509) -> Cancelable<Option<Ty>> { 509) -> Cancelable<Option<Ty>> {
510 let def = def_id.resolve(db)?; 510 let def = def_id.resolve(db);
511 let variant_data = match def { 511 let variant_data = match def {
512 Def::Struct(s) => s.variant_data(db)?, 512 Def::Struct(s) => s.variant_data(db)?,
513 Def::EnumVariant(ev) => ev.variant_data(db)?, 513 Def::EnumVariant(ev) => ev.variant_data(db),
514 // TODO: unions 514 // TODO: unions
515 _ => panic!( 515 _ => panic!(
516 "trying to get type for field in non-struct/variant {:?}", 516 "trying to get type for field in non-struct/variant {:?}",
@@ -877,7 +877,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
877 } else { 877 } else {
878 return Ok((Ty::Unknown, None)); 878 return Ok((Ty::Unknown, None));
879 }; 879 };
880 Ok(match def_id.resolve(self.db)? { 880 Ok(match def_id.resolve(self.db) {
881 Def::Struct(s) => { 881 Def::Struct(s) => {
882 let ty = type_for_struct(self.db, s)?; 882 let ty = type_for_struct(self.db, s)?;
883 (ty, Some(def_id)) 883 (ty, Some(def_id))
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index 37985b398..886dc54d4 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -27,9 +27,9 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
27 for receiver in receiver.autoderef(ctx.db) { 27 for receiver in receiver.autoderef(ctx.db) {
28 match receiver { 28 match receiver {
29 Ty::Adt { def_id, .. } => { 29 Ty::Adt { def_id, .. } => {
30 match def_id.resolve(ctx.db)? { 30 match def_id.resolve(ctx.db) {
31 Def::Struct(s) => { 31 Def::Struct(s) => {
32 for field in s.fields(ctx.db)? { 32 for field in s.fields(ctx.db) {
33 CompletionItem::new( 33 CompletionItem::new(
34 CompletionKind::Reference, 34 CompletionKind::Reference,
35 field.name().to_string(), 35 field.name().to_string(),
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index a25ad3f13..207a509b3 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -12,7 +12,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
12 Some(it) => it, 12 Some(it) => it,
13 None => return Ok(()), 13 None => return Ok(()),
14 }; 14 };
15 match def_id.resolve(ctx.db)? { 15 match def_id.resolve(ctx.db) {
16 hir::Def::Module(module) => { 16 hir::Def::Module(module) => {
17 let module_scope = module.scope(ctx.db)?; 17 let module_scope = module.scope(ctx.db)?;
18 for (name, res) in module_scope.entries() { 18 for (name, res) in module_scope.entries() {
@@ -22,7 +22,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
22 } 22 }
23 } 23 }
24 hir::Def::Enum(e) => { 24 hir::Def::Enum(e) => {
25 e.variants(ctx.db)? 25 e.variants(ctx.db)
26 .into_iter() 26 .into_iter()
27 .for_each(|(variant_name, _variant)| { 27 .for_each(|(variant_name, _variant)| {
28 CompletionItem::new(CompletionKind::Reference, variant_name.to_string()) 28 CompletionItem::new(CompletionKind::Reference, variant_name.to_string())
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index b75d65de3..11d00f78c 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -144,7 +144,7 @@ impl Builder {
144 ctx: &CompletionContext, 144 ctx: &CompletionContext,
145 resolution: &hir::Resolution, 145 resolution: &hir::Resolution,
146 ) -> Builder { 146 ) -> Builder {
147 let resolved = resolution.def_id.and_then(|d| d.resolve(ctx.db).ok()); 147 let resolved = resolution.def_id.map(|d| d.resolve(ctx.db));
148 let kind = match resolved { 148 let kind = match resolved {
149 PerNs { 149 PerNs {
150 types: Some(hir::Def::Module(..)), 150 types: Some(hir::Def::Module(..)),
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 591f36cce..7229293a4 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -70,7 +70,7 @@ pub(crate) fn reference_definition(
70 .node_expr(expr) 70 .node_expr(expr)
71 .and_then(|it| infer_result.method_resolution(it)) 71 .and_then(|it| infer_result.method_resolution(it))
72 { 72 {
73 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)?)? { 73 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) {
74 return Ok(Exact(target)); 74 return Ok(Exact(target));
75 } 75 }
76 }; 76 };
@@ -87,7 +87,7 @@ pub(crate) fn reference_definition(
87 { 87 {
88 let resolved = module.resolve_path(db, &path)?; 88 let resolved = module.resolve_path(db, &path)?;
89 if let Some(def_id) = resolved.take_types().or(resolved.take_values()) { 89 if let Some(def_id) = resolved.take_types().or(resolved.take_values()) {
90 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)?)? { 90 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) {
91 return Ok(Exact(target)); 91 return Ok(Exact(target));
92 } 92 }
93 } 93 }
@@ -112,7 +112,7 @@ fn name_definition(
112 if let Some(child_module) = 112 if let Some(child_module) =
113 hir::source_binder::module_from_declaration(db, file_id, module) 113 hir::source_binder::module_from_declaration(db, file_id, module)
114 { 114 {
115 let nav = NavigationTarget::from_module(db, child_module)?; 115 let nav = NavigationTarget::from_module(db, child_module);
116 return Ok(Some(vec![nav])); 116 return Ok(Some(vec![nav]));
117 } 117 }
118 } 118 }
diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs
index 7562b9a1f..21c15c0c0 100644
--- a/crates/ra_ide_api/src/navigation_target.rs
+++ b/crates/ra_ide_api/src/navigation_target.rs
@@ -1,4 +1,4 @@
1use ra_db::{FileId, Cancelable}; 1use ra_db::FileId;
2use ra_syntax::{ 2use ra_syntax::{
3 SyntaxNode, AstNode, SmolStr, TextRange, ast, 3 SyntaxNode, AstNode, SmolStr, TextRange, ast,
4 SyntaxKind::{self, NAME}, 4 SyntaxKind::{self, NAME},
@@ -69,84 +69,72 @@ impl NavigationTarget {
69 } 69 }
70 } 70 }
71 71
72 pub(crate) fn from_module( 72 pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
73 db: &RootDatabase,
74 module: hir::Module,
75 ) -> Cancelable<NavigationTarget> {
76 let (file_id, source) = module.definition_source(db); 73 let (file_id, source) = module.definition_source(db);
77 let name = module 74 let name = module
78 .name(db) 75 .name(db)
79 .map(|it| it.to_string().into()) 76 .map(|it| it.to_string().into())
80 .unwrap_or_default(); 77 .unwrap_or_default();
81 let res = match source { 78 match source {
82 ModuleSource::SourceFile(node) => { 79 ModuleSource::SourceFile(node) => {
83 NavigationTarget::from_syntax(file_id, name, None, node.syntax()) 80 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
84 } 81 }
85 ModuleSource::Module(node) => { 82 ModuleSource::Module(node) => {
86 NavigationTarget::from_syntax(file_id, name, None, node.syntax()) 83 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
87 } 84 }
88 }; 85 }
89 Ok(res)
90 } 86 }
91 87
92 pub(crate) fn from_module_to_decl( 88 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
93 db: &RootDatabase,
94 module: hir::Module,
95 ) -> Cancelable<NavigationTarget> {
96 let name = module 89 let name = module
97 .name(db) 90 .name(db)
98 .map(|it| it.to_string().into()) 91 .map(|it| it.to_string().into())
99 .unwrap_or_default(); 92 .unwrap_or_default();
100 if let Some((file_id, source)) = module.declaration_source(db) { 93 if let Some((file_id, source)) = module.declaration_source(db) {
101 return Ok(NavigationTarget::from_syntax( 94 return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
102 file_id,
103 name,
104 None,
105 source.syntax(),
106 ));
107 } 95 }
108 NavigationTarget::from_module(db, module) 96 NavigationTarget::from_module(db, module)
109 } 97 }
110 98
111 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget 99 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
112 pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> { 100 pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Option<NavigationTarget> {
113 let res = match def { 101 let res = match def {
114 Def::Struct(s) => { 102 Def::Struct(s) => {
115 let (file_id, node) = s.source(db)?; 103 let (file_id, node) = s.source(db);
116 NavigationTarget::from_named(file_id.original_file(db), &*node) 104 NavigationTarget::from_named(file_id.original_file(db), &*node)
117 } 105 }
118 Def::Enum(e) => { 106 Def::Enum(e) => {
119 let (file_id, node) = e.source(db)?; 107 let (file_id, node) = e.source(db);
120 NavigationTarget::from_named(file_id.original_file(db), &*node) 108 NavigationTarget::from_named(file_id.original_file(db), &*node)
121 } 109 }
122 Def::EnumVariant(ev) => { 110 Def::EnumVariant(ev) => {
123 let (file_id, node) = ev.source(db)?; 111 let (file_id, node) = ev.source(db);
124 NavigationTarget::from_named(file_id.original_file(db), &*node) 112 NavigationTarget::from_named(file_id.original_file(db), &*node)
125 } 113 }
126 Def::Function(f) => { 114 Def::Function(f) => {
127 let (file_id, node) = f.source(db)?; 115 let (file_id, node) = f.source(db);
128 NavigationTarget::from_named(file_id.original_file(db), &*node) 116 NavigationTarget::from_named(file_id.original_file(db), &*node)
129 } 117 }
130 Def::Trait(f) => { 118 Def::Trait(f) => {
131 let (file_id, node) = f.source(db)?; 119 let (file_id, node) = f.source(db);
132 NavigationTarget::from_named(file_id.original_file(db), &*node) 120 NavigationTarget::from_named(file_id.original_file(db), &*node)
133 } 121 }
134 Def::Type(f) => { 122 Def::Type(f) => {
135 let (file_id, node) = f.source(db)?; 123 let (file_id, node) = f.source(db);
136 NavigationTarget::from_named(file_id.original_file(db), &*node) 124 NavigationTarget::from_named(file_id.original_file(db), &*node)
137 } 125 }
138 Def::Static(f) => { 126 Def::Static(f) => {
139 let (file_id, node) = f.source(db)?; 127 let (file_id, node) = f.source(db);
140 NavigationTarget::from_named(file_id.original_file(db), &*node) 128 NavigationTarget::from_named(file_id.original_file(db), &*node)
141 } 129 }
142 Def::Const(f) => { 130 Def::Const(f) => {
143 let (file_id, node) = f.source(db)?; 131 let (file_id, node) = f.source(db);
144 NavigationTarget::from_named(file_id.original_file(db), &*node) 132 NavigationTarget::from_named(file_id.original_file(db), &*node)
145 } 133 }
146 Def::Module(m) => NavigationTarget::from_module(db, m)?, 134 Def::Module(m) => NavigationTarget::from_module(db, m),
147 Def::Item => return Ok(None), 135 Def::Item => return None,
148 }; 136 };
149 Ok(Some(res)) 137 Some(res)
150 } 138 }
151 139
152 #[cfg(test)] 140 #[cfg(test)]
diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs
index 451304739..379b3f3a4 100644
--- a/crates/ra_ide_api/src/parent_module.rs
+++ b/crates/ra_ide_api/src/parent_module.rs
@@ -12,7 +12,7 @@ pub(crate) fn parent_module(
12 None => return Ok(Vec::new()), 12 None => return Ok(Vec::new()),
13 Some(it) => it, 13 Some(it) => it,
14 }; 14 };
15 let nav = NavigationTarget::from_module_to_decl(db, module)?; 15 let nav = NavigationTarget::from_module_to_decl(db, module);
16 Ok(vec![nav]) 16 Ok(vec![nav])
17} 17}
18 18