Skip to content

Commit 33c83e7

Browse files
Work towards better import labels
1 parent 4d4ac1d commit 33c83e7

File tree

13 files changed

+243
-239
lines changed

13 files changed

+243
-239
lines changed

crates/hir_def/src/import_map.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,4 +1094,27 @@ mod tests {
10941094
expect![[r#""#]],
10951095
);
10961096
}
1097+
1098+
#[test]
1099+
fn search_with_path() {
1100+
check_search(
1101+
r#"
1102+
//- /main.rs crate:main deps:dep
1103+
//- /dep.rs crate:dep
1104+
pub mod foo {
1105+
pub mod bar {
1106+
pub mod baz {
1107+
pub trait Display {
1108+
fn fmt();
1109+
}
1110+
}
1111+
}
1112+
}"#,
1113+
"main",
1114+
Query::new("baz::fmt".to_string()).search_mode(SearchMode::Fuzzy),
1115+
expect![[r#"
1116+
dep::foo::bar::baz::Display::fmt (a)
1117+
"#]],
1118+
);
1119+
}
10971120
}

crates/ide/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ impl Analysis {
478478
position: FilePosition,
479479
full_import_path: &str,
480480
imported_name: String,
481-
import_for_trait_assoc_item: bool,
482481
) -> Cancelable<Vec<TextEdit>> {
483482
Ok(self
484483
.with_db(|db| {
@@ -488,7 +487,6 @@ impl Analysis {
488487
position,
489488
full_import_path,
490489
imported_name,
491-
import_for_trait_assoc_item,
492490
)
493491
})?
494492
.unwrap_or_default())

crates/ide_assists/src/handlers/auto_import.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,18 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
9393
let group = import_group_message(import_assets.import_candidate());
9494
let scope = ImportScope::find_insert_use_container(&syntax_under_caret, &ctx.sema)?;
9595
for import in proposed_imports {
96+
let name = match import.original_item_name(ctx.db()) {
97+
Some(name) => name,
98+
None => continue,
99+
};
96100
acc.add_group(
97101
&group,
98102
AssistId("auto_import", AssistKind::QuickFix),
99-
format!("Import `{}`", import.display_path()),
103+
format!("Import `{}`", name),
100104
range,
101105
|builder| {
102-
let rewriter = insert_use(
103-
&scope,
104-
mod_path_to_ast(import.import_path()),
105-
ctx.config.insert_use,
106-
);
106+
let rewriter =
107+
insert_use(&scope, mod_path_to_ast(&import.import_path), ctx.config.insert_use);
107108
builder.rewrite(rewriter);
108109
},
109110
);

crates/ide_assists/src/handlers/qualify_path.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::iter;
22

33
use hir::AsAssocItem;
44
use ide_db::helpers::{
5-
import_assets::{ImportCandidate, Qualifier},
5+
import_assets::{ImportCandidate, LocatedImport, Qualifier},
66
mod_path_to_ast,
77
};
88
use ide_db::RootDatabase;
@@ -78,13 +78,13 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
7878
acc.add_group(
7979
&group_label,
8080
AssistId("qualify_path", AssistKind::QuickFix),
81-
label(candidate, import.display_path()),
81+
label(ctx.db(), candidate, &import),
8282
range,
8383
|builder| {
8484
qualify_candidate.qualify(
8585
|replace_with: String| builder.replace(range, replace_with),
86-
import.import_path(),
87-
import.item_to_import(),
86+
&import.import_path,
87+
import.item_to_import,
8888
)
8989
},
9090
);
@@ -197,17 +197,21 @@ fn group_label(candidate: &ImportCandidate) -> GroupLabel {
197197
GroupLabel(format!("Qualify {}", name))
198198
}
199199

200-
fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String {
200+
fn label(db: &RootDatabase, candidate: &ImportCandidate, import: &LocatedImport) -> String {
201+
let display_path = match import.original_item_name(db) {
202+
Some(display_path) => display_path.to_string(),
203+
None => "{unknown}".to_string(),
204+
};
201205
match candidate {
202206
ImportCandidate::Path(candidate) => {
203207
if !matches!(candidate.qualifier, Qualifier::Absent) {
204-
format!("Qualify with `{}`", &import)
208+
format!("Qualify with `{}`", display_path)
205209
} else {
206-
format!("Qualify as `{}`", &import)
210+
format!("Qualify as `{}`", display_path)
207211
}
208212
}
209-
ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", &import),
210-
ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", &import),
213+
ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", display_path),
214+
ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", display_path),
211215
}
212216
}
213217

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use hir::ModuleDef;
12
use ide_db::helpers::mod_path_to_ast;
2-
use ide_db::imports_locator;
3+
use ide_db::items_locator;
34
use itertools::Itertools;
45
use syntax::{
56
ast::{self, make, AstNode, NameOwner},
@@ -64,13 +65,14 @@ pub(crate) fn replace_derive_with_manual_impl(
6465
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
6566
let current_crate = current_module.krate();
6667

67-
let found_traits = imports_locator::find_exact_imports(
68+
let found_traits = items_locator::with_for_exact_name(
6869
&ctx.sema,
6970
current_crate,
7071
trait_token.text().to_string(),
7172
)
72-
.filter_map(|candidate: either::Either<hir::ModuleDef, hir::MacroDef>| match candidate {
73-
either::Either::Left(hir::ModuleDef::Trait(trait_)) => Some(trait_),
73+
.into_iter()
74+
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
75+
ModuleDef::Trait(trait_) => Some(trait_),
7476
_ => None,
7577
})
7678
.flat_map(|trait_| {

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@
8787
//! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding
8888
//! capability enabled.
8989
90-
use hir::{AsAssocItem, ModPath, ModuleDef, ScopeDef};
90+
use hir::ModPath;
9191
use ide_db::helpers::{
9292
import_assets::{ImportAssets, ImportCandidate},
9393
insert_use::ImportScope,
9494
};
95+
use itertools::Itertools;
9596
use syntax::{AstNode, SyntaxNode, T};
9697

9798
use crate::{
@@ -130,27 +131,23 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
130131
&ctx.sema,
131132
)?;
132133

133-
let mut all_imports =
134-
import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind);
135-
all_imports.sort_by_cached_key(|import| {
136-
compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased)
137-
});
138-
139-
acc.add_all(all_imports.into_iter().filter_map(|import| {
140-
let import_for_trait_assoc_item = import
141-
.item_to_display()
142-
.as_module_def_id()
143-
.and_then(|module_def_id| {
144-
ModuleDef::from(module_def_id).as_assoc_item(ctx.db)?.containing_trait(ctx.db)
134+
acc.add_all(
135+
import_assets
136+
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
137+
.into_iter()
138+
.sorted_by_key(|located_import| {
139+
compute_fuzzy_completion_order_key(
140+
&located_import.import_path,
141+
&user_input_lowercased,
142+
)
145143
})
146-
.is_some();
147-
let def_to_display = ScopeDef::from(import.item_to_display());
148-
render_resolution_with_import(
149-
RenderContext::new(ctx),
150-
ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item },
151-
&def_to_display,
152-
)
153-
}));
144+
.filter_map(|import| {
145+
render_resolution_with_import(
146+
RenderContext::new(ctx),
147+
ImportEdit { import, import_scope: import_scope.clone() },
148+
)
149+
}),
150+
);
154151
Some(())
155152
}
156153

@@ -190,6 +187,7 @@ fn import_assets<'a>(ctx: &'a CompletionContext, fuzzy_name: String) -> Option<I
190187
ctx.scope.clone(),
191188
)?;
192189

190+
// TODO kb bad: with the path prefix, the "min 3 symbols" limit applies. Fix in a separate PR on the symbol_index level
193191
if matches!(assets_for_path.import_candidate(), ImportCandidate::Path(_))
194192
&& fuzzy_name_length < 2
195193
{
@@ -796,9 +794,7 @@ fn main() {
796794

797795
#[test]
798796
fn unresolved_qualifier() {
799-
check_edit(
800-
"Item",
801-
r#"
797+
let fixture = r#"
802798
mod foo {
803799
pub mod bar {
804800
pub mod baz {
@@ -809,31 +805,34 @@ mod foo {
809805
810806
fn main() {
811807
bar::baz::Ite$0
812-
}
813-
"#,
808+
}"#;
809+
810+
check(fixture, expect![["st Item (foo::bar::baz::Item)"]]);
811+
812+
check_edit(
813+
"Item",
814+
fixture,
814815
r#"
815-
use foo::bar;
816+
use foo::bar;
816817
817-
mod foo {
818-
pub mod bar {
819-
pub mod baz {
820-
pub struct Item;
818+
mod foo {
819+
pub mod bar {
820+
pub mod baz {
821+
pub struct Item;
822+
}
823+
}
821824
}
822-
}
823-
}
824825
825-
fn main() {
826-
bar::baz::Item
827-
}
828-
"#,
826+
fn main() {
827+
bar::baz::Item
828+
}
829+
"#,
829830
);
830831
}
831832

832833
#[test]
833834
fn unresolved_assoc_item_container() {
834-
check_edit(
835-
"TEST_ASSOC",
836-
r#"
835+
let fixture = r#"
837836
mod foo {
838837
pub struct Item;
839838
@@ -844,8 +843,13 @@ mod foo {
844843
845844
fn main() {
846845
Item::TEST_A$0
847-
}
848-
"#,
846+
}"#;
847+
848+
check(fixture, expect![["ct TEST_ASSOC (foo::bar::baz::Item)"]]);
849+
850+
check_edit(
851+
"TEST_ASSOC",
852+
fixture,
849853
r#"
850854
use foo::Item;
851855
@@ -866,9 +870,7 @@ fn main() {
866870

867871
#[test]
868872
fn unresolved_assoc_item_container_with_path() {
869-
check_edit(
870-
"TEST_ASSOC",
871-
r#"
873+
let fixture = r#"
872874
mod foo {
873875
pub mod bar {
874876
pub struct Item;
@@ -881,8 +883,13 @@ mod foo {
881883
882884
fn main() {
883885
bar::Item::TEST_A$0
884-
}
885-
"#,
886+
}"#;
887+
888+
check(fixture, expect![["ct TEST_ASSOC (foo::bar::baz::Item)"]]);
889+
890+
check_edit(
891+
"TEST_ASSOC",
892+
fixture,
886893
r#"
887894
use foo::bar;
888895

crates/ide_completion/src/item.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ impl CompletionItem {
275275
pub struct ImportEdit {
276276
pub import: LocatedImport,
277277
pub import_scope: ImportScope,
278-
pub import_for_trait_assoc_item: bool,
279278
}
280279

281280
impl ImportEdit {
@@ -286,7 +285,7 @@ impl ImportEdit {
286285

287286
let rewriter = insert_use::insert_use(
288287
&self.import_scope,
289-
mod_path_to_ast(self.import.import_path()),
288+
mod_path_to_ast(&self.import.import_path),
290289
cfg,
291290
);
292291
let old_ast = rewriter.rewrite_root()?;
@@ -303,6 +302,7 @@ impl ImportEdit {
303302
pub(crate) struct Builder {
304303
source_range: TextRange,
305304
completion_kind: CompletionKind,
305+
// TODO kb also add a db here, to resolve the completion label?
306306
import_to_add: Option<ImportEdit>,
307307
label: String,
308308
insert_text: Option<String>,
@@ -322,19 +322,22 @@ impl Builder {
322322
pub(crate) fn build(self) -> CompletionItem {
323323
let _p = profile::span("item::Builder::build");
324324

325-
let mut label = self.label;
326-
let mut lookup = self.lookup;
327-
let mut insert_text = self.insert_text;
328-
329-
if let Some(import_to_add) = self.import_to_add.as_ref() {
330-
lookup = lookup.or_else(|| Some(label.clone()));
331-
insert_text = insert_text.or_else(|| Some(label.clone()));
332-
let display_path = import_to_add.import.display_path();
333-
if import_to_add.import_for_trait_assoc_item {
334-
label = format!("{} ({})", label, display_path);
335-
} else {
336-
label = display_path.to_string();
337-
}
325+
let label = self.label;
326+
let lookup = self.lookup;
327+
let insert_text = self.insert_text;
328+
329+
if let Some(_import_to_add) = self.import_to_add.as_ref() {
330+
todo!("todo kb")
331+
// let import = &import_to_add.import;
332+
// let item_to_import = import.item_to_import();
333+
// lookup = lookup.or_else(|| Some(label.clone()));
334+
// insert_text = insert_text.or_else(|| Some(label.clone()));
335+
// let display_path = import_to_add.import.display_path();
336+
// if import_to_add.import {
337+
// label = format!("{} ({})", label, display_path);
338+
// } else {
339+
// label = display_path.to_string();
340+
// }
338341
}
339342

340343
let text_edit = match self.text_edit {
@@ -438,8 +441,8 @@ impl Builder {
438441
}
439442
}
440443

441-
impl<'a> Into<CompletionItem> for Builder {
442-
fn into(self) -> CompletionItem {
443-
self.build()
444-
}
445-
}
444+
// impl<'a> Into<CompletionItem> for Builder {
445+
// fn into(self) -> CompletionItem {
446+
// self.build()
447+
// }
448+
// }

0 commit comments

Comments
 (0)