1 //! Completes references after dot (fields and method calls).
4 use hir::{HasVisibility, ScopeDef};
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) {
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 if ctx.scope.module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) {
67 // Skip private field. FIXME: If the definition location of the
68 // field is editable, we should show the completion
71 f(Either::Left(field), ty);
73 for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
74 // FIXME: Handle visibility
75 f(Either::Right(i), ty);
81 ctx: &CompletionContext,
83 mut f: impl FnMut(hir::Function),
85 if let Some(krate) = ctx.krate {
86 let mut seen_methods = FxHashSet::default();
87 let traits_in_scope = ctx.scope.traits_in_scope();
88 receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
89 if func.self_param(ctx.db).is_some()
90 && ctx.scope.module().map_or(true, |m| func.is_visible_from(ctx.db, m))
91 && seen_methods.insert(func.name(ctx.db))
102 use expect_test::{expect, Expect};
104 use crate::{tests::filtered_completion_list, CompletionKind};
106 fn check(ra_fixture: &str, expect: Expect) {
107 let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
108 expect.assert_eq(&actual);
112 fn test_struct_field_and_method_completion() {
115 struct S { foo: u32 }
119 fn foo(s: S) { s.$0 }
129 fn test_struct_field_completion_self() {
132 struct S { the_field: (u32,) }
134 fn foo(self) { self.$0 }
145 fn test_struct_field_completion_autoderef() {
148 struct A { the_field: (u32, i32) }
150 fn foo(&self) { self.$0 }
154 fd the_field (u32, i32)
161 fn test_no_struct_field_completion_for_method_call() {
162 cov_mark::check!(test_no_struct_field_completion_for_method_call);
165 struct A { the_field: u32 }
166 fn foo(a: A) { a.$0() }
173 fn test_visibility_filtering() {
180 pub(crate) crate_field: u32,
181 pub(crate) super_field: u32,
184 fn foo(a: inner::A) { a.$0 }
198 fn private_method(&self) {}
199 pub(crate) fn the_method(&self) {}
202 fn foo(a: A) { a.$0 }
205 me the_method() fn(&self)
211 fn test_union_field_completion() {
214 union U { field: u8, other: u16 }
215 fn foo(u: U) { u.$0 }
225 fn test_method_completion_only_fitting_impls() {
230 fn the_method(&self) {}
233 fn the_other_method(&self) {}
235 fn foo(a: A<u32>) { a.$0 }
238 me the_method() fn(&self)
244 fn test_trait_method_completion() {
248 trait Trait { fn the_method(&self); }
250 fn foo(a: A) { a.$0 }
253 me the_method() (as Trait) fn(&self)
259 fn test_trait_method_completion_deduplicated() {
263 trait Trait { fn the_method(&self); }
264 impl<T> Trait for T {}
265 fn foo(a: &A) { a.$0 }
268 me the_method() (as Trait) fn(&self)
274 fn completes_trait_method_from_other_module() {
279 pub trait Trait { fn the_method(&self); }
283 fn foo(a: A) { a.$0 }
286 me the_method() (as Trait) fn(&self)
292 fn test_no_non_self_method() {
308 fn test_tuple_field_completion() {
324 fn test_tuple_field_inference() {
328 impl S { pub fn blah(&self) {} }
334 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
346 fn test_completion_works_in_consts() {
349 struct A { the_field: u32 }
351 A { the_field: 92 }.$0
361 fn works_in_simple_macro_1() {
364 macro_rules! m { ($e:expr) => { $e } }
365 struct A { the_field: u32 }
377 fn works_in_simple_macro_2() {
378 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
381 macro_rules! m { ($e:expr) => { $e } }
382 struct A { the_field: u32 }
394 fn works_in_simple_macro_recursive_1() {
397 macro_rules! m { ($e:expr) => { $e } }
398 struct A { the_field: u32 }
410 fn macro_expansion_resilient() {
416 match $val { tmp => { tmp } }
418 // Trailing comma with single argument is ignored
419 ($val:expr,) => { $crate::d!($val) };
420 ($($val:expr),+ $(,)?) => {
421 ($($crate::d!($val)),+,)
424 struct A { the_field: u32 }
436 fn test_method_completion_issue_3547() {
441 pub fn the_method(&self) {}
449 me the_method() fn(&self)
455 fn completes_method_call_when_receiver_is_a_macro_call() {
459 impl S { fn foo(&self) {} }
460 macro_rules! make_s { () => { S }; }
461 fn main() { make_s!().f$0; }
470 fn completes_after_macro_call_in_submodule() {
478 #[derive(Debug, Default)]
485 let goo: Template2 = Template2 {};
492 me private() fn(&self)
506 fn foo(&self) -> &[u8] { loop {} }
510 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
513 fn f(&mut self, v: Foo) {
514 self.indented(|this| v.$0)
519 me foo() fn(&self) -> &[u8]
525 fn completes_bare_fields_and_methods_in_methods() {
528 struct Foo { field: i32 }
530 impl Foo { fn foo(&self) { $0 } }"#,
536 me self.foo() fn(&self)
543 impl Foo { fn foo(&mut self) { $0 } }"#,
549 me self.foo() fn(&mut self)