Fix labels for single import assists

This commit is contained in:
Kirill Bulatov 2021-03-10 11:30:25 +02:00
parent 63715e1f73
commit 94bb9cb9ee
2 changed files with 12 additions and 20 deletions

View File

@ -1,7 +1,7 @@
use ide_db::helpers::{ use ide_db::helpers::{
import_assets::{ImportAssets, ImportCandidate}, import_assets::{ImportAssets, ImportCandidate},
insert_use::{insert_use, ImportScope}, insert_use::{insert_use, ImportScope},
item_name, mod_path_to_ast, mod_path_to_ast,
}; };
use syntax::{ast, AstNode, SyntaxNode}; use syntax::{ast, AstNode, SyntaxNode};
@ -90,17 +90,13 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
} }
let range = ctx.sema.original_range(&syntax_under_caret).range; let range = ctx.sema.original_range(&syntax_under_caret).range;
let group = import_group_message(import_assets.import_candidate()); let group_label = group_label(import_assets.import_candidate());
let scope = ImportScope::find_insert_use_container(&syntax_under_caret, &ctx.sema)?; let scope = ImportScope::find_insert_use_container(&syntax_under_caret, &ctx.sema)?;
for import in proposed_imports { for import in proposed_imports {
let name = match item_name(ctx.db(), import.original_item) {
Some(name) => name,
None => continue,
};
acc.add_group( acc.add_group(
&group, &group_label,
AssistId("auto_import", AssistKind::QuickFix), AssistId("auto_import", AssistKind::QuickFix),
format!("Import `{}`", name), format!("Import `{}`", import.import_path),
range, range,
|builder| { |builder| {
let rewriter = let rewriter =
@ -126,7 +122,7 @@ pub(super) fn find_importable_node(ctx: &AssistContext) -> Option<(ImportAssets,
} }
} }
fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel { fn group_label(import_candidate: &ImportCandidate) -> GroupLabel {
let name = match import_candidate { let name = match import_candidate {
ImportCandidate::Path(candidate) => format!("Import {}", candidate.name.text()), ImportCandidate::Path(candidate) => format!("Import {}", candidate.name.text()),
ImportCandidate::TraitAssocItem(candidate) => { ImportCandidate::TraitAssocItem(candidate) => {

View File

@ -3,7 +3,7 @@
use hir::AsAssocItem; use hir::AsAssocItem;
use ide_db::helpers::{ use ide_db::helpers::{
import_assets::{ImportCandidate, LocatedImport}, import_assets::{ImportCandidate, LocatedImport},
item_name, mod_path_to_ast, mod_path_to_ast,
}; };
use ide_db::RootDatabase; use ide_db::RootDatabase;
use syntax::{ use syntax::{
@ -78,7 +78,7 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
acc.add_group( acc.add_group(
&group_label, &group_label,
AssistId("qualify_path", AssistKind::QuickFix), AssistId("qualify_path", AssistKind::QuickFix),
label(ctx.db(), candidate, &import), label(candidate, &import),
range, range,
|builder| { |builder| {
qualify_candidate.qualify( qualify_candidate.qualify(
@ -199,21 +199,17 @@ fn group_label(candidate: &ImportCandidate) -> GroupLabel {
GroupLabel(format!("Qualify {}", name)) GroupLabel(format!("Qualify {}", name))
} }
fn label(db: &RootDatabase, candidate: &ImportCandidate, import: &LocatedImport) -> String { fn label(candidate: &ImportCandidate, import: &LocatedImport) -> String {
let display_path = match item_name(db, import.original_item) {
Some(display_path) => display_path.to_string(),
None => "{unknown}".to_string(),
};
match candidate { match candidate {
ImportCandidate::Path(candidate) => { ImportCandidate::Path(candidate) => {
if candidate.qualifier.is_some() { if candidate.qualifier.is_some() {
format!("Qualify with `{}`", display_path) format!("Qualify with `{}`", import.import_path)
} else { } else {
format!("Qualify as `{}`", display_path) format!("Qualify as `{}`", import.import_path)
} }
} }
ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", display_path), ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", import.import_path),
ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", display_path), ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", import.import_path),
} }
} }