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 f(Either::Left(field), ty);
68 for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
69 // Tuple fields are always public (tuple struct fields are handled above).
70 f(Either::Right(i), ty);
76 ctx: &CompletionContext,
78 mut f: impl FnMut(hir::Function),
80 if let Some(krate) = ctx.krate {
81 let mut seen_methods = FxHashSet::default();
82 let traits_in_scope = ctx.scope.traits_in_scope();
83 receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
84 if func.self_param(ctx.db).is_some() && seen_methods.insert(func.name(ctx.db)) {
94 use expect_test::{expect, Expect};
96 use crate::tests::{check_edit, completion_list_no_kw};
98 fn check(ra_fixture: &str, expect: Expect) {
99 let actual = completion_list_no_kw(ra_fixture);
100 expect.assert_eq(&actual);
104 fn test_struct_field_and_method_completion() {
107 struct S { foo: u32 }
111 fn foo(s: S) { s.$0 }
121 fn test_struct_field_completion_self() {
124 struct S { the_field: (u32,) }
126 fn foo(self) { self.$0 }
137 fn test_struct_field_completion_autoderef() {
140 struct A { the_field: (u32, i32) }
142 fn foo(&self) { self.$0 }
146 fd the_field (u32, i32)
153 fn test_no_struct_field_completion_for_method_call() {
154 cov_mark::check!(test_no_struct_field_completion_for_method_call);
157 struct A { the_field: u32 }
158 fn foo(a: A) { a.$0() }
165 fn test_visibility_filtering() {
168 //- /lib.rs crate:lib new_source_root:local
173 pub(crate) crate_field: u32,
174 pub(super) super_field: u32,
177 //- /main.rs crate:main deps:lib new_source_root:local
178 fn foo(a: lib::m::A) { a.$0 }
190 //- /lib.rs crate:lib new_source_root:library
195 pub(crate) crate_field: u32,
196 pub(super) super_field: u32,
199 //- /main.rs crate:main deps:lib new_source_root:local
200 fn foo(a: lib::m::A) { a.$0 }
209 //- /lib.rs crate:lib new_source_root:library
216 //- /main.rs crate:main deps:lib new_source_root:local
217 fn foo(a: lib::m::A) { a.$0 }
226 //- /lib.rs crate:lib new_source_root:local
230 fn private_method(&self) {}
231 pub(crate) fn crate_method(&self) {}
232 pub fn pub_method(&self) {}
235 //- /main.rs crate:main deps:lib new_source_root:local
236 fn foo(a: lib::A) { a.$0 }
239 me private_method() fn(&self)
240 me crate_method() fn(&self)
241 me pub_method() fn(&self)
246 //- /lib.rs crate:lib new_source_root:library
250 fn private_method(&self) {}
251 pub(crate) fn crate_method(&self) {}
252 pub fn pub_method(&self) {}
255 //- /main.rs crate:main deps:lib new_source_root:local
256 fn foo(a: lib::A) { a.$0 }
259 me pub_method() fn(&self)
265 fn test_doc_hidden_filtering() {
268 //- /lib.rs crate:lib deps:dep
269 fn foo(a: dep::A) { a.$0 }
270 //- /dep.rs crate:dep
273 pub hidden_field: u32,
278 pub fn pub_method(&self) {}
281 pub fn hidden_method(&self) {}
286 me pub_method() fn(&self)
292 fn test_union_field_completion() {
295 union U { field: u8, other: u16 }
296 fn foo(u: U) { u.$0 }
306 fn test_method_completion_only_fitting_impls() {
311 fn the_method(&self) {}
314 fn the_other_method(&self) {}
316 fn foo(a: A<u32>) { a.$0 }
319 me the_method() fn(&self)
325 fn test_trait_method_completion() {
329 trait Trait { fn the_method(&self); }
331 fn foo(a: A) { a.$0 }
334 me the_method() (as Trait) fn(&self)
341 trait Trait { fn the_method(&self); }
343 fn foo(a: A) { a.$0 }
347 trait Trait { fn the_method(&self); }
349 fn foo(a: A) { a.the_method()$0 }
355 fn test_trait_method_completion_deduplicated() {
359 trait Trait { fn the_method(&self); }
360 impl<T> Trait for T {}
361 fn foo(a: &A) { a.$0 }
364 me the_method() (as Trait) fn(&self)
370 fn completes_trait_method_from_other_module() {
375 pub trait Trait { fn the_method(&self); }
379 fn foo(a: A) { a.$0 }
382 me the_method() (as Trait) fn(&self)
388 fn test_no_non_self_method() {
404 fn test_tuple_field_completion() {
420 fn test_tuple_struct_field_completion() {
437 fn test_tuple_field_inference() {
441 impl S { pub fn blah(&self) {} }
447 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
459 fn test_completion_works_in_consts() {
462 struct A { the_field: u32 }
464 A { the_field: 92 }.$0
474 fn works_in_simple_macro_1() {
477 macro_rules! m { ($e:expr) => { $e } }
478 struct A { the_field: u32 }
490 fn works_in_simple_macro_2() {
491 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
494 macro_rules! m { ($e:expr) => { $e } }
495 struct A { the_field: u32 }
507 fn works_in_simple_macro_recursive_1() {
510 macro_rules! m { ($e:expr) => { $e } }
511 struct A { the_field: u32 }
523 fn macro_expansion_resilient() {
529 match $val { tmp => { tmp } }
531 // Trailing comma with single argument is ignored
532 ($val:expr,) => { $crate::d!($val) };
533 ($($val:expr),+ $(,)?) => {
534 ($($crate::d!($val)),+,)
537 struct A { the_field: u32 }
549 fn test_method_completion_issue_3547() {
554 pub fn the_method(&self) {}
562 me the_method() fn(&self)
568 fn completes_method_call_when_receiver_is_a_macro_call() {
572 impl S { fn foo(&self) {} }
573 macro_rules! make_s { () => { S }; }
574 fn main() { make_s!().f$0; }
583 fn completes_after_macro_call_in_submodule() {
591 #[derive(Debug, Default)]
598 let goo: Template2 = Template2 {};
605 me private() fn(&self)
619 fn foo(&self) -> &[u8] { loop {} }
623 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
626 fn f(&mut self, v: Foo) {
627 self.indented(|this| v.$0)
632 me foo() fn(&self) -> &[u8]
638 fn completes_bare_fields_and_methods_in_methods() {
641 struct Foo { field: i32 }
643 impl Foo { fn foo(&self) { $0 } }"#,
650 me self.foo() fn(&self)
657 impl Foo { fn foo(&mut self) { $0 } }"#,
664 me self.foo() fn(&mut self)
670 fn macro_completion_after_dot() {
689 me method() fn(&self)
695 fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
698 //- minicore: iterator, sized
700 impl<T> IntoIterator for Vec<T> {
711 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter