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(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
88 if func.self_param(ctx.db).is_some() && seen_methods.insert(func.name(ctx.db)) {
98 use expect_test::{expect, Expect};
100 use crate::tests::{check_edit, completion_list_no_kw};
102 fn check(ra_fixture: &str, expect: Expect) {
103 let actual = completion_list_no_kw(ra_fixture);
104 expect.assert_eq(&actual);
108 fn test_struct_field_and_method_completion() {
111 struct S { foo: u32 }
115 fn foo(s: S) { s.$0 }
125 fn test_struct_field_completion_self() {
128 struct S { the_field: (u32,) }
130 fn foo(self) { self.$0 }
141 fn test_struct_field_completion_autoderef() {
144 struct A { the_field: (u32, i32) }
146 fn foo(&self) { self.$0 }
150 fd the_field (u32, i32)
157 fn test_no_struct_field_completion_for_method_call() {
158 cov_mark::check!(test_no_struct_field_completion_for_method_call);
161 struct A { the_field: u32 }
162 fn foo(a: A) { a.$0() }
169 fn test_visibility_filtering() {
172 //- /lib.rs crate:lib new_source_root:local
177 pub(crate) crate_field: u32,
178 pub(super) super_field: u32,
181 //- /main.rs crate:main deps:lib new_source_root:local
182 fn foo(a: lib::m::A) { a.$0 }
194 //- /lib.rs crate:lib new_source_root:library
199 pub(crate) crate_field: u32,
200 pub(super) super_field: u32,
203 //- /main.rs crate:main deps:lib new_source_root:local
204 fn foo(a: lib::m::A) { a.$0 }
213 //- /lib.rs crate:lib new_source_root:library
220 //- /main.rs crate:main deps:lib new_source_root:local
221 fn foo(a: lib::m::A) { a.$0 }
230 //- /lib.rs crate:lib new_source_root:local
234 fn private_method(&self) {}
235 pub(crate) fn crate_method(&self) {}
236 pub fn pub_method(&self) {}
239 //- /main.rs crate:main deps:lib new_source_root:local
240 fn foo(a: lib::A) { a.$0 }
243 me private_method() fn(&self)
244 me crate_method() fn(&self)
245 me pub_method() fn(&self)
250 //- /lib.rs crate:lib new_source_root:library
254 fn private_method(&self) {}
255 pub(crate) fn crate_method(&self) {}
256 pub fn pub_method(&self) {}
259 //- /main.rs crate:main deps:lib new_source_root:local
260 fn foo(a: lib::A) { a.$0 }
263 me pub_method() fn(&self)
269 fn test_doc_hidden_filtering() {
272 //- /lib.rs crate:lib deps:dep
273 fn foo(a: dep::A) { a.$0 }
274 //- /dep.rs crate:dep
277 pub hidden_field: u32,
282 pub fn pub_method(&self) {}
285 pub fn hidden_method(&self) {}
290 me pub_method() fn(&self)
296 fn test_union_field_completion() {
299 union U { field: u8, other: u16 }
300 fn foo(u: U) { u.$0 }
310 fn test_method_completion_only_fitting_impls() {
315 fn the_method(&self) {}
318 fn the_other_method(&self) {}
320 fn foo(a: A<u32>) { a.$0 }
323 me the_method() fn(&self)
329 fn test_trait_method_completion() {
333 trait Trait { fn the_method(&self); }
335 fn foo(a: A) { a.$0 }
338 me the_method() (as Trait) fn(&self)
345 trait Trait { fn the_method(&self); }
347 fn foo(a: A) { a.$0 }
351 trait Trait { fn the_method(&self); }
353 fn foo(a: A) { a.the_method()$0 }
359 fn test_trait_method_completion_deduplicated() {
363 trait Trait { fn the_method(&self); }
364 impl<T> Trait for T {}
365 fn foo(a: &A) { a.$0 }
368 me the_method() (as Trait) fn(&self)
374 fn completes_trait_method_from_other_module() {
379 pub trait Trait { fn the_method(&self); }
383 fn foo(a: A) { a.$0 }
386 me the_method() (as Trait) fn(&self)
392 fn test_no_non_self_method() {
408 fn test_tuple_field_completion() {
424 fn test_tuple_struct_field_completion() {
441 fn test_tuple_field_inference() {
445 impl S { pub fn blah(&self) {} }
451 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
463 fn test_completion_works_in_consts() {
466 struct A { the_field: u32 }
468 A { the_field: 92 }.$0
478 fn works_in_simple_macro_1() {
481 macro_rules! m { ($e:expr) => { $e } }
482 struct A { the_field: u32 }
494 fn works_in_simple_macro_2() {
495 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
498 macro_rules! m { ($e:expr) => { $e } }
499 struct A { the_field: u32 }
511 fn works_in_simple_macro_recursive_1() {
514 macro_rules! m { ($e:expr) => { $e } }
515 struct A { the_field: u32 }
527 fn macro_expansion_resilient() {
533 match $val { tmp => { tmp } }
535 // Trailing comma with single argument is ignored
536 ($val:expr,) => { $crate::d!($val) };
537 ($($val:expr),+ $(,)?) => {
538 ($($crate::d!($val)),+,)
541 struct A { the_field: u32 }
553 fn test_method_completion_issue_3547() {
558 pub fn the_method(&self) {}
566 me the_method() fn(&self)
572 fn completes_method_call_when_receiver_is_a_macro_call() {
576 impl S { fn foo(&self) {} }
577 macro_rules! make_s { () => { S }; }
578 fn main() { make_s!().f$0; }
587 fn completes_after_macro_call_in_submodule() {
595 #[derive(Debug, Default)]
602 let goo: Template2 = Template2 {};
609 me private() fn(&self)
623 fn foo(&self) -> &[u8] { loop {} }
627 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
630 fn f(&mut self, v: Foo) {
631 self.indented(|this| v.$0)
636 me foo() fn(&self) -> &[u8]
642 fn completes_bare_fields_and_methods_in_methods() {
645 struct Foo { field: i32 }
647 impl Foo { fn foo(&self) { $0 } }"#,
654 me self.foo() fn(&self)
661 impl Foo { fn foo(&mut self) { $0 } }"#,
668 me self.foo() fn(&mut self)
674 fn macro_completion_after_dot() {
693 me method() fn(&self)
699 fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
702 //- minicore: iterator, sized
704 impl<T> IntoIterator for Vec<T> {
715 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
721 fn postfix_drop_completion() {
722 cov_mark::check!(dot_remove_drop_trait);
723 cov_mark::check!(postfix_drop_completion);
729 impl<T> Drop for Vec<T> {
730 fn drop(&mut self) {}
739 impl<T> Drop for Vec<T> {
740 fn drop(&mut self) {}