1 //! Completes references after dot (fields and method calls).
4 use rustc_hash::FxHashSet;
6 use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions};
8 /// Complete dot accesses, i.e. fields or methods.
9 pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
10 let dot_receiver = match ctx.dot_receiver() {
12 _ => return complete_undotted_self(acc, ctx),
15 let receiver_ty = match ctx.sema.type_of_expr(dot_receiver) {
16 Some(ty) => ty.original,
20 if matches!(ctx.completion_location, Some(ImmediateLocation::MethodCall { .. })) {
21 cov_mark::hit!(test_no_struct_field_completion_for_method_call);
23 complete_fields(ctx, &receiver_ty, |field, ty| match field {
24 Either::Left(field) => acc.add_field(ctx, None, field, &ty),
25 Either::Right(tuple_idx) => acc.add_tuple_field(ctx, None, tuple_idx, &ty),
28 complete_methods(ctx, &receiver_ty, |func| acc.add_method(ctx, func, None, None));
31 fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
32 if !ctx.config.enable_self_on_the_fly {
35 if !ctx.is_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() {
38 if let Some(func) = ctx.function_def.as_ref().and_then(|fn_| ctx.sema.to_def(fn_)) {
39 if let Some(self_) = func.self_param(ctx.db) {
40 let ty = self_.ty(ctx.db);
41 complete_fields(ctx, &ty, |field, ty| match field {
42 either::Either::Left(field) => {
43 acc.add_field(ctx, Some(hir::known::SELF_PARAM), field, &ty)
45 either::Either::Right(tuple_idx) => {
46 acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), tuple_idx, &ty)
49 complete_methods(ctx, &ty, |func| {
50 acc.add_method(ctx, func, Some(hir::known::SELF_PARAM), None)
57 ctx: &CompletionContext,
59 mut f: impl FnMut(Either<hir::Field, usize>, hir::Type),
61 for receiver in receiver.autoderef(ctx.db) {
62 for (field, ty) in receiver.fields(ctx.db) {
63 f(Either::Left(field), ty);
65 for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
66 // Tuple fields are always public (tuple struct fields are handled above).
67 f(Either::Right(i), ty);
73 ctx: &CompletionContext,
75 mut f: impl FnMut(hir::Function),
77 if let Some(krate) = ctx.krate {
78 let mut seen_methods = FxHashSet::default();
79 let mut traits_in_scope = ctx.scope.visible_traits();
81 // Remove drop from the environment as calling `Drop::drop` is not allowed
82 if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {
83 cov_mark::hit!(dot_remove_drop_trait);
84 traits_in_scope.remove(&drop_trait.into());
87 receiver.iterate_method_candidates(
94 if func.self_param(ctx.db).is_some() && seen_methods.insert(func.name(ctx.db)) {
105 use expect_test::{expect, Expect};
107 use crate::tests::{check_edit, completion_list_no_kw};
109 fn check(ra_fixture: &str, expect: Expect) {
110 let actual = completion_list_no_kw(ra_fixture);
111 expect.assert_eq(&actual);
115 fn test_struct_field_and_method_completion() {
118 struct S { foo: u32 }
122 fn foo(s: S) { s.$0 }
132 fn test_struct_field_completion_self() {
135 struct S { the_field: (u32,) }
137 fn foo(self) { self.$0 }
148 fn test_struct_field_completion_autoderef() {
151 struct A { the_field: (u32, i32) }
153 fn foo(&self) { self.$0 }
157 fd the_field (u32, i32)
164 fn test_no_struct_field_completion_for_method_call() {
165 cov_mark::check!(test_no_struct_field_completion_for_method_call);
168 struct A { the_field: u32 }
169 fn foo(a: A) { a.$0() }
176 fn test_visibility_filtering() {
179 //- /lib.rs crate:lib new_source_root:local
184 pub(crate) crate_field: u32,
185 pub(super) super_field: u32,
188 //- /main.rs crate:main deps:lib new_source_root:local
189 fn foo(a: lib::m::A) { a.$0 }
201 //- /lib.rs crate:lib new_source_root:library
206 pub(crate) crate_field: u32,
207 pub(super) super_field: u32,
210 //- /main.rs crate:main deps:lib new_source_root:local
211 fn foo(a: lib::m::A) { a.$0 }
220 //- /lib.rs crate:lib new_source_root:library
227 //- /main.rs crate:main deps:lib new_source_root:local
228 fn foo(a: lib::m::A) { a.$0 }
237 //- /lib.rs crate:lib new_source_root:local
241 fn private_method(&self) {}
242 pub(crate) fn crate_method(&self) {}
243 pub fn pub_method(&self) {}
246 //- /main.rs crate:main deps:lib new_source_root:local
247 fn foo(a: lib::A) { a.$0 }
250 me private_method() fn(&self)
251 me crate_method() fn(&self)
252 me pub_method() fn(&self)
257 //- /lib.rs crate:lib new_source_root:library
261 fn private_method(&self) {}
262 pub(crate) fn crate_method(&self) {}
263 pub fn pub_method(&self) {}
266 //- /main.rs crate:main deps:lib new_source_root:local
267 fn foo(a: lib::A) { a.$0 }
270 me pub_method() fn(&self)
276 fn test_local_impls() {
279 //- /lib.rs crate:lib
283 pub fn pub_module_method(&self) {}
287 pub fn pub_foreign_local_method(&self) {}
291 //- /main.rs crate:main deps:lib
294 fn local_method(&self) {}
300 me local_method() fn(&self)
301 me pub_module_method() fn(&self)
307 fn test_doc_hidden_filtering() {
310 //- /lib.rs crate:lib deps:dep
311 fn foo(a: dep::A) { a.$0 }
312 //- /dep.rs crate:dep
315 pub hidden_field: u32,
320 pub fn pub_method(&self) {}
323 pub fn hidden_method(&self) {}
328 me pub_method() fn(&self)
334 fn test_union_field_completion() {
337 union U { field: u8, other: u16 }
338 fn foo(u: U) { u.$0 }
348 fn test_method_completion_only_fitting_impls() {
353 fn the_method(&self) {}
356 fn the_other_method(&self) {}
358 fn foo(a: A<u32>) { a.$0 }
361 me the_method() fn(&self)
367 fn test_trait_method_completion() {
371 trait Trait { fn the_method(&self); }
373 fn foo(a: A) { a.$0 }
376 me the_method() (as Trait) fn(&self)
383 trait Trait { fn the_method(&self); }
385 fn foo(a: A) { a.$0 }
389 trait Trait { fn the_method(&self); }
391 fn foo(a: A) { a.the_method()$0 }
397 fn test_trait_method_completion_deduplicated() {
401 trait Trait { fn the_method(&self); }
402 impl<T> Trait for T {}
403 fn foo(a: &A) { a.$0 }
406 me the_method() (as Trait) fn(&self)
412 fn completes_trait_method_from_other_module() {
417 pub trait Trait { fn the_method(&self); }
421 fn foo(a: A) { a.$0 }
424 me the_method() (as Trait) fn(&self)
430 fn test_no_non_self_method() {
446 fn test_tuple_field_completion() {
462 fn test_tuple_struct_field_completion() {
479 fn test_tuple_field_inference() {
483 impl S { pub fn blah(&self) {} }
489 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
501 fn test_completion_works_in_consts() {
504 struct A { the_field: u32 }
506 A { the_field: 92 }.$0
516 fn works_in_simple_macro_1() {
519 macro_rules! m { ($e:expr) => { $e } }
520 struct A { the_field: u32 }
532 fn works_in_simple_macro_2() {
533 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
536 macro_rules! m { ($e:expr) => { $e } }
537 struct A { the_field: u32 }
549 fn works_in_simple_macro_recursive_1() {
552 macro_rules! m { ($e:expr) => { $e } }
553 struct A { the_field: u32 }
565 fn macro_expansion_resilient() {
571 match $val { tmp => { tmp } }
573 // Trailing comma with single argument is ignored
574 ($val:expr,) => { $crate::d!($val) };
575 ($($val:expr),+ $(,)?) => {
576 ($($crate::d!($val)),+,)
579 struct A { the_field: u32 }
591 fn test_method_completion_issue_3547() {
596 pub fn the_method(&self) {}
604 me the_method() fn(&self)
610 fn completes_method_call_when_receiver_is_a_macro_call() {
614 impl S { fn foo(&self) {} }
615 macro_rules! make_s { () => { S }; }
616 fn main() { make_s!().f$0; }
625 fn completes_after_macro_call_in_submodule() {
633 #[derive(Debug, Default)]
640 let goo: Template2 = Template2 {};
647 me private() fn(&self)
661 fn foo(&self) -> &[u8] { loop {} }
665 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
668 fn f(&mut self, v: Foo) {
669 self.indented(|this| v.$0)
674 me foo() fn(&self) -> &[u8]
680 fn completes_bare_fields_and_methods_in_methods() {
683 struct Foo { field: i32 }
685 impl Foo { fn foo(&self) { $0 } }"#,
692 me self.foo() fn(&self)
699 impl Foo { fn foo(&mut self) { $0 } }"#,
706 me self.foo() fn(&mut self)
712 fn macro_completion_after_dot() {
731 me method() fn(&self)
737 fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
740 //- minicore: iterator, sized
742 impl<T> IntoIterator for Vec<T> {
753 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
759 fn postfix_drop_completion() {
760 cov_mark::check!(dot_remove_drop_trait);
761 cov_mark::check!(postfix_drop_completion);
767 impl<T> Drop for Vec<T> {
768 fn drop(&mut self) {}
777 impl<T> Drop for Vec<T> {
778 fn drop(&mut self) {}