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};
102 use crate::tests::{check_edit, completion_list_no_kw};
104 fn check(ra_fixture: &str, expect: Expect) {
105 let actual = completion_list_no_kw(ra_fixture);
106 expect.assert_eq(&actual);
110 fn test_struct_field_and_method_completion() {
113 struct S { foo: u32 }
117 fn foo(s: S) { s.$0 }
127 fn test_struct_field_completion_self() {
130 struct S { the_field: (u32,) }
132 fn foo(self) { self.$0 }
143 fn test_struct_field_completion_autoderef() {
146 struct A { the_field: (u32, i32) }
148 fn foo(&self) { self.$0 }
152 fd the_field (u32, i32)
159 fn test_no_struct_field_completion_for_method_call() {
160 cov_mark::check!(test_no_struct_field_completion_for_method_call);
163 struct A { the_field: u32 }
164 fn foo(a: A) { a.$0() }
171 fn test_visibility_filtering() {
174 //- /lib.rs crate:lib new_source_root:local
179 pub(crate) crate_field: u32,
180 pub(super) super_field: u32,
183 //- /main.rs crate:main deps:lib new_source_root:local
184 fn foo(a: lib::m::A) { a.$0 }
196 //- /lib.rs crate:lib new_source_root:library
201 pub(crate) crate_field: u32,
202 pub(super) super_field: u32,
205 //- /main.rs crate:main deps:lib new_source_root:local
206 fn foo(a: lib::m::A) { a.$0 }
215 //- /lib.rs crate:lib new_source_root:library
222 //- /main.rs crate:main deps:lib new_source_root:local
223 fn foo(a: lib::m::A) { a.$0 }
232 //- /lib.rs crate:lib new_source_root:local
236 fn private_method(&self) {}
237 pub(crate) fn crate_method(&self) {}
238 pub fn pub_method(&self) {}
241 //- /main.rs crate:main deps:lib new_source_root:local
242 fn foo(a: lib::A) { a.$0 }
245 me private_method() fn(&self)
246 me crate_method() fn(&self)
247 me pub_method() fn(&self)
252 //- /lib.rs crate:lib new_source_root:library
256 fn private_method(&self) {}
257 pub(crate) fn crate_method(&self) {}
258 pub fn pub_method(&self) {}
261 //- /main.rs crate:main deps:lib new_source_root:local
262 fn foo(a: lib::A) { a.$0 }
265 me pub_method() fn(&self)
271 fn test_doc_hidden_filtering() {
274 //- /lib.rs crate:lib deps:dep
275 fn foo(a: dep::A) { a.$0 }
276 //- /dep.rs crate:dep
279 pub hidden_field: u32,
284 pub fn pub_method(&self) {}
287 pub fn hidden_method(&self) {}
292 me pub_method() fn(&self)
298 fn test_union_field_completion() {
301 union U { field: u8, other: u16 }
302 fn foo(u: U) { u.$0 }
312 fn test_method_completion_only_fitting_impls() {
317 fn the_method(&self) {}
320 fn the_other_method(&self) {}
322 fn foo(a: A<u32>) { a.$0 }
325 me the_method() fn(&self)
331 fn test_trait_method_completion() {
335 trait Trait { fn the_method(&self); }
337 fn foo(a: A) { a.$0 }
340 me the_method() (as Trait) fn(&self)
347 trait Trait { fn the_method(&self); }
349 fn foo(a: A) { a.$0 }
353 trait Trait { fn the_method(&self); }
355 fn foo(a: A) { a.the_method()$0 }
361 fn test_trait_method_completion_deduplicated() {
365 trait Trait { fn the_method(&self); }
366 impl<T> Trait for T {}
367 fn foo(a: &A) { a.$0 }
370 me the_method() (as Trait) fn(&self)
376 fn completes_trait_method_from_other_module() {
381 pub trait Trait { fn the_method(&self); }
385 fn foo(a: A) { a.$0 }
388 me the_method() (as Trait) fn(&self)
394 fn test_no_non_self_method() {
410 fn test_tuple_field_completion() {
426 fn test_tuple_struct_field_completion() {
443 fn test_tuple_field_inference() {
447 impl S { pub fn blah(&self) {} }
453 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
465 fn test_completion_works_in_consts() {
468 struct A { the_field: u32 }
470 A { the_field: 92 }.$0
480 fn works_in_simple_macro_1() {
483 macro_rules! m { ($e:expr) => { $e } }
484 struct A { the_field: u32 }
496 fn works_in_simple_macro_2() {
497 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
500 macro_rules! m { ($e:expr) => { $e } }
501 struct A { the_field: u32 }
513 fn works_in_simple_macro_recursive_1() {
516 macro_rules! m { ($e:expr) => { $e } }
517 struct A { the_field: u32 }
529 fn macro_expansion_resilient() {
535 match $val { tmp => { tmp } }
537 // Trailing comma with single argument is ignored
538 ($val:expr,) => { $crate::d!($val) };
539 ($($val:expr),+ $(,)?) => {
540 ($($crate::d!($val)),+,)
543 struct A { the_field: u32 }
555 fn test_method_completion_issue_3547() {
560 pub fn the_method(&self) {}
568 me the_method() fn(&self)
574 fn completes_method_call_when_receiver_is_a_macro_call() {
578 impl S { fn foo(&self) {} }
579 macro_rules! make_s { () => { S }; }
580 fn main() { make_s!().f$0; }
589 fn completes_after_macro_call_in_submodule() {
597 #[derive(Debug, Default)]
604 let goo: Template2 = Template2 {};
611 me private() fn(&self)
625 fn foo(&self) -> &[u8] { loop {} }
629 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
632 fn f(&mut self, v: Foo) {
633 self.indented(|this| v.$0)
638 me foo() fn(&self) -> &[u8]
644 fn completes_bare_fields_and_methods_in_methods() {
647 struct Foo { field: i32 }
649 impl Foo { fn foo(&self) { $0 } }"#,
656 me self.foo() fn(&self)
663 impl Foo { fn foo(&mut self) { $0 } }"#,
670 me self.foo() fn(&mut self)
676 fn macro_completion_after_dot() {
695 me method() fn(&self)
701 fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
704 //- minicore: iterator, sized
706 impl<T> IntoIterator for Vec<T> {
717 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter