Skip to content

Commit 27df89f

Browse files
bors[bot]flodiebold
andcommitted
Merge #1456
1456: Deduplicate method candidates r=matklad a=flodiebold With trait method completion + autoderef, we were getting a lot of duplicates, which was really annoying... Co-authored-by: Florian Diebold <[email protected]>
2 parents 2bfcfb0 + 5fd3df0 commit 27df89f

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

crates/ra_ide_api/src/completion/complete_dot.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use hir::{Ty, AdtDef, TypeCtor};
22

33
use crate::completion::{CompletionContext, Completions};
4+
use rustc_hash::FxHashSet;
45

56
/// Complete dot accesses, i.e. fields or methods (currently only fields).
67
pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
@@ -36,9 +37,10 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
3637
}
3738

3839
fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) {
40+
let mut seen_methods = FxHashSet::default();
3941
ctx.analyzer.iterate_method_candidates(ctx.db, receiver, None, |_ty, func| {
4042
let data = func.data(ctx.db);
41-
if data.has_self_param() {
43+
if data.has_self_param() && seen_methods.insert(data.name().clone()) {
4244
acc.add_function(ctx, func);
4345
}
4446
None::<()>
@@ -230,6 +232,34 @@ mod tests {
230232
);
231233
}
232234

235+
#[test]
236+
fn test_trait_method_completion_deduplicated() {
237+
assert_debug_snapshot_matches!(
238+
do_ref_completion(
239+
r"
240+
struct A {}
241+
trait Trait { fn the_method(&self); }
242+
impl<T> Trait for T {}
243+
fn foo(a: &A) {
244+
a.<|>
245+
}
246+
",
247+
),
248+
@r###"
249+
⋮[
250+
⋮ CompletionItem {
251+
⋮ label: "the_method",
252+
⋮ source_range: [155; 155),
253+
⋮ delete: [155; 155),
254+
⋮ insert: "the_method()$0",
255+
⋮ kind: Method,
256+
⋮ detail: "fn the_method(&self)",
257+
⋮ },
258+
⋮]
259+
"###
260+
);
261+
}
262+
233263
#[test]
234264
fn test_no_non_self_method() {
235265
assert_debug_snapshot_matches!(

0 commit comments

Comments
 (0)