From 33c83e72b9b48177a6171fd06a26676679963a4d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 3 Mar 2021 01:26:53 +0200 Subject: Work towards better import labels --- crates/ide_assists/src/handlers/auto_import.rs | 13 +++++++------ crates/ide_assists/src/handlers/qualify_path.rs | 22 +++++++++++++--------- .../handlers/replace_derive_with_manual_impl.rs | 10 ++++++---- 3 files changed, 26 insertions(+), 19 deletions(-) (limited to 'crates/ide_assists/src/handlers') diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index 182547589..f3c969eee 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs @@ -93,17 +93,18 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let group = import_group_message(import_assets.import_candidate()); let scope = ImportScope::find_insert_use_container(&syntax_under_caret, &ctx.sema)?; for import in proposed_imports { + let name = match import.original_item_name(ctx.db()) { + Some(name) => name, + None => continue, + }; acc.add_group( &group, AssistId("auto_import", AssistKind::QuickFix), - format!("Import `{}`", import.display_path()), + format!("Import `{}`", name), range, |builder| { - let rewriter = insert_use( - &scope, - mod_path_to_ast(import.import_path()), - ctx.config.insert_use, - ); + let rewriter = + insert_use(&scope, mod_path_to_ast(&import.import_path), ctx.config.insert_use); builder.rewrite(rewriter); }, ); diff --git a/crates/ide_assists/src/handlers/qualify_path.rs b/crates/ide_assists/src/handlers/qualify_path.rs index 261178448..407ba47be 100644 --- a/crates/ide_assists/src/handlers/qualify_path.rs +++ b/crates/ide_assists/src/handlers/qualify_path.rs @@ -2,7 +2,7 @@ use std::iter; use hir::AsAssocItem; use ide_db::helpers::{ - import_assets::{ImportCandidate, Qualifier}, + import_assets::{ImportCandidate, LocatedImport, Qualifier}, mod_path_to_ast, }; use ide_db::RootDatabase; @@ -78,13 +78,13 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()> acc.add_group( &group_label, AssistId("qualify_path", AssistKind::QuickFix), - label(candidate, import.display_path()), + label(ctx.db(), candidate, &import), range, |builder| { qualify_candidate.qualify( |replace_with: String| builder.replace(range, replace_with), - import.import_path(), - import.item_to_import(), + &import.import_path, + import.item_to_import, ) }, ); @@ -197,17 +197,21 @@ fn group_label(candidate: &ImportCandidate) -> GroupLabel { GroupLabel(format!("Qualify {}", name)) } -fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String { +fn label(db: &RootDatabase, candidate: &ImportCandidate, import: &LocatedImport) -> String { + let display_path = match import.original_item_name(db) { + Some(display_path) => display_path.to_string(), + None => "{unknown}".to_string(), + }; match candidate { ImportCandidate::Path(candidate) => { if !matches!(candidate.qualifier, Qualifier::Absent) { - format!("Qualify with `{}`", &import) + format!("Qualify with `{}`", display_path) } else { - format!("Qualify as `{}`", &import) + format!("Qualify as `{}`", display_path) } } - ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", &import), - ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", &import), + ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", display_path), + ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", display_path), } } diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index c69bc5cac..93a03e8b2 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs @@ -1,5 +1,6 @@ +use hir::ModuleDef; use ide_db::helpers::mod_path_to_ast; -use ide_db::imports_locator; +use ide_db::items_locator; use itertools::Itertools; use syntax::{ ast::{self, make, AstNode, NameOwner}, @@ -64,13 +65,14 @@ pub(crate) fn replace_derive_with_manual_impl( let current_module = ctx.sema.scope(annotated_name.syntax()).module()?; let current_crate = current_module.krate(); - let found_traits = imports_locator::find_exact_imports( + let found_traits = items_locator::with_for_exact_name( &ctx.sema, current_crate, trait_token.text().to_string(), ) - .filter_map(|candidate: either::Either| match candidate { - either::Either::Left(hir::ModuleDef::Trait(trait_)) => Some(trait_), + .into_iter() + .filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) { + ModuleDef::Trait(trait_) => Some(trait_), _ => None, }) .flat_map(|trait_| { -- cgit v1.2.3