From 5fd3df086864bdaf2e3040bed608fea063f81967 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 29 Jun 2019 12:19:03 +0200 Subject: Deduplicate method candidates --- crates/ra_ide_api/src/completion/complete_dot.rs | 32 +++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 40bd1e75e..a97e876e9 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -1,6 +1,7 @@ use hir::{Ty, AdtDef, TypeCtor}; use crate::completion::{CompletionContext, Completions}; +use rustc_hash::FxHashSet; /// Complete dot accesses, i.e. fields or methods (currently only fields). pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { @@ -36,9 +37,10 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) } fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { + let mut seen_methods = FxHashSet::default(); ctx.analyzer.iterate_method_candidates(ctx.db, receiver, None, |_ty, func| { let data = func.data(ctx.db); - if data.has_self_param() { + if data.has_self_param() && seen_methods.insert(data.name().clone()) { acc.add_function(ctx, func); } None::<()> @@ -230,6 +232,34 @@ mod tests { ); } + #[test] + fn test_trait_method_completion_deduplicated() { + assert_debug_snapshot_matches!( + do_ref_completion( + r" + struct A {} + trait Trait { fn the_method(&self); } + impl Trait for T {} + fn foo(a: &A) { + a.<|> + } + ", + ), + @r###" + ⋮[ + ⋮ CompletionItem { + ⋮ label: "the_method", + ⋮ source_range: [155; 155), + ⋮ delete: [155; 155), + ⋮ insert: "the_method()$0", + ⋮ kind: Method, + ⋮ detail: "fn the_method(&self)", + ⋮ }, + ⋮] + "### + ); + } + #[test] fn test_no_non_self_method() { assert_debug_snapshot_matches!( -- cgit v1.2.3