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) {
17 Some(ty) => ty.original,
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 // Tuple fields are always public (tuple struct fields are handled above).
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() {
177 //- /lib.rs crate:lib new_source_root:local
182 pub(crate) crate_field: u32,
183 pub(super) super_field: u32,
186 //- /main.rs crate:main deps:lib new_source_root:local
187 fn foo(a: lib::m::A) { a.$0 }
199 //- /lib.rs crate:lib new_source_root:library
204 pub(crate) crate_field: u32,
205 pub(super) super_field: u32,
208 //- /main.rs crate:main deps:lib new_source_root:local
209 fn foo(a: lib::m::A) { a.$0 }
218 //- /lib.rs crate:lib new_source_root:library
225 //- /main.rs crate:main deps:lib new_source_root:local
226 fn foo(a: lib::m::A) { a.$0 }
235 //- /lib.rs crate:lib new_source_root:local
239 fn private_method(&self) {}
240 pub(crate) fn crate_method(&self) {}
241 pub fn pub_method(&self) {}
244 //- /main.rs crate:main deps:lib new_source_root:local
245 fn foo(a: lib::A) { a.$0 }
248 me private_method() fn(&self)
249 me crate_method() fn(&self)
250 me pub_method() fn(&self)
255 //- /lib.rs crate:lib new_source_root:library
259 fn private_method(&self) {}
260 pub(crate) fn crate_method(&self) {}
261 pub fn pub_method(&self) {}
264 //- /main.rs crate:main deps:lib new_source_root:local
265 fn foo(a: lib::A) { a.$0 }
268 me pub_method() fn(&self)
274 fn test_doc_hidden_filtering() {
277 //- /lib.rs crate:lib deps:dep
278 fn foo(a: dep::A) { a.$0 }
279 //- /dep.rs crate:dep
282 pub hidden_field: u32,
287 pub fn pub_method(&self) {}
290 pub fn hidden_method(&self) {}
295 me pub_method() fn(&self)
301 fn test_union_field_completion() {
304 union U { field: u8, other: u16 }
305 fn foo(u: U) { u.$0 }
315 fn test_method_completion_only_fitting_impls() {
320 fn the_method(&self) {}
323 fn the_other_method(&self) {}
325 fn foo(a: A<u32>) { a.$0 }
328 me the_method() fn(&self)
334 fn test_trait_method_completion() {
338 trait Trait { fn the_method(&self); }
340 fn foo(a: A) { a.$0 }
343 me the_method() (as Trait) fn(&self)
350 trait Trait { fn the_method(&self); }
352 fn foo(a: A) { a.$0 }
356 trait Trait { fn the_method(&self); }
358 fn foo(a: A) { a.the_method()$0 }
364 fn test_trait_method_completion_deduplicated() {
368 trait Trait { fn the_method(&self); }
369 impl<T> Trait for T {}
370 fn foo(a: &A) { a.$0 }
373 me the_method() (as Trait) fn(&self)
379 fn completes_trait_method_from_other_module() {
384 pub trait Trait { fn the_method(&self); }
388 fn foo(a: A) { a.$0 }
391 me the_method() (as Trait) fn(&self)
397 fn test_no_non_self_method() {
413 fn test_tuple_field_completion() {
429 fn test_tuple_struct_field_completion() {
446 fn test_tuple_field_inference() {
450 impl S { pub fn blah(&self) {} }
456 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
468 fn test_completion_works_in_consts() {
471 struct A { the_field: u32 }
473 A { the_field: 92 }.$0
483 fn works_in_simple_macro_1() {
486 macro_rules! m { ($e:expr) => { $e } }
487 struct A { the_field: u32 }
499 fn works_in_simple_macro_2() {
500 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
503 macro_rules! m { ($e:expr) => { $e } }
504 struct A { the_field: u32 }
516 fn works_in_simple_macro_recursive_1() {
519 macro_rules! m { ($e:expr) => { $e } }
520 struct A { the_field: u32 }
532 fn macro_expansion_resilient() {
538 match $val { tmp => { tmp } }
540 // Trailing comma with single argument is ignored
541 ($val:expr,) => { $crate::d!($val) };
542 ($($val:expr),+ $(,)?) => {
543 ($($crate::d!($val)),+,)
546 struct A { the_field: u32 }
558 fn test_method_completion_issue_3547() {
563 pub fn the_method(&self) {}
571 me the_method() fn(&self)
577 fn completes_method_call_when_receiver_is_a_macro_call() {
581 impl S { fn foo(&self) {} }
582 macro_rules! make_s { () => { S }; }
583 fn main() { make_s!().f$0; }
592 fn completes_after_macro_call_in_submodule() {
600 #[derive(Debug, Default)]
607 let goo: Template2 = Template2 {};
614 me private() fn(&self)
628 fn foo(&self) -> &[u8] { loop {} }
632 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
635 fn f(&mut self, v: Foo) {
636 self.indented(|this| v.$0)
641 me foo() fn(&self) -> &[u8]
647 fn completes_bare_fields_and_methods_in_methods() {
650 struct Foo { field: i32 }
652 impl Foo { fn foo(&self) { $0 } }"#,
658 me self.foo() fn(&self)
665 impl Foo { fn foo(&mut self) { $0 } }"#,
671 me self.foo() fn(&mut self)
677 fn macro_completion_after_dot() {
696 me method() fn(&self)
702 fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
705 //- minicore: iterator, sized
707 impl<T> IntoIterator for Vec<T> {
718 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter