mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
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 <flodiebold@gmail.com>
This commit is contained in:
commit
27df89f47d
1 changed files with 31 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
||||||
use hir::{Ty, AdtDef, TypeCtor};
|
use hir::{Ty, AdtDef, TypeCtor};
|
||||||
|
|
||||||
use crate::completion::{CompletionContext, Completions};
|
use crate::completion::{CompletionContext, Completions};
|
||||||
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
/// Complete dot accesses, i.e. fields or methods (currently only fields).
|
/// Complete dot accesses, i.e. fields or methods (currently only fields).
|
||||||
pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
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) {
|
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| {
|
ctx.analyzer.iterate_method_candidates(ctx.db, receiver, None, |_ty, func| {
|
||||||
let data = func.data(ctx.db);
|
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);
|
acc.add_function(ctx, func);
|
||||||
}
|
}
|
||||||
None::<()>
|
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<T> 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]
|
#[test]
|
||||||
fn test_no_non_self_method() {
|
fn test_no_non_self_method() {
|
||||||
assert_debug_snapshot_matches!(
|
assert_debug_snapshot_matches!(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue