aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/handlers/qualify_path.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists/src/handlers/qualify_path.rs')
-rw-r--r--crates/ide_assists/src/handlers/qualify_path.rs40
1 files changed, 27 insertions, 13 deletions
diff --git a/crates/ide_assists/src/handlers/qualify_path.rs b/crates/ide_assists/src/handlers/qualify_path.rs
index d3e34e540..272874ae3 100644
--- a/crates/ide_assists/src/handlers/qualify_path.rs
+++ b/crates/ide_assists/src/handlers/qualify_path.rs
@@ -1,7 +1,10 @@
1use std::iter; 1use std::iter;
2 2
3use hir::AsAssocItem; 3use hir::AsAssocItem;
4use ide_db::helpers::{import_assets::ImportCandidate, mod_path_to_ast}; 4use ide_db::helpers::{
5 import_assets::{ImportCandidate, LocatedImport},
6 item_name, mod_path_to_ast,
7};
5use ide_db::RootDatabase; 8use ide_db::RootDatabase;
6use syntax::{ 9use syntax::{
7 ast, 10 ast,
@@ -71,17 +74,17 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
71 }; 74 };
72 75
73 let group_label = group_label(candidate); 76 let group_label = group_label(candidate);
74 for (import, item) in proposed_imports { 77 for import in proposed_imports {
75 acc.add_group( 78 acc.add_group(
76 &group_label, 79 &group_label,
77 AssistId("qualify_path", AssistKind::QuickFix), 80 AssistId("qualify_path", AssistKind::QuickFix),
78 label(candidate, &import), 81 label(ctx.db(), candidate, &import),
79 range, 82 range,
80 |builder| { 83 |builder| {
81 qualify_candidate.qualify( 84 qualify_candidate.qualify(
82 |replace_with: String| builder.replace(range, replace_with), 85 |replace_with: String| builder.replace(range, replace_with),
83 import, 86 &import.import_path,
84 item, 87 import.item_to_import,
85 ) 88 )
86 }, 89 },
87 ); 90 );
@@ -97,8 +100,13 @@ enum QualifyCandidate<'db> {
97} 100}
98 101
99impl QualifyCandidate<'_> { 102impl QualifyCandidate<'_> {
100 fn qualify(&self, mut replacer: impl FnMut(String), import: hir::ModPath, item: hir::ItemInNs) { 103 fn qualify(
101 let import = mod_path_to_ast(&import); 104 &self,
105 mut replacer: impl FnMut(String),
106 import: &hir::ModPath,
107 item: hir::ItemInNs,
108 ) {
109 let import = mod_path_to_ast(import);
102 match self { 110 match self {
103 QualifyCandidate::QualifierStart(segment, generics) => { 111 QualifyCandidate::QualifierStart(segment, generics) => {
104 let generics = generics.as_ref().map_or_else(String::new, ToString::to_string); 112 let generics = generics.as_ref().map_or_else(String::new, ToString::to_string);
@@ -183,23 +191,29 @@ fn item_as_trait(db: &RootDatabase, item: hir::ItemInNs) -> Option<hir::Trait> {
183fn group_label(candidate: &ImportCandidate) -> GroupLabel { 191fn group_label(candidate: &ImportCandidate) -> GroupLabel {
184 let name = match candidate { 192 let name = match candidate {
185 ImportCandidate::Path(it) => &it.name, 193 ImportCandidate::Path(it) => &it.name,
186 ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => &it.name, 194 ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => {
195 &it.assoc_item_name
196 }
187 } 197 }
188 .text(); 198 .text();
189 GroupLabel(format!("Qualify {}", name)) 199 GroupLabel(format!("Qualify {}", name))
190} 200}
191 201
192fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String { 202fn label(db: &RootDatabase, candidate: &ImportCandidate, import: &LocatedImport) -> String {
203 let display_path = match item_name(db, import.original_item) {
204 Some(display_path) => display_path.to_string(),
205 None => "{unknown}".to_string(),
206 };
193 match candidate { 207 match candidate {
194 ImportCandidate::Path(candidate) => { 208 ImportCandidate::Path(candidate) => {
195 if candidate.qualifier.is_some() { 209 if candidate.qualifier.is_some() {
196 format!("Qualify with `{}`", &import) 210 format!("Qualify with `{}`", display_path)
197 } else { 211 } else {
198 format!("Qualify as `{}`", &import) 212 format!("Qualify as `{}`", display_path)
199 } 213 }
200 } 214 }
201 ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", &import), 215 ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", display_path),
202 ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", &import), 216 ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", display_path),
203 } 217 }
204} 218}
205 219