aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/display
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/display')
-rw-r--r--crates/ide/src/display/navigation_target.rs137
1 files changed, 74 insertions, 63 deletions
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs
index 6431e7d6d..e24c78301 100644
--- a/crates/ide/src/display/navigation_target.rs
+++ b/crates/ide/src/display/navigation_target.rs
@@ -24,6 +24,7 @@ pub enum SymbolKind {
24 Impl, 24 Impl,
25 Field, 25 Field,
26 TypeParam, 26 TypeParam,
27 ConstParam,
27 LifetimeParam, 28 LifetimeParam,
28 ValueParam, 29 ValueParam,
29 SelfParam, 30 SelfParam,
@@ -209,40 +210,32 @@ impl ToNav for FileSymbol {
209impl TryToNav for Definition { 210impl TryToNav for Definition {
210 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { 211 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
211 match self { 212 match self {
212 Definition::Macro(it) => { 213 Definition::Macro(it) => it.try_to_nav(db),
213 // FIXME: Currently proc-macro do not have ast-node, 214 Definition::Field(it) => it.try_to_nav(db),
214 // such that it does not have source
215 // more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913
216 if it.is_proc_macro() {
217 return None;
218 }
219 Some(it.to_nav(db))
220 }
221 Definition::Field(it) => Some(it.to_nav(db)),
222 Definition::ModuleDef(it) => it.try_to_nav(db), 215 Definition::ModuleDef(it) => it.try_to_nav(db),
223 Definition::SelfType(it) => Some(it.to_nav(db)), 216 Definition::SelfType(it) => it.try_to_nav(db),
224 Definition::Local(it) => Some(it.to_nav(db)), 217 Definition::Local(it) => Some(it.to_nav(db)),
225 Definition::TypeParam(it) => Some(it.to_nav(db)), 218 Definition::TypeParam(it) => it.try_to_nav(db),
226 Definition::LifetimeParam(it) => Some(it.to_nav(db)), 219 Definition::LifetimeParam(it) => it.try_to_nav(db),
227 Definition::Label(it) => Some(it.to_nav(db)), 220 Definition::Label(it) => Some(it.to_nav(db)),
221 Definition::ConstParam(it) => it.try_to_nav(db),
228 } 222 }
229 } 223 }
230} 224}
231 225
232impl TryToNav for hir::ModuleDef { 226impl TryToNav for hir::ModuleDef {
233 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { 227 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
234 let res = match self { 228 match self {
235 hir::ModuleDef::Module(it) => it.to_nav(db), 229 hir::ModuleDef::Module(it) => Some(it.to_nav(db)),
236 hir::ModuleDef::Function(it) => it.to_nav(db), 230 hir::ModuleDef::Function(it) => it.try_to_nav(db),
237 hir::ModuleDef::Adt(it) => it.to_nav(db), 231 hir::ModuleDef::Adt(it) => it.try_to_nav(db),
238 hir::ModuleDef::Variant(it) => it.to_nav(db), 232 hir::ModuleDef::Variant(it) => it.try_to_nav(db),
239 hir::ModuleDef::Const(it) => it.to_nav(db), 233 hir::ModuleDef::Const(it) => it.try_to_nav(db),
240 hir::ModuleDef::Static(it) => it.to_nav(db), 234 hir::ModuleDef::Static(it) => it.try_to_nav(db),
241 hir::ModuleDef::Trait(it) => it.to_nav(db), 235 hir::ModuleDef::Trait(it) => it.try_to_nav(db),
242 hir::ModuleDef::TypeAlias(it) => it.to_nav(db), 236 hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db),
243 hir::ModuleDef::BuiltinType(_) => return None, 237 hir::ModuleDef::BuiltinType(_) => None,
244 }; 238 }
245 Some(res)
246 } 239 }
247} 240}
248 241
@@ -277,13 +270,13 @@ impl ToNavFromAst for hir::Trait {
277 const KIND: SymbolKind = SymbolKind::Trait; 270 const KIND: SymbolKind = SymbolKind::Trait;
278} 271}
279 272
280impl<D> ToNav for D 273impl<D> TryToNav for D
281where 274where
282 D: HasSource + ToNavFromAst + Copy + HasAttrs, 275 D: HasSource + ToNavFromAst + Copy + HasAttrs,
283 D::Ast: ast::NameOwner + ShortLabel, 276 D::Ast: ast::NameOwner + ShortLabel,
284{ 277{
285 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 278 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
286 let src = self.source(db); 279 let src = self.source(db)?;
287 let mut res = NavigationTarget::from_named( 280 let mut res = NavigationTarget::from_named(
288 db, 281 db,
289 src.as_ref().map(|it| it as &dyn ast::NameOwner), 282 src.as_ref().map(|it| it as &dyn ast::NameOwner),
@@ -291,7 +284,7 @@ where
291 ); 284 );
292 res.docs = self.docs(db); 285 res.docs = self.docs(db);
293 res.description = src.value.short_label(); 286 res.description = src.value.short_label();
294 res 287 Some(res)
295 } 288 }
296} 289}
297 290
@@ -310,9 +303,9 @@ impl ToNav for hir::Module {
310 } 303 }
311} 304}
312 305
313impl ToNav for hir::Impl { 306impl TryToNav for hir::Impl {
314 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 307 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
315 let src = self.source(db); 308 let src = self.source(db)?;
316 let derive_attr = self.is_builtin_derive(db); 309 let derive_attr = self.is_builtin_derive(db);
317 let frange = if let Some(item) = &derive_attr { 310 let frange = if let Some(item) = &derive_attr {
318 item.syntax().original_file_range(db) 311 item.syntax().original_file_range(db)
@@ -325,21 +318,21 @@ impl ToNav for hir::Impl {
325 src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range) 318 src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range)
326 }; 319 };
327 320
328 NavigationTarget::from_syntax( 321 Some(NavigationTarget::from_syntax(
329 frange.file_id, 322 frange.file_id,
330 "impl".into(), 323 "impl".into(),
331 focus_range, 324 focus_range,
332 frange.range, 325 frange.range,
333 SymbolKind::Impl, 326 SymbolKind::Impl,
334 ) 327 ))
335 } 328 }
336} 329}
337 330
338impl ToNav for hir::Field { 331impl TryToNav for hir::Field {
339 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 332 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
340 let src = self.source(db); 333 let src = self.source(db)?;
341 334
342 match &src.value { 335 let field_source = match &src.value {
343 FieldSource::Named(it) => { 336 FieldSource::Named(it) => {
344 let mut res = 337 let mut res =
345 NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); 338 NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field);
@@ -357,13 +350,14 @@ impl ToNav for hir::Field {
357 SymbolKind::Field, 350 SymbolKind::Field,
358 ) 351 )
359 } 352 }
360 } 353 };
354 Some(field_source)
361 } 355 }
362} 356}
363 357
364impl ToNav for hir::MacroDef { 358impl TryToNav for hir::MacroDef {
365 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 359 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
366 let src = self.source(db); 360 let src = self.source(db)?;
367 log::debug!("nav target {:#?}", src.value.syntax()); 361 log::debug!("nav target {:#?}", src.value.syntax());
368 let mut res = NavigationTarget::from_named( 362 let mut res = NavigationTarget::from_named(
369 db, 363 db,
@@ -371,26 +365,26 @@ impl ToNav for hir::MacroDef {
371 SymbolKind::Macro, 365 SymbolKind::Macro,
372 ); 366 );
373 res.docs = self.docs(db); 367 res.docs = self.docs(db);
374 res 368 Some(res)
375 } 369 }
376} 370}
377 371
378impl ToNav for hir::Adt { 372impl TryToNav for hir::Adt {
379 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 373 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
380 match self { 374 match self {
381 hir::Adt::Struct(it) => it.to_nav(db), 375 hir::Adt::Struct(it) => it.try_to_nav(db),
382 hir::Adt::Union(it) => it.to_nav(db), 376 hir::Adt::Union(it) => it.try_to_nav(db),
383 hir::Adt::Enum(it) => it.to_nav(db), 377 hir::Adt::Enum(it) => it.try_to_nav(db),
384 } 378 }
385 } 379 }
386} 380}
387 381
388impl ToNav for hir::AssocItem { 382impl TryToNav for hir::AssocItem {
389 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 383 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
390 match self { 384 match self {
391 AssocItem::Function(it) => it.to_nav(db), 385 AssocItem::Function(it) => it.try_to_nav(db),
392 AssocItem::Const(it) => it.to_nav(db), 386 AssocItem::Const(it) => it.try_to_nav(db),
393 AssocItem::TypeAlias(it) => it.to_nav(db), 387 AssocItem::TypeAlias(it) => it.try_to_nav(db),
394 } 388 }
395 } 389 }
396} 390}
@@ -444,9 +438,9 @@ impl ToNav for hir::Label {
444 } 438 }
445} 439}
446 440
447impl ToNav for hir::TypeParam { 441impl TryToNav for hir::TypeParam {
448 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 442 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
449 let src = self.source(db); 443 let src = self.source(db)?;
450 let full_range = match &src.value { 444 let full_range = match &src.value {
451 Either::Left(it) => it.syntax().text_range(), 445 Either::Left(it) => it.syntax().text_range(),
452 Either::Right(it) => it.syntax().text_range(), 446 Either::Right(it) => it.syntax().text_range(),
@@ -455,7 +449,7 @@ impl ToNav for hir::TypeParam {
455 Either::Left(_) => None, 449 Either::Left(_) => None,
456 Either::Right(it) => it.name().map(|it| it.syntax().text_range()), 450 Either::Right(it) => it.name().map(|it| it.syntax().text_range()),
457 }; 451 };
458 NavigationTarget { 452 Some(NavigationTarget {
459 file_id: src.file_id.original_file(db), 453 file_id: src.file_id.original_file(db),
460 name: self.name(db).to_string().into(), 454 name: self.name(db).to_string().into(),
461 kind: Some(SymbolKind::TypeParam), 455 kind: Some(SymbolKind::TypeParam),
@@ -464,15 +458,15 @@ impl ToNav for hir::TypeParam {
464 container_name: None, 458 container_name: None,
465 description: None, 459 description: None,
466 docs: None, 460 docs: None,
467 } 461 })
468 } 462 }
469} 463}
470 464
471impl ToNav for hir::LifetimeParam { 465impl TryToNav for hir::LifetimeParam {
472 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 466 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
473 let src = self.source(db); 467 let src = self.source(db)?;
474 let full_range = src.value.syntax().text_range(); 468 let full_range = src.value.syntax().text_range();
475 NavigationTarget { 469 Some(NavigationTarget {
476 file_id: src.file_id.original_file(db), 470 file_id: src.file_id.original_file(db),
477 name: self.name(db).to_string().into(), 471 name: self.name(db).to_string().into(),
478 kind: Some(SymbolKind::LifetimeParam), 472 kind: Some(SymbolKind::LifetimeParam),
@@ -481,7 +475,24 @@ impl ToNav for hir::LifetimeParam {
481 container_name: None, 475 container_name: None,
482 description: None, 476 description: None,
483 docs: None, 477 docs: None,
484 } 478 })
479 }
480}
481
482impl TryToNav for hir::ConstParam {
483 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
484 let src = self.source(db)?;
485 let full_range = src.value.syntax().text_range();
486 Some(NavigationTarget {
487 file_id: src.file_id.original_file(db),
488 name: self.name(db).to_string().into(),
489 kind: Some(SymbolKind::ConstParam),
490 full_range,
491 focus_range: src.value.name().map(|n| n.syntax().text_range()),
492 container_name: None,
493 description: None,
494 docs: None,
495 })
485 } 496 }
486} 497}
487 498