diff options
Diffstat (limited to 'crates/ide_db/src')
-rw-r--r-- | crates/ide_db/src/helpers/import_assets.rs | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs index b78d1969d..9bdc93877 100644 --- a/crates/ide_db/src/helpers/import_assets.rs +++ b/crates/ide_db/src/helpers/import_assets.rs | |||
@@ -314,19 +314,21 @@ fn import_for_item( | |||
314 | let import_path_candidate = mod_path(original_item_candidate)?; | 314 | let import_path_candidate = mod_path(original_item_candidate)?; |
315 | let import_path_string = import_path_candidate.to_string(); | 315 | let import_path_string = import_path_candidate.to_string(); |
316 | 316 | ||
317 | let expected_import_end = if item_as_assoc(db, original_item).is_some() { | ||
318 | unresolved_qualifier.to_string() | ||
319 | } else { | ||
320 | format!("{}::{}", unresolved_qualifier, item_name(db, original_item)?) | ||
321 | }; | ||
317 | if !import_path_string.contains(unresolved_first_segment) | 322 | if !import_path_string.contains(unresolved_first_segment) |
318 | || !import_path_string.contains(unresolved_qualifier) | 323 | || !import_path_string.ends_with(&expected_import_end) |
319 | { | 324 | { |
320 | return None; | 325 | return None; |
321 | } | 326 | } |
322 | 327 | ||
323 | let segment_import = | 328 | let segment_import = |
324 | find_import_for_segment(db, original_item_candidate, &unresolved_first_segment)?; | 329 | find_import_for_segment(db, original_item_candidate, &unresolved_first_segment)?; |
325 | let trait_item_to_import = original_item | 330 | let trait_item_to_import = item_as_assoc(db, original_item) |
326 | .as_module_def_id() | 331 | .and_then(|assoc| assoc.containing_trait(db)) |
327 | .and_then(|module_def_id| { | ||
328 | ModuleDef::from(module_def_id).as_assoc_item(db)?.containing_trait(db) | ||
329 | }) | ||
330 | .map(|trait_| ItemInNs::from(ModuleDef::from(trait_))); | 332 | .map(|trait_| ItemInNs::from(ModuleDef::from(trait_))); |
331 | Some(match (segment_import == original_item_candidate, trait_item_to_import) { | 333 | Some(match (segment_import == original_item_candidate, trait_item_to_import) { |
332 | (true, Some(_)) => { | 334 | (true, Some(_)) => { |
@@ -358,19 +360,15 @@ fn import_for_item( | |||
358 | 360 | ||
359 | fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> { | 361 | fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> { |
360 | Some(match item { | 362 | Some(match item { |
361 | ItemInNs::Types(module_def_id) | ItemInNs::Values(module_def_id) => { | 363 | ItemInNs::Types(_) | ItemInNs::Values(_) => match item_as_assoc(db, item) { |
362 | let module_def = ModuleDef::from(module_def_id); | 364 | Some(assoc_item) => match assoc_item.container(db) { |
363 | 365 | AssocItemContainer::Trait(trait_) => ItemInNs::from(ModuleDef::from(trait_)), | |
364 | match module_def.as_assoc_item(db) { | 366 | AssocItemContainer::Impl(impl_) => { |
365 | Some(assoc_item) => match assoc_item.container(db) { | 367 | ItemInNs::from(ModuleDef::from(impl_.target_ty(db).as_adt()?)) |
366 | AssocItemContainer::Trait(trait_) => ItemInNs::from(ModuleDef::from(trait_)), | 368 | } |
367 | AssocItemContainer::Impl(impl_) => { | 369 | }, |
368 | ItemInNs::from(ModuleDef::from(impl_.target_ty(db).as_adt()?)) | 370 | None => item, |
369 | } | 371 | }, |
370 | }, | ||
371 | None => item, | ||
372 | } | ||
373 | } | ||
374 | ItemInNs::Macros(_) => item, | 372 | ItemInNs::Macros(_) => item, |
375 | }) | 373 | }) |
376 | } | 374 | } |
@@ -427,7 +425,7 @@ fn trait_applicable_items( | |||
427 | 425 | ||
428 | let trait_candidates = items_with_candidate_name | 426 | let trait_candidates = items_with_candidate_name |
429 | .into_iter() | 427 | .into_iter() |
430 | .filter_map(|input| ModuleDef::from(input.as_module_def_id()?).as_assoc_item(db)) | 428 | .filter_map(|input| item_as_assoc(db, input)) |
431 | .filter_map(|assoc| { | 429 | .filter_map(|assoc| { |
432 | let assoc_item_trait = assoc.containing_trait(db)?; | 430 | let assoc_item_trait = assoc.containing_trait(db)?; |
433 | required_assoc_items.insert(assoc); | 431 | required_assoc_items.insert(assoc); |
@@ -583,3 +581,8 @@ fn path_import_candidate( | |||
583 | None => ImportCandidate::Path(PathImportCandidate { qualifier: Qualifier::Absent, name }), | 581 | None => ImportCandidate::Path(PathImportCandidate { qualifier: Qualifier::Absent, name }), |
584 | }) | 582 | }) |
585 | } | 583 | } |
584 | |||
585 | fn item_as_assoc(db: &RootDatabase, item: ItemInNs) -> Option<AssocItem> { | ||
586 | item.as_module_def_id() | ||
587 | .and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db)) | ||
588 | } | ||