1 //! Completes references after dot (fields and method calls).
3 use hir::{HasVisibility, Type};
4 use rustc_hash::FxHashSet;
6 use crate::{context::CompletionContext, 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 {
15 let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) {
21 cov_mark::hit!(test_no_struct_field_completion_for_method_call);
23 complete_fields(acc, ctx, &receiver_ty);
25 complete_methods(acc, ctx, &receiver_ty);
28 fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) {
29 for receiver in receiver.autoderef(ctx.db) {
30 for (field, ty) in receiver.fields(ctx.db) {
31 if ctx.scope.module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) {
32 // Skip private field. FIXME: If the definition location of the
33 // field is editable, we should show the completion
36 acc.add_field(ctx, field, &ty);
38 for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
39 // FIXME: Handle visibility
40 acc.add_tuple_field(ctx, i, &ty);
45 fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) {
46 if let Some(krate) = ctx.krate {
47 let mut seen_methods = FxHashSet::default();
48 let traits_in_scope = ctx.scope.traits_in_scope();
49 receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
50 if func.self_param(ctx.db).is_some()
51 && ctx.scope.module().map_or(true, |m| func.is_visible_from(ctx.db, m))
52 && seen_methods.insert(func.name(ctx.db))
54 acc.add_method(ctx, func, None);
63 use expect_test::{expect, Expect};
65 use crate::{test_utils::completion_list, CompletionKind};
67 fn check(ra_fixture: &str, expect: Expect) {
68 let actual = completion_list(ra_fixture, CompletionKind::Reference);
69 expect.assert_eq(&actual);
73 fn test_struct_field_and_method_completion() {
90 fn test_struct_field_completion_self() {
93 struct S { the_field: (u32,) }
95 fn foo(self) { self.$0 }
106 fn test_struct_field_completion_autoderef() {
109 struct A { the_field: (u32, i32) }
111 fn foo(&self) { self.$0 }
115 fd the_field (u32, i32)
122 fn test_no_struct_field_completion_for_method_call() {
123 cov_mark::check!(test_no_struct_field_completion_for_method_call);
126 struct A { the_field: u32 }
127 fn foo(a: A) { a.$0() }
134 fn test_visibility_filtering() {
141 pub(crate) crate_field: u32,
142 pub(crate) super_field: u32,
145 fn foo(a: inner::A) { a.$0 }
159 fn private_method(&self) {}
160 pub(crate) fn the_method(&self) {}
163 fn foo(a: A) { a.$0 }
166 me the_method() fn(&self)
172 fn test_union_field_completion() {
175 union U { field: u8, other: u16 }
176 fn foo(u: U) { u.$0 }
186 fn test_method_completion_only_fitting_impls() {
191 fn the_method(&self) {}
194 fn the_other_method(&self) {}
196 fn foo(a: A<u32>) { a.$0 }
199 me the_method() fn(&self)
205 fn test_trait_method_completion() {
209 trait Trait { fn the_method(&self); }
211 fn foo(a: A) { a.$0 }
214 me the_method() fn(&self)
220 fn test_trait_method_completion_deduplicated() {
224 trait Trait { fn the_method(&self); }
225 impl<T> Trait for T {}
226 fn foo(a: &A) { a.$0 }
229 me the_method() fn(&self)
235 fn completes_trait_method_from_other_module() {
240 pub trait Trait { fn the_method(&self); }
244 fn foo(a: A) { a.$0 }
247 me the_method() fn(&self)
253 fn test_no_non_self_method() {
269 fn test_tuple_field_completion() {
285 fn test_tuple_field_inference() {
289 impl S { pub fn blah(&self) {} }
295 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
307 fn test_completion_works_in_consts() {
310 struct A { the_field: u32 }
312 A { the_field: 92 }.$0
322 fn works_in_simple_macro_1() {
325 macro_rules! m { ($e:expr) => { $e } }
326 struct A { the_field: u32 }
338 fn works_in_simple_macro_2() {
339 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
342 macro_rules! m { ($e:expr) => { $e } }
343 struct A { the_field: u32 }
355 fn works_in_simple_macro_recursive_1() {
358 macro_rules! m { ($e:expr) => { $e } }
359 struct A { the_field: u32 }
371 fn macro_expansion_resilient() {
377 match $val { tmp => { tmp } }
379 // Trailing comma with single argument is ignored
380 ($val:expr,) => { $crate::d!($val) };
381 ($($val:expr),+ $(,)?) => {
382 ($($crate::d!($val)),+,)
385 struct A { the_field: u32 }
397 fn test_method_completion_issue_3547() {
402 pub fn the_method(&self) {}
410 me the_method() fn(&self)
416 fn completes_method_call_when_receiver_is_a_macro_call() {
420 impl S { fn foo(&self) {} }
421 macro_rules! make_s { () => { S }; }
422 fn main() { make_s!().f$0; }
431 fn completes_after_macro_call_in_submodule() {
439 #[derive(Debug, Default)]
446 let goo: Template2 = Template2 {};
453 me private() fn(&self)
470 fn foo(&self) -> &[u8] { loop {} }
474 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
477 fn f(&mut self, v: Foo) {
478 self.indented(|this| v.$0)