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 traits_in_scope = ctx.scope.visible_traits();
80 receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
81 if func.self_param(ctx.db).is_some() && seen_methods.insert(func.name(ctx.db)) {
91 use expect_test::{expect, Expect};
93 use crate::tests::{check_edit, completion_list_no_kw};
95 fn check(ra_fixture: &str, expect: Expect) {
96 let actual = completion_list_no_kw(ra_fixture);
97 expect.assert_eq(&actual);
101 fn test_struct_field_and_method_completion() {
104 struct S { foo: u32 }
108 fn foo(s: S) { s.$0 }
118 fn test_struct_field_completion_self() {
121 struct S { the_field: (u32,) }
123 fn foo(self) { self.$0 }
134 fn test_struct_field_completion_autoderef() {
137 struct A { the_field: (u32, i32) }
139 fn foo(&self) { self.$0 }
143 fd the_field (u32, i32)
150 fn test_no_struct_field_completion_for_method_call() {
151 cov_mark::check!(test_no_struct_field_completion_for_method_call);
154 struct A { the_field: u32 }
155 fn foo(a: A) { a.$0() }
162 fn test_visibility_filtering() {
165 //- /lib.rs crate:lib new_source_root:local
170 pub(crate) crate_field: u32,
171 pub(super) super_field: u32,
174 //- /main.rs crate:main deps:lib new_source_root:local
175 fn foo(a: lib::m::A) { a.$0 }
187 //- /lib.rs crate:lib new_source_root:library
192 pub(crate) crate_field: u32,
193 pub(super) super_field: u32,
196 //- /main.rs crate:main deps:lib new_source_root:local
197 fn foo(a: lib::m::A) { a.$0 }
206 //- /lib.rs crate:lib new_source_root:library
213 //- /main.rs crate:main deps:lib new_source_root:local
214 fn foo(a: lib::m::A) { a.$0 }
223 //- /lib.rs crate:lib new_source_root:local
227 fn private_method(&self) {}
228 pub(crate) fn crate_method(&self) {}
229 pub fn pub_method(&self) {}
232 //- /main.rs crate:main deps:lib new_source_root:local
233 fn foo(a: lib::A) { a.$0 }
236 me private_method() fn(&self)
237 me crate_method() fn(&self)
238 me pub_method() fn(&self)
243 //- /lib.rs crate:lib new_source_root:library
247 fn private_method(&self) {}
248 pub(crate) fn crate_method(&self) {}
249 pub fn pub_method(&self) {}
252 //- /main.rs crate:main deps:lib new_source_root:local
253 fn foo(a: lib::A) { a.$0 }
256 me pub_method() fn(&self)
262 fn test_doc_hidden_filtering() {
265 //- /lib.rs crate:lib deps:dep
266 fn foo(a: dep::A) { a.$0 }
267 //- /dep.rs crate:dep
270 pub hidden_field: u32,
275 pub fn pub_method(&self) {}
278 pub fn hidden_method(&self) {}
283 me pub_method() fn(&self)
289 fn test_union_field_completion() {
292 union U { field: u8, other: u16 }
293 fn foo(u: U) { u.$0 }
303 fn test_method_completion_only_fitting_impls() {
308 fn the_method(&self) {}
311 fn the_other_method(&self) {}
313 fn foo(a: A<u32>) { a.$0 }
316 me the_method() fn(&self)
322 fn test_trait_method_completion() {
326 trait Trait { fn the_method(&self); }
328 fn foo(a: A) { a.$0 }
331 me the_method() (as Trait) fn(&self)
338 trait Trait { fn the_method(&self); }
340 fn foo(a: A) { a.$0 }
344 trait Trait { fn the_method(&self); }
346 fn foo(a: A) { a.the_method()$0 }
352 fn test_trait_method_completion_deduplicated() {
356 trait Trait { fn the_method(&self); }
357 impl<T> Trait for T {}
358 fn foo(a: &A) { a.$0 }
361 me the_method() (as Trait) fn(&self)
367 fn completes_trait_method_from_other_module() {
372 pub trait Trait { fn the_method(&self); }
376 fn foo(a: A) { a.$0 }
379 me the_method() (as Trait) fn(&self)
385 fn test_no_non_self_method() {
401 fn test_tuple_field_completion() {
417 fn test_tuple_struct_field_completion() {
434 fn test_tuple_field_inference() {
438 impl S { pub fn blah(&self) {} }
444 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
456 fn test_completion_works_in_consts() {
459 struct A { the_field: u32 }
461 A { the_field: 92 }.$0
471 fn works_in_simple_macro_1() {
474 macro_rules! m { ($e:expr) => { $e } }
475 struct A { the_field: u32 }
487 fn works_in_simple_macro_2() {
488 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
491 macro_rules! m { ($e:expr) => { $e } }
492 struct A { the_field: u32 }
504 fn works_in_simple_macro_recursive_1() {
507 macro_rules! m { ($e:expr) => { $e } }
508 struct A { the_field: u32 }
520 fn macro_expansion_resilient() {
526 match $val { tmp => { tmp } }
528 // Trailing comma with single argument is ignored
529 ($val:expr,) => { $crate::d!($val) };
530 ($($val:expr),+ $(,)?) => {
531 ($($crate::d!($val)),+,)
534 struct A { the_field: u32 }
546 fn test_method_completion_issue_3547() {
551 pub fn the_method(&self) {}
559 me the_method() fn(&self)
565 fn completes_method_call_when_receiver_is_a_macro_call() {
569 impl S { fn foo(&self) {} }
570 macro_rules! make_s { () => { S }; }
571 fn main() { make_s!().f$0; }
580 fn completes_after_macro_call_in_submodule() {
588 #[derive(Debug, Default)]
595 let goo: Template2 = Template2 {};
602 me private() fn(&self)
616 fn foo(&self) -> &[u8] { loop {} }
620 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
623 fn f(&mut self, v: Foo) {
624 self.indented(|this| v.$0)
629 me foo() fn(&self) -> &[u8]
635 fn completes_bare_fields_and_methods_in_methods() {
638 struct Foo { field: i32 }
640 impl Foo { fn foo(&self) { $0 } }"#,
647 me self.foo() fn(&self)
654 impl Foo { fn foo(&mut self) { $0 } }"#,
661 me self.foo() fn(&mut self)
667 fn macro_completion_after_dot() {
686 me method() fn(&self)
692 fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
695 //- minicore: iterator, sized
697 impl<T> IntoIterator for Vec<T> {
708 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter