diff options
Diffstat (limited to 'crates/ide/src/display')
-rw-r--r-- | crates/ide/src/display/navigation_target.rs | 145 | ||||
-rw-r--r-- | crates/ide/src/display/short_label.rs | 11 |
2 files changed, 93 insertions, 63 deletions
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 6431e7d6d..4eecae697 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,21 +210,12 @@ impl ToNav for FileSymbol { | |||
209 | impl TryToNav for Definition { | 210 | impl 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::GenericParam(it) => it.try_to_nav(db), |
226 | Definition::LifetimeParam(it) => Some(it.to_nav(db)), | ||
227 | Definition::Label(it) => Some(it.to_nav(db)), | 219 | Definition::Label(it) => Some(it.to_nav(db)), |
228 | } | 220 | } |
229 | } | 221 | } |
@@ -231,18 +223,17 @@ impl TryToNav for Definition { | |||
231 | 223 | ||
232 | impl TryToNav for hir::ModuleDef { | 224 | impl TryToNav for hir::ModuleDef { |
233 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | 225 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
234 | let res = match self { | 226 | match self { |
235 | hir::ModuleDef::Module(it) => it.to_nav(db), | 227 | hir::ModuleDef::Module(it) => Some(it.to_nav(db)), |
236 | hir::ModuleDef::Function(it) => it.to_nav(db), | 228 | hir::ModuleDef::Function(it) => it.try_to_nav(db), |
237 | hir::ModuleDef::Adt(it) => it.to_nav(db), | 229 | hir::ModuleDef::Adt(it) => it.try_to_nav(db), |
238 | hir::ModuleDef::Variant(it) => it.to_nav(db), | 230 | hir::ModuleDef::Variant(it) => it.try_to_nav(db), |
239 | hir::ModuleDef::Const(it) => it.to_nav(db), | 231 | hir::ModuleDef::Const(it) => it.try_to_nav(db), |
240 | hir::ModuleDef::Static(it) => it.to_nav(db), | 232 | hir::ModuleDef::Static(it) => it.try_to_nav(db), |
241 | hir::ModuleDef::Trait(it) => it.to_nav(db), | 233 | hir::ModuleDef::Trait(it) => it.try_to_nav(db), |
242 | hir::ModuleDef::TypeAlias(it) => it.to_nav(db), | 234 | hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db), |
243 | hir::ModuleDef::BuiltinType(_) => return None, | 235 | hir::ModuleDef::BuiltinType(_) => None, |
244 | }; | 236 | } |
245 | Some(res) | ||
246 | } | 237 | } |
247 | } | 238 | } |
248 | 239 | ||
@@ -277,13 +268,13 @@ impl ToNavFromAst for hir::Trait { | |||
277 | const KIND: SymbolKind = SymbolKind::Trait; | 268 | const KIND: SymbolKind = SymbolKind::Trait; |
278 | } | 269 | } |
279 | 270 | ||
280 | impl<D> ToNav for D | 271 | impl<D> TryToNav for D |
281 | where | 272 | where |
282 | D: HasSource + ToNavFromAst + Copy + HasAttrs, | 273 | D: HasSource + ToNavFromAst + Copy + HasAttrs, |
283 | D::Ast: ast::NameOwner + ShortLabel, | 274 | D::Ast: ast::NameOwner + ShortLabel, |
284 | { | 275 | { |
285 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 276 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
286 | let src = self.source(db); | 277 | let src = self.source(db)?; |
287 | let mut res = NavigationTarget::from_named( | 278 | let mut res = NavigationTarget::from_named( |
288 | db, | 279 | db, |
289 | src.as_ref().map(|it| it as &dyn ast::NameOwner), | 280 | src.as_ref().map(|it| it as &dyn ast::NameOwner), |
@@ -291,7 +282,7 @@ where | |||
291 | ); | 282 | ); |
292 | res.docs = self.docs(db); | 283 | res.docs = self.docs(db); |
293 | res.description = src.value.short_label(); | 284 | res.description = src.value.short_label(); |
294 | res | 285 | Some(res) |
295 | } | 286 | } |
296 | } | 287 | } |
297 | 288 | ||
@@ -310,9 +301,9 @@ impl ToNav for hir::Module { | |||
310 | } | 301 | } |
311 | } | 302 | } |
312 | 303 | ||
313 | impl ToNav for hir::Impl { | 304 | impl TryToNav for hir::Impl { |
314 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 305 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
315 | let src = self.source(db); | 306 | let src = self.source(db)?; |
316 | let derive_attr = self.is_builtin_derive(db); | 307 | let derive_attr = self.is_builtin_derive(db); |
317 | let frange = if let Some(item) = &derive_attr { | 308 | let frange = if let Some(item) = &derive_attr { |
318 | item.syntax().original_file_range(db) | 309 | item.syntax().original_file_range(db) |
@@ -325,21 +316,21 @@ impl ToNav for hir::Impl { | |||
325 | src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range) | 316 | src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range) |
326 | }; | 317 | }; |
327 | 318 | ||
328 | NavigationTarget::from_syntax( | 319 | Some(NavigationTarget::from_syntax( |
329 | frange.file_id, | 320 | frange.file_id, |
330 | "impl".into(), | 321 | "impl".into(), |
331 | focus_range, | 322 | focus_range, |
332 | frange.range, | 323 | frange.range, |
333 | SymbolKind::Impl, | 324 | SymbolKind::Impl, |
334 | ) | 325 | )) |
335 | } | 326 | } |
336 | } | 327 | } |
337 | 328 | ||
338 | impl ToNav for hir::Field { | 329 | impl TryToNav for hir::Field { |
339 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 330 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
340 | let src = self.source(db); | 331 | let src = self.source(db)?; |
341 | 332 | ||
342 | match &src.value { | 333 | let field_source = match &src.value { |
343 | FieldSource::Named(it) => { | 334 | FieldSource::Named(it) => { |
344 | let mut res = | 335 | let mut res = |
345 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); | 336 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); |
@@ -357,13 +348,14 @@ impl ToNav for hir::Field { | |||
357 | SymbolKind::Field, | 348 | SymbolKind::Field, |
358 | ) | 349 | ) |
359 | } | 350 | } |
360 | } | 351 | }; |
352 | Some(field_source) | ||
361 | } | 353 | } |
362 | } | 354 | } |
363 | 355 | ||
364 | impl ToNav for hir::MacroDef { | 356 | impl TryToNav for hir::MacroDef { |
365 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 357 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
366 | let src = self.source(db); | 358 | let src = self.source(db)?; |
367 | log::debug!("nav target {:#?}", src.value.syntax()); | 359 | log::debug!("nav target {:#?}", src.value.syntax()); |
368 | let mut res = NavigationTarget::from_named( | 360 | let mut res = NavigationTarget::from_named( |
369 | db, | 361 | db, |
@@ -371,26 +363,36 @@ impl ToNav for hir::MacroDef { | |||
371 | SymbolKind::Macro, | 363 | SymbolKind::Macro, |
372 | ); | 364 | ); |
373 | res.docs = self.docs(db); | 365 | res.docs = self.docs(db); |
374 | res | 366 | Some(res) |
375 | } | 367 | } |
376 | } | 368 | } |
377 | 369 | ||
378 | impl ToNav for hir::Adt { | 370 | impl TryToNav for hir::Adt { |
379 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 371 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
380 | match self { | 372 | match self { |
381 | hir::Adt::Struct(it) => it.to_nav(db), | 373 | hir::Adt::Struct(it) => it.try_to_nav(db), |
382 | hir::Adt::Union(it) => it.to_nav(db), | 374 | hir::Adt::Union(it) => it.try_to_nav(db), |
383 | hir::Adt::Enum(it) => it.to_nav(db), | 375 | hir::Adt::Enum(it) => it.try_to_nav(db), |
384 | } | 376 | } |
385 | } | 377 | } |
386 | } | 378 | } |
387 | 379 | ||
388 | impl ToNav for hir::AssocItem { | 380 | impl TryToNav for hir::AssocItem { |
389 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 381 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
390 | match self { | 382 | match self { |
391 | AssocItem::Function(it) => it.to_nav(db), | 383 | AssocItem::Function(it) => it.try_to_nav(db), |
392 | AssocItem::Const(it) => it.to_nav(db), | 384 | AssocItem::Const(it) => it.try_to_nav(db), |
393 | AssocItem::TypeAlias(it) => it.to_nav(db), | 385 | AssocItem::TypeAlias(it) => it.try_to_nav(db), |
386 | } | ||
387 | } | ||
388 | } | ||
389 | |||
390 | impl TryToNav for hir::GenericParam { | ||
391 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | ||
392 | match self { | ||
393 | hir::GenericParam::TypeParam(it) => it.try_to_nav(db), | ||
394 | hir::GenericParam::ConstParam(it) => it.try_to_nav(db), | ||
395 | hir::GenericParam::LifetimeParam(it) => it.try_to_nav(db), | ||
394 | } | 396 | } |
395 | } | 397 | } |
396 | } | 398 | } |
@@ -444,9 +446,9 @@ impl ToNav for hir::Label { | |||
444 | } | 446 | } |
445 | } | 447 | } |
446 | 448 | ||
447 | impl ToNav for hir::TypeParam { | 449 | impl TryToNav for hir::TypeParam { |
448 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 450 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
449 | let src = self.source(db); | 451 | let src = self.source(db)?; |
450 | let full_range = match &src.value { | 452 | let full_range = match &src.value { |
451 | Either::Left(it) => it.syntax().text_range(), | 453 | Either::Left(it) => it.syntax().text_range(), |
452 | Either::Right(it) => it.syntax().text_range(), | 454 | Either::Right(it) => it.syntax().text_range(), |
@@ -455,7 +457,7 @@ impl ToNav for hir::TypeParam { | |||
455 | Either::Left(_) => None, | 457 | Either::Left(_) => None, |
456 | Either::Right(it) => it.name().map(|it| it.syntax().text_range()), | 458 | Either::Right(it) => it.name().map(|it| it.syntax().text_range()), |
457 | }; | 459 | }; |
458 | NavigationTarget { | 460 | Some(NavigationTarget { |
459 | file_id: src.file_id.original_file(db), | 461 | file_id: src.file_id.original_file(db), |
460 | name: self.name(db).to_string().into(), | 462 | name: self.name(db).to_string().into(), |
461 | kind: Some(SymbolKind::TypeParam), | 463 | kind: Some(SymbolKind::TypeParam), |
@@ -464,15 +466,15 @@ impl ToNav for hir::TypeParam { | |||
464 | container_name: None, | 466 | container_name: None, |
465 | description: None, | 467 | description: None, |
466 | docs: None, | 468 | docs: None, |
467 | } | 469 | }) |
468 | } | 470 | } |
469 | } | 471 | } |
470 | 472 | ||
471 | impl ToNav for hir::LifetimeParam { | 473 | impl TryToNav for hir::LifetimeParam { |
472 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 474 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
473 | let src = self.source(db); | 475 | let src = self.source(db)?; |
474 | let full_range = src.value.syntax().text_range(); | 476 | let full_range = src.value.syntax().text_range(); |
475 | NavigationTarget { | 477 | Some(NavigationTarget { |
476 | file_id: src.file_id.original_file(db), | 478 | file_id: src.file_id.original_file(db), |
477 | name: self.name(db).to_string().into(), | 479 | name: self.name(db).to_string().into(), |
478 | kind: Some(SymbolKind::LifetimeParam), | 480 | kind: Some(SymbolKind::LifetimeParam), |
@@ -481,7 +483,24 @@ impl ToNav for hir::LifetimeParam { | |||
481 | container_name: None, | 483 | container_name: None, |
482 | description: None, | 484 | description: None, |
483 | docs: None, | 485 | docs: None, |
484 | } | 486 | }) |
487 | } | ||
488 | } | ||
489 | |||
490 | impl TryToNav for hir::ConstParam { | ||
491 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | ||
492 | let src = self.source(db)?; | ||
493 | let full_range = src.value.syntax().text_range(); | ||
494 | Some(NavigationTarget { | ||
495 | file_id: src.file_id.original_file(db), | ||
496 | name: self.name(db).to_string().into(), | ||
497 | kind: Some(SymbolKind::ConstParam), | ||
498 | full_range, | ||
499 | focus_range: src.value.name().map(|n| n.syntax().text_range()), | ||
500 | container_name: None, | ||
501 | description: None, | ||
502 | docs: None, | ||
503 | }) | ||
485 | } | 504 | } |
486 | } | 505 | } |
487 | 506 | ||
diff --git a/crates/ide/src/display/short_label.rs b/crates/ide/src/display/short_label.rs index ea49d9f97..990f740b8 100644 --- a/crates/ide/src/display/short_label.rs +++ b/crates/ide/src/display/short_label.rs | |||
@@ -87,6 +87,17 @@ impl ShortLabel for ast::Variant { | |||
87 | } | 87 | } |
88 | } | 88 | } |
89 | 89 | ||
90 | impl ShortLabel for ast::ConstParam { | ||
91 | fn short_label(&self) -> Option<String> { | ||
92 | let mut buf = "const ".to_owned(); | ||
93 | buf.push_str(self.name()?.text().as_str()); | ||
94 | if let Some(type_ref) = self.ty() { | ||
95 | format_to!(buf, ": {}", type_ref.syntax()); | ||
96 | } | ||
97 | Some(buf) | ||
98 | } | ||
99 | } | ||
100 | |||
90 | fn short_label_from_ty<T>(node: &T, ty: Option<ast::Type>, prefix: &str) -> Option<String> | 101 | fn short_label_from_ty<T>(node: &T, ty: Option<ast::Type>, prefix: &str) -> Option<String> |
91 | where | 102 | where |
92 | T: NameOwner + VisibilityOwner, | 103 | T: NameOwner + VisibilityOwner, |