1 //! Completes references after dot (fields and method calls).
6 context::{CompletionContext, DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, Qualified},
7 CompletionItem, CompletionItemKind, Completions,
10 /// Complete dot accesses, i.e. fields or methods.
11 pub(crate) fn complete_dot(
12 acc: &mut Completions,
13 ctx: &CompletionContext<'_>,
14 dot_access: &DotAccess,
16 let receiver_ty = match dot_access {
17 DotAccess { receiver_ty: Some(receiver_ty), .. } => &receiver_ty.original,
21 // Suggest .await syntax for types that implement Future trait
22 if receiver_ty.impls_into_future(ctx.db) {
24 CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), "await");
25 item.detail("expr.await");
29 if let DotAccessKind::Method { .. } = dot_access.kind {
30 cov_mark::hit!(test_no_struct_field_completion_for_method_call);
36 |acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty),
37 |acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
40 complete_methods(ctx, &receiver_ty, |func| acc.add_method(ctx, dot_access, func, None, None));
43 pub(crate) fn complete_undotted_self(
44 acc: &mut Completions,
45 ctx: &CompletionContext<'_>,
46 path_ctx: &PathCompletionCtx,
49 if !ctx.config.enable_self_on_the_fly {
52 if !path_ctx.is_trivial_path() {
55 if !ctx.qualifier_ctx.none() {
58 if !matches!(path_ctx.qualified, Qualified::No) {
61 let self_param = match expr_ctx {
62 ExprCtx { self_param: Some(self_param), .. } => self_param,
66 let ty = self_param.ty(ctx.db);
77 kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false },
79 Some(hir::known::SELF_PARAM),
84 |acc, field, ty| acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), field, &ty),
86 complete_methods(ctx, &ty, |func| {
92 kind: DotAccessKind::Method { has_parens: false },
95 Some(hir::known::SELF_PARAM),
102 acc: &mut Completions,
103 ctx: &CompletionContext<'_>,
104 receiver: &hir::Type,
105 mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type),
106 mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type),
108 for receiver in receiver.autoderef(ctx.db) {
109 for (field, ty) in receiver.fields(ctx.db) {
110 named_field(acc, field, ty);
112 for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
113 // Tuple fields are always public (tuple struct fields are handled above).
114 tuple_index(acc, i, ty);
120 ctx: &CompletionContext<'_>,
121 receiver: &hir::Type,
122 mut f: impl FnMut(hir::Function),
124 let mut seen_methods = FxHashSet::default();
125 receiver.iterate_method_candidates(
128 &ctx.traits_in_scope(),
132 if func.self_param(ctx.db).is_some() && seen_methods.insert(func.name(ctx.db)) {
142 use expect_test::{expect, Expect};
145 check_edit, completion_list_no_kw, completion_list_no_kw_with_private_editable,
148 fn check(ra_fixture: &str, expect: Expect) {
149 let actual = completion_list_no_kw(ra_fixture);
150 expect.assert_eq(&actual);
153 fn check_with_private_editable(ra_fixture: &str, expect: Expect) {
154 let actual = completion_list_no_kw_with_private_editable(ra_fixture);
155 expect.assert_eq(&actual);
159 fn test_struct_field_and_method_completion() {
162 struct S { foo: u32 }
166 fn foo(s: S) { s.$0 }
176 fn test_struct_field_completion_self() {
179 struct S { the_field: (u32,) }
181 fn foo(self) { self.$0 }
192 fn test_struct_field_completion_autoderef() {
195 struct A { the_field: (u32, i32) }
197 fn foo(&self) { self.$0 }
201 fd the_field (u32, i32)
208 fn test_no_struct_field_completion_for_method_call() {
209 cov_mark::check!(test_no_struct_field_completion_for_method_call);
212 struct A { the_field: u32 }
213 fn foo(a: A) { a.$0() }
220 fn test_visibility_filtering() {
223 //- /lib.rs crate:lib new_source_root:local
228 pub(crate) crate_field: u32,
229 pub(super) super_field: u32,
232 //- /main.rs crate:main deps:lib new_source_root:local
233 fn foo(a: lib::m::A) { a.$0 }
242 //- /lib.rs crate:lib new_source_root:library
247 pub(crate) crate_field: u32,
248 pub(super) super_field: u32,
251 //- /main.rs crate:main deps:lib new_source_root:local
252 fn foo(a: lib::m::A) { a.$0 }
261 //- /lib.rs crate:lib new_source_root:library
268 //- /main.rs crate:main deps:lib new_source_root:local
269 fn foo(a: lib::m::A) { a.$0 }
278 //- /lib.rs crate:lib new_source_root:local
282 fn private_method(&self) {}
283 pub(crate) fn crate_method(&self) {}
284 pub fn pub_method(&self) {}
287 //- /main.rs crate:main deps:lib new_source_root:local
288 fn foo(a: lib::A) { a.$0 }
291 me pub_method() fn(&self)
296 //- /lib.rs crate:lib new_source_root:library
300 fn private_method(&self) {}
301 pub(crate) fn crate_method(&self) {}
302 pub fn pub_method(&self) {}
305 //- /main.rs crate:main deps:lib new_source_root:local
306 fn foo(a: lib::A) { a.$0 }
309 me pub_method() fn(&self)
315 fn test_visibility_filtering_with_private_editable_enabled() {
316 check_with_private_editable(
318 //- /lib.rs crate:lib new_source_root:local
323 pub(crate) crate_field: u32,
324 pub(super) super_field: u32,
327 //- /main.rs crate:main deps:lib new_source_root:local
328 fn foo(a: lib::m::A) { a.$0 }
338 check_with_private_editable(
340 //- /lib.rs crate:lib new_source_root:library
345 pub(crate) crate_field: u32,
346 pub(super) super_field: u32,
349 //- /main.rs crate:main deps:lib new_source_root:local
350 fn foo(a: lib::m::A) { a.$0 }
357 check_with_private_editable(
359 //- /lib.rs crate:lib new_source_root:library
366 //- /main.rs crate:main deps:lib new_source_root:local
367 fn foo(a: lib::m::A) { a.$0 }
374 check_with_private_editable(
376 //- /lib.rs crate:lib new_source_root:local
380 fn private_method(&self) {}
381 pub(crate) fn crate_method(&self) {}
382 pub fn pub_method(&self) {}
385 //- /main.rs crate:main deps:lib new_source_root:local
386 fn foo(a: lib::A) { a.$0 }
389 me crate_method() fn(&self)
390 me private_method() fn(&self)
391 me pub_method() fn(&self)
394 check_with_private_editable(
396 //- /lib.rs crate:lib new_source_root:library
400 fn private_method(&self) {}
401 pub(crate) fn crate_method(&self) {}
402 pub fn pub_method(&self) {}
405 //- /main.rs crate:main deps:lib new_source_root:local
406 fn foo(a: lib::A) { a.$0 }
409 me pub_method() fn(&self)
415 fn test_local_impls() {
418 //- /lib.rs crate:lib
422 pub fn pub_module_method(&self) {}
426 pub fn pub_foreign_local_method(&self) {}
430 //- /main.rs crate:main deps:lib
433 fn local_method(&self) {}
439 me local_method() fn(&self)
440 me pub_module_method() fn(&self)
446 fn test_doc_hidden_filtering() {
449 //- /lib.rs crate:lib deps:dep
450 fn foo(a: dep::A) { a.$0 }
451 //- /dep.rs crate:dep
454 pub hidden_field: u32,
459 pub fn pub_method(&self) {}
462 pub fn hidden_method(&self) {}
467 me pub_method() fn(&self)
473 fn test_union_field_completion() {
476 union U { field: u8, other: u16 }
477 fn foo(u: U) { u.$0 }
487 fn test_method_completion_only_fitting_impls() {
492 fn the_method(&self) {}
495 fn the_other_method(&self) {}
497 fn foo(a: A<u32>) { a.$0 }
500 me the_method() fn(&self)
506 fn test_trait_method_completion() {
510 trait Trait { fn the_method(&self); }
512 fn foo(a: A) { a.$0 }
515 me the_method() (as Trait) fn(&self)
522 trait Trait { fn the_method(&self); }
524 fn foo(a: A) { a.$0 }
528 trait Trait { fn the_method(&self); }
530 fn foo(a: A) { a.the_method()$0 }
536 fn test_trait_method_completion_deduplicated() {
540 trait Trait { fn the_method(&self); }
541 impl<T> Trait for T {}
542 fn foo(a: &A) { a.$0 }
545 me the_method() (as Trait) fn(&self)
551 fn completes_trait_method_from_other_module() {
556 pub trait Trait { fn the_method(&self); }
560 fn foo(a: A) { a.$0 }
563 me the_method() (as Trait) fn(&self)
569 fn test_no_non_self_method() {
585 fn test_tuple_field_completion() {
601 fn test_tuple_struct_field_completion() {
618 fn test_tuple_field_inference() {
622 impl S { pub fn blah(&self) {} }
628 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
640 fn test_completion_works_in_consts() {
643 struct A { the_field: u32 }
645 A { the_field: 92 }.$0
655 fn works_in_simple_macro_1() {
658 macro_rules! m { ($e:expr) => { $e } }
659 struct A { the_field: u32 }
671 fn works_in_simple_macro_2() {
672 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
675 macro_rules! m { ($e:expr) => { $e } }
676 struct A { the_field: u32 }
688 fn works_in_simple_macro_recursive_1() {
691 macro_rules! m { ($e:expr) => { $e } }
692 struct A { the_field: u32 }
704 fn macro_expansion_resilient() {
710 match $val { tmp => { tmp } }
712 // Trailing comma with single argument is ignored
713 ($val:expr,) => { $crate::d!($val) };
714 ($($val:expr),+ $(,)?) => {
715 ($($crate::d!($val)),+,)
718 struct A { the_field: u32 }
730 fn test_method_completion_issue_3547() {
735 pub fn the_method(&self) {}
743 me the_method() fn(&self)
749 fn completes_method_call_when_receiver_is_a_macro_call() {
753 impl S { fn foo(&self) {} }
754 macro_rules! make_s { () => { S }; }
755 fn main() { make_s!().f$0; }
764 fn completes_after_macro_call_in_submodule() {
772 #[derive(Debug, Default)]
779 let goo: Template2 = Template2 {};
786 me private() fn(&self)
800 fn foo(&self) -> &[u8] { loop {} }
804 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
807 fn f(&mut self, v: Foo) {
808 self.indented(|this| v.$0)
813 me foo() fn(&self) -> &[u8]
819 fn completes_bare_fields_and_methods_in_methods() {
822 struct Foo { field: i32 }
824 impl Foo { fn foo(&self) { $0 } }"#,
831 me self.foo() fn(&self)
838 impl Foo { fn foo(&mut self) { $0 } }"#,
845 me self.foo() fn(&mut self)
851 fn macro_completion_after_dot() {
870 me method() fn(&self)
876 fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
879 //- minicore: iterator, sized
881 impl<T> IntoIterator for Vec<T> {
892 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
898 fn postfix_drop_completion() {
899 cov_mark::check!(postfix_drop_completion);
905 impl<T> Drop for Vec<T> {
906 fn drop(&mut self) {}
915 impl<T> Drop for Vec<T> {
916 fn drop(&mut self) {}
934 trait Closure: SizeUser {}
935 trait Encrypt: SizeUser {
936 fn encrypt(self, _: impl Closure<Size = Self::Size>);
938 fn test(thing: impl Encrypt) {
943 me encrypt(…) (as Encrypt) fn(self, impl Closure<Size = <Self as SizeUser>::Size>)