diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-01-03 08:56:17 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-01-03 08:56:17 +0000 |
commit | 520b8a5a4dde032ba6118efb02801611191acc4e (patch) | |
tree | 811cd86e5c9a2803bc3d38f19f4ad86e60be1d18 /crates/ide/src/display | |
parent | 3bf4cec79932de0a49338f6b87dc20f85dc3a509 (diff) | |
parent | 40cd6cdf67dcfad89a80ff3a662bec2dfd983d67 (diff) |
Merge #7115
7115: Migrate HasSource::source to return Option r=matklad a=nick96
I've made a start on fixing #6913 based on the provided work plan, migrating `HasSource::source` to return an `Option`. The simple cases are migrated but there are a few that I'm unsure exactly how they should be handled:
- Logging the processing of functions in `AnalysisStatsCmd::run`: In verbose mode it includes the path to the module containing the function and the syntax range. I've handled this with an if-let but would it be better to blow up here with `expect`? I'm not 100% on the code paths but if we're processing a function definition then the source should exist.
I've handled `source()` in all code paths as `None` being a valid return value but are there some cases where we should just blow up? Also, all I've done is bubble up the returned `None`s, there may be some places where we can recover and still provide something.
Co-authored-by: Nick Spain <[email protected]>
Co-authored-by: Nick Spain <[email protected]>
Diffstat (limited to 'crates/ide/src/display')
-rw-r--r-- | crates/ide/src/display/navigation_target.rs | 130 |
1 files changed, 61 insertions, 69 deletions
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index bcde2b6f1..e24c78301 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs | |||
@@ -210,41 +210,32 @@ impl ToNav for FileSymbol { | |||
210 | impl TryToNav for Definition { | 210 | impl TryToNav for Definition { |
211 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | 211 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
212 | match self { | 212 | match self { |
213 | Definition::Macro(it) => { | 213 | Definition::Macro(it) => it.try_to_nav(db), |
214 | // FIXME: Currently proc-macro do not have ast-node, | 214 | Definition::Field(it) => it.try_to_nav(db), |
215 | // such that it does not have source | ||
216 | // more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913 | ||
217 | if it.is_proc_macro() { | ||
218 | return None; | ||
219 | } | ||
220 | Some(it.to_nav(db)) | ||
221 | } | ||
222 | Definition::Field(it) => Some(it.to_nav(db)), | ||
223 | Definition::ModuleDef(it) => it.try_to_nav(db), | 215 | Definition::ModuleDef(it) => it.try_to_nav(db), |
224 | Definition::SelfType(it) => Some(it.to_nav(db)), | 216 | Definition::SelfType(it) => it.try_to_nav(db), |
225 | Definition::Local(it) => Some(it.to_nav(db)), | 217 | Definition::Local(it) => Some(it.to_nav(db)), |
226 | Definition::TypeParam(it) => Some(it.to_nav(db)), | 218 | Definition::TypeParam(it) => it.try_to_nav(db), |
227 | Definition::LifetimeParam(it) => Some(it.to_nav(db)), | 219 | Definition::LifetimeParam(it) => it.try_to_nav(db), |
228 | Definition::Label(it) => Some(it.to_nav(db)), | 220 | Definition::Label(it) => Some(it.to_nav(db)), |
229 | Definition::ConstParam(it) => Some(it.to_nav(db)), | 221 | Definition::ConstParam(it) => it.try_to_nav(db), |
230 | } | 222 | } |
231 | } | 223 | } |
232 | } | 224 | } |
233 | 225 | ||
234 | impl TryToNav for hir::ModuleDef { | 226 | impl TryToNav for hir::ModuleDef { |
235 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | 227 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
236 | let res = match self { | 228 | match self { |
237 | hir::ModuleDef::Module(it) => it.to_nav(db), | 229 | hir::ModuleDef::Module(it) => Some(it.to_nav(db)), |
238 | hir::ModuleDef::Function(it) => it.to_nav(db), | 230 | hir::ModuleDef::Function(it) => it.try_to_nav(db), |
239 | hir::ModuleDef::Adt(it) => it.to_nav(db), | 231 | hir::ModuleDef::Adt(it) => it.try_to_nav(db), |
240 | hir::ModuleDef::Variant(it) => it.to_nav(db), | 232 | hir::ModuleDef::Variant(it) => it.try_to_nav(db), |
241 | hir::ModuleDef::Const(it) => it.to_nav(db), | 233 | hir::ModuleDef::Const(it) => it.try_to_nav(db), |
242 | hir::ModuleDef::Static(it) => it.to_nav(db), | 234 | hir::ModuleDef::Static(it) => it.try_to_nav(db), |
243 | hir::ModuleDef::Trait(it) => it.to_nav(db), | 235 | hir::ModuleDef::Trait(it) => it.try_to_nav(db), |
244 | hir::ModuleDef::TypeAlias(it) => it.to_nav(db), | 236 | hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db), |
245 | hir::ModuleDef::BuiltinType(_) => return None, | 237 | hir::ModuleDef::BuiltinType(_) => None, |
246 | }; | 238 | } |
247 | Some(res) | ||
248 | } | 239 | } |
249 | } | 240 | } |
250 | 241 | ||
@@ -279,13 +270,13 @@ impl ToNavFromAst for hir::Trait { | |||
279 | const KIND: SymbolKind = SymbolKind::Trait; | 270 | const KIND: SymbolKind = SymbolKind::Trait; |
280 | } | 271 | } |
281 | 272 | ||
282 | impl<D> ToNav for D | 273 | impl<D> TryToNav for D |
283 | where | 274 | where |
284 | D: HasSource + ToNavFromAst + Copy + HasAttrs, | 275 | D: HasSource + ToNavFromAst + Copy + HasAttrs, |
285 | D::Ast: ast::NameOwner + ShortLabel, | 276 | D::Ast: ast::NameOwner + ShortLabel, |
286 | { | 277 | { |
287 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 278 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
288 | let src = self.source(db); | 279 | let src = self.source(db)?; |
289 | let mut res = NavigationTarget::from_named( | 280 | let mut res = NavigationTarget::from_named( |
290 | db, | 281 | db, |
291 | src.as_ref().map(|it| it as &dyn ast::NameOwner), | 282 | src.as_ref().map(|it| it as &dyn ast::NameOwner), |
@@ -293,7 +284,7 @@ where | |||
293 | ); | 284 | ); |
294 | res.docs = self.docs(db); | 285 | res.docs = self.docs(db); |
295 | res.description = src.value.short_label(); | 286 | res.description = src.value.short_label(); |
296 | res | 287 | Some(res) |
297 | } | 288 | } |
298 | } | 289 | } |
299 | 290 | ||
@@ -312,9 +303,9 @@ impl ToNav for hir::Module { | |||
312 | } | 303 | } |
313 | } | 304 | } |
314 | 305 | ||
315 | impl ToNav for hir::Impl { | 306 | impl TryToNav for hir::Impl { |
316 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 307 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
317 | let src = self.source(db); | 308 | let src = self.source(db)?; |
318 | let derive_attr = self.is_builtin_derive(db); | 309 | let derive_attr = self.is_builtin_derive(db); |
319 | let frange = if let Some(item) = &derive_attr { | 310 | let frange = if let Some(item) = &derive_attr { |
320 | item.syntax().original_file_range(db) | 311 | item.syntax().original_file_range(db) |
@@ -327,21 +318,21 @@ impl ToNav for hir::Impl { | |||
327 | 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) |
328 | }; | 319 | }; |
329 | 320 | ||
330 | NavigationTarget::from_syntax( | 321 | Some(NavigationTarget::from_syntax( |
331 | frange.file_id, | 322 | frange.file_id, |
332 | "impl".into(), | 323 | "impl".into(), |
333 | focus_range, | 324 | focus_range, |
334 | frange.range, | 325 | frange.range, |
335 | SymbolKind::Impl, | 326 | SymbolKind::Impl, |
336 | ) | 327 | )) |
337 | } | 328 | } |
338 | } | 329 | } |
339 | 330 | ||
340 | impl ToNav for hir::Field { | 331 | impl TryToNav for hir::Field { |
341 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 332 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
342 | let src = self.source(db); | 333 | let src = self.source(db)?; |
343 | 334 | ||
344 | match &src.value { | 335 | let field_source = match &src.value { |
345 | FieldSource::Named(it) => { | 336 | FieldSource::Named(it) => { |
346 | let mut res = | 337 | let mut res = |
347 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); | 338 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); |
@@ -359,13 +350,14 @@ impl ToNav for hir::Field { | |||
359 | SymbolKind::Field, | 350 | SymbolKind::Field, |
360 | ) | 351 | ) |
361 | } | 352 | } |
362 | } | 353 | }; |
354 | Some(field_source) | ||
363 | } | 355 | } |
364 | } | 356 | } |
365 | 357 | ||
366 | impl ToNav for hir::MacroDef { | 358 | impl TryToNav for hir::MacroDef { |
367 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 359 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
368 | let src = self.source(db); | 360 | let src = self.source(db)?; |
369 | log::debug!("nav target {:#?}", src.value.syntax()); | 361 | log::debug!("nav target {:#?}", src.value.syntax()); |
370 | let mut res = NavigationTarget::from_named( | 362 | let mut res = NavigationTarget::from_named( |
371 | db, | 363 | db, |
@@ -373,26 +365,26 @@ impl ToNav for hir::MacroDef { | |||
373 | SymbolKind::Macro, | 365 | SymbolKind::Macro, |
374 | ); | 366 | ); |
375 | res.docs = self.docs(db); | 367 | res.docs = self.docs(db); |
376 | res | 368 | Some(res) |
377 | } | 369 | } |
378 | } | 370 | } |
379 | 371 | ||
380 | impl ToNav for hir::Adt { | 372 | impl TryToNav for hir::Adt { |
381 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 373 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
382 | match self { | 374 | match self { |
383 | hir::Adt::Struct(it) => it.to_nav(db), | 375 | hir::Adt::Struct(it) => it.try_to_nav(db), |
384 | hir::Adt::Union(it) => it.to_nav(db), | 376 | hir::Adt::Union(it) => it.try_to_nav(db), |
385 | hir::Adt::Enum(it) => it.to_nav(db), | 377 | hir::Adt::Enum(it) => it.try_to_nav(db), |
386 | } | 378 | } |
387 | } | 379 | } |
388 | } | 380 | } |
389 | 381 | ||
390 | impl ToNav for hir::AssocItem { | 382 | impl TryToNav for hir::AssocItem { |
391 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 383 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
392 | match self { | 384 | match self { |
393 | AssocItem::Function(it) => it.to_nav(db), | 385 | AssocItem::Function(it) => it.try_to_nav(db), |
394 | AssocItem::Const(it) => it.to_nav(db), | 386 | AssocItem::Const(it) => it.try_to_nav(db), |
395 | AssocItem::TypeAlias(it) => it.to_nav(db), | 387 | AssocItem::TypeAlias(it) => it.try_to_nav(db), |
396 | } | 388 | } |
397 | } | 389 | } |
398 | } | 390 | } |
@@ -446,9 +438,9 @@ impl ToNav for hir::Label { | |||
446 | } | 438 | } |
447 | } | 439 | } |
448 | 440 | ||
449 | impl ToNav for hir::TypeParam { | 441 | impl TryToNav for hir::TypeParam { |
450 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 442 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
451 | let src = self.source(db); | 443 | let src = self.source(db)?; |
452 | let full_range = match &src.value { | 444 | let full_range = match &src.value { |
453 | Either::Left(it) => it.syntax().text_range(), | 445 | Either::Left(it) => it.syntax().text_range(), |
454 | Either::Right(it) => it.syntax().text_range(), | 446 | Either::Right(it) => it.syntax().text_range(), |
@@ -457,7 +449,7 @@ impl ToNav for hir::TypeParam { | |||
457 | Either::Left(_) => None, | 449 | Either::Left(_) => None, |
458 | Either::Right(it) => it.name().map(|it| it.syntax().text_range()), | 450 | Either::Right(it) => it.name().map(|it| it.syntax().text_range()), |
459 | }; | 451 | }; |
460 | NavigationTarget { | 452 | Some(NavigationTarget { |
461 | file_id: src.file_id.original_file(db), | 453 | file_id: src.file_id.original_file(db), |
462 | name: self.name(db).to_string().into(), | 454 | name: self.name(db).to_string().into(), |
463 | kind: Some(SymbolKind::TypeParam), | 455 | kind: Some(SymbolKind::TypeParam), |
@@ -466,15 +458,15 @@ impl ToNav for hir::TypeParam { | |||
466 | container_name: None, | 458 | container_name: None, |
467 | description: None, | 459 | description: None, |
468 | docs: None, | 460 | docs: None, |
469 | } | 461 | }) |
470 | } | 462 | } |
471 | } | 463 | } |
472 | 464 | ||
473 | impl ToNav for hir::LifetimeParam { | 465 | impl TryToNav for hir::LifetimeParam { |
474 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 466 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
475 | let src = self.source(db); | 467 | let src = self.source(db)?; |
476 | let full_range = src.value.syntax().text_range(); | 468 | let full_range = src.value.syntax().text_range(); |
477 | NavigationTarget { | 469 | Some(NavigationTarget { |
478 | file_id: src.file_id.original_file(db), | 470 | file_id: src.file_id.original_file(db), |
479 | name: self.name(db).to_string().into(), | 471 | name: self.name(db).to_string().into(), |
480 | kind: Some(SymbolKind::LifetimeParam), | 472 | kind: Some(SymbolKind::LifetimeParam), |
@@ -483,15 +475,15 @@ impl ToNav for hir::LifetimeParam { | |||
483 | container_name: None, | 475 | container_name: None, |
484 | description: None, | 476 | description: None, |
485 | docs: None, | 477 | docs: None, |
486 | } | 478 | }) |
487 | } | 479 | } |
488 | } | 480 | } |
489 | 481 | ||
490 | impl ToNav for hir::ConstParam { | 482 | impl TryToNav for hir::ConstParam { |
491 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 483 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
492 | let src = self.source(db); | 484 | let src = self.source(db)?; |
493 | let full_range = src.value.syntax().text_range(); | 485 | let full_range = src.value.syntax().text_range(); |
494 | NavigationTarget { | 486 | Some(NavigationTarget { |
495 | file_id: src.file_id.original_file(db), | 487 | file_id: src.file_id.original_file(db), |
496 | name: self.name(db).to_string().into(), | 488 | name: self.name(db).to_string().into(), |
497 | kind: Some(SymbolKind::ConstParam), | 489 | kind: Some(SymbolKind::ConstParam), |
@@ -500,7 +492,7 @@ impl ToNav for hir::ConstParam { | |||
500 | container_name: None, | 492 | container_name: None, |
501 | description: None, | 493 | description: None, |
502 | docs: None, | 494 | docs: None, |
503 | } | 495 | }) |
504 | } | 496 | } |
505 | } | 497 | } |
506 | 498 | ||