Skip to content

Commit b03a0bd

Browse files
committed
Auto merge of rust-lang#15627 - jmintb:sort_imports, r=Veykril
feat: Prioritize import suggestions based on the expected type Hi, this is a draft PR to solve rust-lang#15384. `Adt` types work and now I have a few questions :) 1. What other types make sense in this context? Looking at [ModuleDef](https://github.com/rust-lang/rust-analyzer/blob/05666441bafd6010787a4097a6bd44266ad21018/crates/hir/src/lib.rs#L275) I am thinking everything except Modules. 2. Is there an existing way of converting between `ModeuleDef` and `hir::Type` in the rustanalyzer code base? 3. Does this approach seem sound to you? Ups: Upon writing this I just realised that the enum test is invalided as there are no enum variants and this no variant is passed as a function argument.
2 parents 86cccc7 + 1475848 commit b03a0bd

File tree

15 files changed

+845
-477
lines changed

15 files changed

+845
-477
lines changed

crates/ide-completion/src/completions/dot.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,9 @@ impl Foo { fn foo(&self) { $0 } }"#,
940940
expect![[r#"
941941
fd self.field i32
942942
lc self &Foo
943-
sp Self
944-
st Foo
945-
bt u32
943+
sp Self Foo
944+
st Foo Foo
945+
bt u32 u32
946946
me self.foo() fn(&self)
947947
"#]],
948948
);
@@ -954,9 +954,9 @@ impl Foo { fn foo(&mut self) { $0 } }"#,
954954
expect![[r#"
955955
fd self.0 i32
956956
lc self &mut Foo
957-
sp Self
958-
st Foo
959-
bt u32
957+
sp Self Foo
958+
st Foo Foo
959+
bt u32 u32
960960
me self.foo() fn(&mut self)
961961
"#]],
962962
);

crates/ide-completion/src/completions/item_list/trait_impl.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,10 @@ impl Test for T {
417417
}
418418
",
419419
expect![[r#"
420-
sp Self
421-
st T
420+
sp Self T
421+
st T T
422422
tt Test
423-
bt u32
423+
bt u32 u32
424424
"#]],
425425
);
426426

@@ -526,10 +526,10 @@ impl Test for T {
526526
}
527527
",
528528
expect![[r#"
529-
sp Self
530-
st T
529+
sp Self T
530+
st T T
531531
tt Test
532-
bt u32
532+
bt u32 u32
533533
"#]],
534534
);
535535

@@ -543,10 +543,10 @@ impl Test for T {
543543
}
544544
",
545545
expect![[r#"
546-
sp Self
547-
st T
546+
sp Self T
547+
st T T
548548
tt Test
549-
bt u32
549+
bt u32 u32
550550
"#]],
551551
);
552552

@@ -562,10 +562,10 @@ impl Test for T {
562562
}
563563
",
564564
expect![[r#"
565-
sp Self
566-
st T
565+
sp Self T
566+
st T T
567567
tt Test
568-
bt u32
568+
bt u32 u32
569569
"#]],
570570
);
571571

@@ -610,10 +610,10 @@ impl Test for T {
610610
}
611611
",
612612
expect![[r#"
613-
sp Self
614-
st T
613+
sp Self T
614+
st T T
615615
tt Test
616-
bt u32
616+
bt u32 u32
617617
"#]],
618618
);
619619

crates/ide-completion/src/completions/use_.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ pub(crate) fn complete_use_path(
7171

7272
if add_resolution {
7373
let mut builder = Builder::from_resolution(ctx, path_ctx, name, def);
74-
builder.set_relevance(CompletionRelevance {
74+
builder.with_relevance(|r| CompletionRelevance {
7575
is_name_already_imported,
76-
..Default::default()
76+
..r
7777
});
7878
acc.add(builder.build(ctx.db));
7979
}

crates/ide-completion/src/item.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! See `CompletionItem` structure.
22
3-
use std::fmt;
3+
use std::{fmt, mem};
44

55
use hir::Mutability;
66
use ide_db::{
@@ -570,6 +570,13 @@ impl Builder {
570570
self.relevance = relevance;
571571
self
572572
}
573+
pub(crate) fn with_relevance(
574+
&mut self,
575+
relevance: impl FnOnce(CompletionRelevance) -> CompletionRelevance,
576+
) -> &mut Builder {
577+
self.relevance = relevance(mem::take(&mut self.relevance));
578+
self
579+
}
573580
pub(crate) fn trigger_call_info(&mut self) -> &mut Builder {
574581
self.trigger_call_info = true;
575582
self

0 commit comments

Comments
 (0)