1 //! Completes references after dot (fields and method calls).
5 use rustc_hash::FxHashSet;
7 use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions};
9 /// Complete dot accesses, i.e. fields or methods.
10 pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
11 let dot_receiver = match ctx.dot_receiver() {
13 _ => return complete_undotted_self(acc, ctx),
16 let receiver_ty = match ctx.sema.type_of_expr(dot_receiver) {
21 if matches!(ctx.completion_location, Some(ImmediateLocation::MethodCall { .. })) {
22 cov_mark::hit!(test_no_struct_field_completion_for_method_call);
24 complete_fields(ctx, &receiver_ty, |field, ty| match field {
25 Either::Left(field) => acc.add_field(ctx, None, field, &ty),
26 Either::Right(tuple_idx) => acc.add_tuple_field(ctx, None, tuple_idx, &ty),
29 complete_methods(ctx, &receiver_ty, |func| acc.add_method(ctx, func, None, None));
32 fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
33 if !ctx.config.enable_self_on_the_fly {
36 if !ctx.is_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() {
39 ctx.scope.process_all_names(&mut |name, def| {
40 if let ScopeDef::Local(local) = &def {
41 if local.is_self(ctx.db) {
42 let ty = local.ty(ctx.db);
43 complete_fields(ctx, &ty, |field, ty| match field {
44 either::Either::Left(field) => {
45 acc.add_field(ctx, Some(name.clone()), field, &ty)
47 either::Either::Right(tuple_idx) => {
48 acc.add_tuple_field(ctx, Some(name.clone()), tuple_idx, &ty)
51 complete_methods(ctx, &ty, |func| {
52 acc.add_method(ctx, func, Some(name.clone()), None)
60 ctx: &CompletionContext,
62 mut f: impl FnMut(Either<hir::Field, usize>, hir::Type),
64 for receiver in receiver.autoderef(ctx.db) {
65 for (field, ty) in receiver.fields(ctx.db) {
66 if !ctx.is_visible(&field) {
69 f(Either::Left(field), ty);
71 for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
72 // FIXME: Handle visibility
73 f(Either::Right(i), ty);
79 ctx: &CompletionContext,
81 mut f: impl FnMut(hir::Function),
83 if let Some(krate) = ctx.krate {
84 let mut seen_methods = FxHashSet::default();
85 let traits_in_scope = ctx.scope.traits_in_scope();
86 receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
87 if func.self_param(ctx.db).is_some()
88 && ctx.is_visible(&func)
89 && seen_methods.insert(func.name(ctx.db))
100 use expect_test::{expect, Expect};
103 tests::{check_edit, filtered_completion_list},
107 fn check(ra_fixture: &str, expect: Expect) {
108 let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
109 expect.assert_eq(&actual);
113 fn test_struct_field_and_method_completion() {
116 struct S { foo: u32 }
120 fn foo(s: S) { s.$0 }
130 fn test_struct_field_completion_self() {
133 struct S { the_field: (u32,) }
135 fn foo(self) { self.$0 }
146 fn test_struct_field_completion_autoderef() {
149 struct A { the_field: (u32, i32) }
151 fn foo(&self) { self.$0 }
155 fd the_field (u32, i32)
162 fn test_no_struct_field_completion_for_method_call() {
163 cov_mark::check!(test_no_struct_field_completion_for_method_call);
166 struct A { the_field: u32 }
167 fn foo(a: A) { a.$0() }
174 fn test_visibility_filtering() {
181 pub(crate) crate_field: u32,
182 pub(crate) super_field: u32,
185 fn foo(a: inner::A) { a.$0 }
199 fn private_method(&self) {}
200 pub(crate) fn the_method(&self) {}
203 fn foo(a: A) { a.$0 }
206 me the_method() fn(&self)
212 fn test_doc_hidden_filtering() {
215 //- /lib.rs crate:lib deps:dep
216 fn foo(a: dep::A) { a.$0 }
217 //- /dep.rs crate:dep
220 pub hidden_field: u32,
225 pub fn pub_method(&self) {}
228 pub fn hidden_method(&self) {}
233 me pub_method() fn(&self)
239 fn test_union_field_completion() {
242 union U { field: u8, other: u16 }
243 fn foo(u: U) { u.$0 }
253 fn test_method_completion_only_fitting_impls() {
258 fn the_method(&self) {}
261 fn the_other_method(&self) {}
263 fn foo(a: A<u32>) { a.$0 }
266 me the_method() fn(&self)
272 fn test_trait_method_completion() {
276 trait Trait { fn the_method(&self); }
278 fn foo(a: A) { a.$0 }
281 me the_method() (as Trait) fn(&self)
288 trait Trait { fn the_method(&self); }
290 fn foo(a: A) { a.$0 }
294 trait Trait { fn the_method(&self); }
296 fn foo(a: A) { a.the_method()$0 }
302 fn test_trait_method_completion_deduplicated() {
306 trait Trait { fn the_method(&self); }
307 impl<T> Trait for T {}
308 fn foo(a: &A) { a.$0 }
311 me the_method() (as Trait) fn(&self)
317 fn completes_trait_method_from_other_module() {
322 pub trait Trait { fn the_method(&self); }
326 fn foo(a: A) { a.$0 }
329 me the_method() (as Trait) fn(&self)
335 fn test_no_non_self_method() {
351 fn test_tuple_field_completion() {
367 fn test_tuple_field_inference() {
371 impl S { pub fn blah(&self) {} }
377 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
389 fn test_completion_works_in_consts() {
392 struct A { the_field: u32 }
394 A { the_field: 92 }.$0
404 fn works_in_simple_macro_1() {
407 macro_rules! m { ($e:expr) => { $e } }
408 struct A { the_field: u32 }
420 fn works_in_simple_macro_2() {
421 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
424 macro_rules! m { ($e:expr) => { $e } }
425 struct A { the_field: u32 }
437 fn works_in_simple_macro_recursive_1() {
440 macro_rules! m { ($e:expr) => { $e } }
441 struct A { the_field: u32 }
453 fn macro_expansion_resilient() {
459 match $val { tmp => { tmp } }
461 // Trailing comma with single argument is ignored
462 ($val:expr,) => { $crate::d!($val) };
463 ($($val:expr),+ $(,)?) => {
464 ($($crate::d!($val)),+,)
467 struct A { the_field: u32 }
479 fn test_method_completion_issue_3547() {
484 pub fn the_method(&self) {}
492 me the_method() fn(&self)
498 fn completes_method_call_when_receiver_is_a_macro_call() {
502 impl S { fn foo(&self) {} }
503 macro_rules! make_s { () => { S }; }
504 fn main() { make_s!().f$0; }
513 fn completes_after_macro_call_in_submodule() {
521 #[derive(Debug, Default)]
528 let goo: Template2 = Template2 {};
535 me private() fn(&self)
549 fn foo(&self) -> &[u8] { loop {} }
553 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
556 fn f(&mut self, v: Foo) {
557 self.indented(|this| v.$0)
562 me foo() fn(&self) -> &[u8]
568 fn completes_bare_fields_and_methods_in_methods() {
571 struct Foo { field: i32 }
573 impl Foo { fn foo(&self) { $0 } }"#,
579 me self.foo() fn(&self)
586 impl Foo { fn foo(&mut self) { $0 } }"#,
592 me self.foo() fn(&mut self)