1 //! Completes references after dot (fields and method calls).
3 use hir::{HasVisibility, Type};
4 use rustc_hash::FxHashSet;
7 use crate::{context::CompletionContext, 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 {
16 let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) {
22 mark::hit!(test_no_struct_field_completion_for_method_call);
24 complete_fields(acc, ctx, &receiver_ty);
26 complete_methods(acc, ctx, &receiver_ty);
29 fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) {
30 for receiver in receiver.autoderef(ctx.db) {
31 for (field, ty) in receiver.fields(ctx.db) {
32 if ctx.scope.module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) {
33 // Skip private field. FIXME: If the definition location of the
34 // field is editable, we should show the completion
37 acc.add_field(ctx, field, &ty);
39 for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
40 // FIXME: Handle visibility
41 acc.add_tuple_field(ctx, i, &ty);
46 fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) {
47 if let Some(krate) = ctx.krate {
48 let mut seen_methods = FxHashSet::default();
49 let traits_in_scope = ctx.scope.traits_in_scope();
50 receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
51 if func.self_param(ctx.db).is_some()
52 && ctx.scope.module().map_or(true, |m| func.is_visible_from(ctx.db, m))
53 && seen_methods.insert(func.name(ctx.db))
55 acc.add_function(ctx, func, None);
64 use expect_test::{expect, Expect};
67 use crate::{test_utils::completion_list, CompletionKind};
69 fn check(ra_fixture: &str, expect: Expect) {
70 let actual = completion_list(ra_fixture, CompletionKind::Reference);
71 expect.assert_eq(&actual);
75 fn test_struct_field_and_method_completion() {
92 fn test_struct_field_completion_self() {
95 struct S { the_field: (u32,) }
97 fn foo(self) { self.$0 }
108 fn test_struct_field_completion_autoderef() {
111 struct A { the_field: (u32, i32) }
113 fn foo(&self) { self.$0 }
117 fd the_field (u32, i32)
124 fn test_no_struct_field_completion_for_method_call() {
125 mark::check!(test_no_struct_field_completion_for_method_call);
128 struct A { the_field: u32 }
129 fn foo(a: A) { a.$0() }
136 fn test_visibility_filtering() {
143 pub(crate) crate_field: u32,
144 pub(crate) super_field: u32,
147 fn foo(a: inner::A) { a.$0 }
161 fn private_method(&self) {}
162 pub(crate) fn the_method(&self) {}
165 fn foo(a: A) { a.$0 }
168 me the_method() -> ()
174 fn test_union_field_completion() {
177 union U { field: u8, other: u16 }
178 fn foo(u: U) { u.$0 }
188 fn test_method_completion_only_fitting_impls() {
193 fn the_method(&self) {}
196 fn the_other_method(&self) {}
198 fn foo(a: A<u32>) { a.$0 }
201 me the_method() -> ()
207 fn test_trait_method_completion() {
211 trait Trait { fn the_method(&self); }
213 fn foo(a: A) { a.$0 }
216 me the_method() -> ()
222 fn test_trait_method_completion_deduplicated() {
226 trait Trait { fn the_method(&self); }
227 impl<T> Trait for T {}
228 fn foo(a: &A) { a.$0 }
231 me the_method() -> ()
237 fn completes_trait_method_from_other_module() {
242 pub trait Trait { fn the_method(&self); }
246 fn foo(a: A) { a.$0 }
249 me the_method() -> ()
255 fn test_no_non_self_method() {
271 fn test_tuple_field_completion() {
287 fn test_tuple_field_inference() {
291 impl S { pub fn blah(&self) {} }
297 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
309 fn test_completion_works_in_consts() {
312 struct A { the_field: u32 }
314 A { the_field: 92 }.$0
324 fn works_in_simple_macro_1() {
327 macro_rules! m { ($e:expr) => { $e } }
328 struct A { the_field: u32 }
340 fn works_in_simple_macro_2() {
341 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
344 macro_rules! m { ($e:expr) => { $e } }
345 struct A { the_field: u32 }
357 fn works_in_simple_macro_recursive_1() {
360 macro_rules! m { ($e:expr) => { $e } }
361 struct A { the_field: u32 }
373 fn macro_expansion_resilient() {
379 match $val { tmp => { tmp } }
381 // Trailing comma with single argument is ignored
382 ($val:expr,) => { $crate::d!($val) };
383 ($($val:expr),+ $(,)?) => {
384 ($($crate::d!($val)),+,)
387 struct A { the_field: u32 }
399 fn test_method_completion_issue_3547() {
404 pub fn the_method(&self) {}
412 me the_method() -> ()
418 fn completes_method_call_when_receiver_is_a_macro_call() {
422 impl S { fn foo(&self) {} }
423 macro_rules! make_s { () => { S }; }
424 fn main() { make_s!().f$0; }