713 lines
15 KiB
Rust
Raw Normal View History

2020-07-04 10:03:27 +02:00
//! Completes references after dot (fields and method calls).
use either::Either;
use rustc_hash::FxHashSet;
2019-01-08 22:33:36 +03:00
use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions};
2019-01-08 22:33:36 +03:00
2020-07-04 10:36:12 +02:00
/// Complete dot accesses, i.e. fields or methods.
2020-10-25 10:59:15 +03:00
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
let dot_receiver = match ctx.dot_receiver() {
2019-08-04 08:03:17 +07:00
Some(expr) => expr,
_ => return complete_undotted_self(acc, ctx),
2019-08-04 08:03:17 +07:00
};
2021-06-13 09:24:16 +05:30
let receiver_ty = match ctx.sema.type_of_expr(dot_receiver) {
Some(ty) => ty.original,
2019-08-04 08:03:17 +07:00
_ => return,
};
if matches!(ctx.completion_location, Some(ImmediateLocation::MethodCall { .. })) {
2021-03-08 22:19:44 +02:00
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
2020-07-04 10:36:12 +02:00
} else {
complete_fields(ctx, &receiver_ty, |field, ty| match field {
Either::Left(field) => acc.add_field(ctx, None, field, &ty),
Either::Right(tuple_idx) => acc.add_tuple_field(ctx, None, tuple_idx, &ty),
2020-01-14 14:42:52 +01:00
});
}
complete_methods(ctx, &receiver_ty, |func| acc.add_method(ctx, func, None, None));
}
fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.config.enable_self_on_the_fly {
return;
}
if !ctx.is_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() {
return;
}
if let Some(func) = ctx.function_def.as_ref().and_then(|fn_| ctx.sema.to_def(fn_)) {
if let Some(self_) = func.self_param(ctx.db) {
let ty = self_.ty(ctx.db);
complete_fields(ctx, &ty, |field, ty| match field {
either::Either::Left(field) => {
acc.add_field(ctx, Some(hir::known::SELF_PARAM), field, &ty)
}
either::Either::Right(tuple_idx) => {
acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), tuple_idx, &ty)
}
});
complete_methods(ctx, &ty, |func| {
acc.add_method(ctx, func, Some(hir::known::SELF_PARAM), None)
});
}
}
}
fn complete_fields(
ctx: &CompletionContext,
receiver: &hir::Type,
mut f: impl FnMut(Either<hir::Field, usize>, hir::Type),
) {
for receiver in receiver.autoderef(ctx.db) {
for (field, ty) in receiver.fields(ctx.db) {
f(Either::Left(field), ty);
}
for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
2021-07-23 20:20:41 +02:00
// Tuple fields are always public (tuple struct fields are handled above).
f(Either::Right(i), ty);
}
}
}
fn complete_methods(
ctx: &CompletionContext,
receiver: &hir::Type,
mut f: impl FnMut(hir::Function),
) {
if let Some(krate) = ctx.krate {
let mut seen_methods = FxHashSet::default();
let traits_in_scope = ctx.scope.visible_traits();
receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
2021-11-16 13:25:23 +01:00
if func.self_param(ctx.db).is_some() && seen_methods.insert(func.name(ctx.db)) {
f(func);
}
None::<()>
});
}
2019-01-07 19:12:19 +01:00
}
2019-01-08 22:33:36 +03:00
#[cfg(test)]
mod tests {
2020-08-21 13:19:31 +02:00
use expect_test::{expect, Expect};
2020-07-04 10:36:12 +02:00
use crate::tests::{check_edit, completion_list_no_kw};
2019-01-08 22:33:36 +03:00
2020-07-04 10:36:12 +02:00
fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list_no_kw(ra_fixture);
2020-07-04 10:36:12 +02:00
expect.assert_eq(&actual);
2019-01-08 22:33:36 +03:00
}
#[test]
2020-07-04 10:36:12 +02:00
fn test_struct_field_and_method_completion() {
check(
r#"
struct S { foo: u32 }
impl S {
fn bar(&self) {}
}
2021-01-06 20:15:48 +00:00
fn foo(s: S) { s.$0 }
2020-07-04 10:36:12 +02:00
"#,
expect![[r#"
2020-07-04 19:03:58 +02:00
fd foo u32
me bar() fn(&self)
2020-07-04 10:36:12 +02:00
"#]],
2019-01-08 22:33:36 +03:00
);
}
#[test]
fn test_struct_field_completion_self() {
2020-07-04 10:36:12 +02:00
check(
r#"
struct S { the_field: (u32,) }
impl S {
2021-01-06 20:15:48 +00:00
fn foo(self) { self.$0 }
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#"
fd the_field (u32,)
me foo() fn(self)
2020-07-04 10:36:12 +02:00
"#]],
)
2019-01-08 22:33:36 +03:00
}
#[test]
fn test_struct_field_completion_autoderef() {
2020-07-04 10:36:12 +02:00
check(
r#"
struct A { the_field: (u32, i32) }
impl A {
2021-01-06 20:15:48 +00:00
fn foo(&self) { self.$0 }
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#"
fd the_field (u32, i32)
me foo() fn(&self)
2020-07-04 10:36:12 +02:00
"#]],
)
2019-01-08 22:33:36 +03:00
}
#[test]
fn test_no_struct_field_completion_for_method_call() {
2021-03-08 22:19:44 +02:00
cov_mark::check!(test_no_struct_field_completion_for_method_call);
2020-07-04 10:36:12 +02:00
check(
r#"
struct A { the_field: u32 }
2021-01-06 20:15:48 +00:00
fn foo(a: A) { a.$0() }
2020-07-04 10:36:12 +02:00
"#,
expect![[r#""#]],
2019-01-08 22:33:36 +03:00
);
}
2019-01-07 19:12:19 +01:00
#[test]
2020-07-04 10:36:12 +02:00
fn test_visibility_filtering() {
check(
r#"
2021-07-23 19:57:16 +02:00
//- /lib.rs crate:lib new_source_root:local
pub mod m {
2020-07-04 10:36:12 +02:00
pub struct A {
private_field: u32,
pub pub_field: u32,
pub(crate) crate_field: u32,
2021-07-23 19:57:16 +02:00
pub(super) super_field: u32,
2020-04-28 22:45:46 +02:00
}
2020-07-04 10:36:12 +02:00
}
2021-07-23 19:57:16 +02:00
//- /main.rs crate:main deps:lib new_source_root:local
fn foo(a: lib::m::A) { a.$0 }
2020-07-04 10:36:12 +02:00
"#,
expect![[r#"
2021-07-23 19:57:16 +02:00
fd private_field u32
fd pub_field u32
fd crate_field u32
fd super_field u32
2020-07-04 10:36:12 +02:00
"#]],
);
2020-07-04 10:36:12 +02:00
check(
r#"
2021-07-23 19:57:16 +02:00
//- /lib.rs crate:lib new_source_root:library
pub mod m {
pub struct A {
private_field: u32,
pub pub_field: u32,
pub(crate) crate_field: u32,
pub(super) super_field: u32,
}
}
//- /main.rs crate:main deps:lib new_source_root:local
fn foo(a: lib::m::A) { a.$0 }
"#,
expect![[r#"
fd pub_field u32
"#]],
);
check(
r#"
//- /lib.rs crate:lib new_source_root:library
pub mod m {
pub struct A(
i32,
pub f64,
);
}
//- /main.rs crate:main deps:lib new_source_root:local
fn foo(a: lib::m::A) { a.$0 }
"#,
expect![[r#"
fd 1 f64
"#]],
);
2021-07-23 19:57:16 +02:00
check(
r#"
//- /lib.rs crate:lib new_source_root:local
pub struct A {}
2020-07-04 10:36:12 +02:00
mod m {
impl super::A {
fn private_method(&self) {}
2021-07-23 19:57:16 +02:00
pub(crate) fn crate_method(&self) {}
pub fn pub_method(&self) {}
2020-07-04 10:36:12 +02:00
}
}
2021-07-23 19:57:16 +02:00
//- /main.rs crate:main deps:lib new_source_root:local
fn foo(a: lib::A) { a.$0 }
2020-07-04 10:36:12 +02:00
"#,
expect![[r#"
2021-07-23 19:57:16 +02:00
me private_method() fn(&self)
me crate_method() fn(&self)
me pub_method() fn(&self)
"#]],
);
check(
r#"
//- /lib.rs crate:lib new_source_root:library
pub struct A {}
mod m {
impl super::A {
fn private_method(&self) {}
pub(crate) fn crate_method(&self) {}
pub fn pub_method(&self) {}
}
}
//- /main.rs crate:main deps:lib new_source_root:local
fn foo(a: lib::A) { a.$0 }
"#,
expect![[r#"
me pub_method() fn(&self)
2020-07-04 10:36:12 +02:00
"#]],
2019-01-07 19:12:19 +01:00
);
}
#[test]
fn test_doc_hidden_filtering() {
check(
r#"
//- /lib.rs crate:lib deps:dep
fn foo(a: dep::A) { a.$0 }
//- /dep.rs crate:dep
pub struct A {
#[doc(hidden)]
pub hidden_field: u32,
pub pub_field: u32,
}
impl A {
pub fn pub_method(&self) {}
#[doc(hidden)]
pub fn hidden_method(&self) {}
}
"#,
expect![[r#"
fd pub_field u32
me pub_method() fn(&self)
2021-07-23 16:45:14 +02:00
"#]],
)
}
#[test]
2020-07-04 10:36:12 +02:00
fn test_union_field_completion() {
check(
r#"
union U { field: u8, other: u16 }
2021-01-06 20:15:48 +00:00
fn foo(u: U) { u.$0 }
2020-07-04 10:36:12 +02:00
"#,
expect![[r#"
fd field u8
fd other u16
"#]],
);
}
#[test]
2020-07-04 10:36:12 +02:00
fn test_method_completion_only_fitting_impls() {
check(
r#"
struct A<T> {}
impl A<u32> {
fn the_method(&self) {}
}
impl A<i32> {
fn the_other_method(&self) {}
}
2021-01-06 20:15:48 +00:00
fn foo(a: A<u32>) { a.$0 }
2020-07-04 10:36:12 +02:00
"#,
expect![[r#"
me the_method() fn(&self)
2020-07-04 10:36:12 +02:00
"#]],
)
}
#[test]
fn test_trait_method_completion() {
2020-07-04 10:36:12 +02:00
check(
r#"
struct A {}
trait Trait { fn the_method(&self); }
impl Trait for A {}
2021-01-06 20:15:48 +00:00
fn foo(a: A) { a.$0 }
2020-07-04 10:36:12 +02:00
"#,
expect![[r#"
2021-07-05 16:44:44 +04:30
me the_method() (as Trait) fn(&self)
2020-07-04 10:36:12 +02:00
"#]],
);
check_edit(
"the_method",
r#"
struct A {}
trait Trait { fn the_method(&self); }
impl Trait for A {}
fn foo(a: A) { a.$0 }
"#,
r#"
struct A {}
trait Trait { fn the_method(&self); }
impl Trait for A {}
fn foo(a: A) { a.the_method()$0 }
"#,
);
}
2019-06-29 12:19:03 +02:00
#[test]
fn test_trait_method_completion_deduplicated() {
2020-07-04 10:36:12 +02:00
check(
r"
struct A {}
trait Trait { fn the_method(&self); }
impl<T> Trait for T {}
2021-01-06 20:15:48 +00:00
fn foo(a: &A) { a.$0 }
2020-07-04 10:36:12 +02:00
",
expect![[r#"
2021-07-05 16:44:44 +04:30
me the_method() (as Trait) fn(&self)
2020-07-04 10:36:12 +02:00
"#]],
2019-06-29 12:19:03 +02:00
);
}
#[test]
fn completes_trait_method_from_other_module() {
2020-07-04 10:36:12 +02:00
check(
2019-01-07 19:12:19 +01:00
r"
2020-07-04 10:36:12 +02:00
struct A {}
mod m {
pub trait Trait { fn the_method(&self); }
}
use m::Trait;
impl Trait for A {}
2021-01-06 20:15:48 +00:00
fn foo(a: A) { a.$0 }
2020-07-04 10:36:12 +02:00
",
expect![[r#"
2021-07-05 16:44:44 +04:30
me the_method() (as Trait) fn(&self)
2020-07-04 10:36:12 +02:00
"#]],
2019-01-07 19:12:19 +01:00
);
}
2019-01-24 08:25:35 -05:00
#[test]
2020-07-04 10:36:12 +02:00
fn test_no_non_self_method() {
check(
r#"
struct A {}
impl A {
fn the_method() {}
}
fn foo(a: A) {
2021-01-06 20:15:48 +00:00
a.$0
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#""#]],
);
}
2019-01-24 08:25:35 -05:00
#[test]
fn test_tuple_field_completion() {
2020-07-04 10:36:12 +02:00
check(
r#"
fn foo() {
let b = (0, 3.14);
2021-01-06 20:15:48 +00:00
b.$0
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#"
fd 0 i32
fd 1 f64
2020-07-04 10:36:12 +02:00
"#]],
);
}
#[test]
fn test_tuple_struct_field_completion() {
check(
r#"
struct S(i32, f64);
fn foo() {
let b = S(0, 3.14);
b.$0
}
"#,
expect![[r#"
fd 0 i32
fd 1 f64
"#]],
);
2019-01-24 08:25:35 -05:00
}
#[test]
fn test_tuple_field_inference() {
2020-07-04 10:36:12 +02:00
check(
r#"
pub struct S;
impl S { pub fn blah(&self) {} }
2020-07-04 10:36:12 +02:00
struct T(S);
2020-07-04 10:36:12 +02:00
impl T {
fn foo(&self) {
// FIXME: This doesn't work without the trailing `a` as `0.` is a float
2021-01-06 20:15:48 +00:00
self.0.a$0
}
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#"
me blah() fn(&self)
2020-07-04 10:36:12 +02:00
"#]],
);
}
#[test]
2020-07-04 10:36:12 +02:00
fn test_completion_works_in_consts() {
check(
r#"
struct A { the_field: u32 }
const X: u32 = {
2021-01-06 20:15:48 +00:00
A { the_field: 92 }.$0
2020-07-04 10:36:12 +02:00
};
"#,
expect![[r#"
fd the_field u32
"#]],
2020-02-29 20:53:01 -08:00
);
}
2020-03-07 15:27:03 +01:00
#[test]
fn works_in_simple_macro_1() {
2020-07-04 10:36:12 +02:00
check(
r#"
macro_rules! m { ($e:expr) => { $e } }
struct A { the_field: u32 }
fn foo(a: A) {
2021-01-06 20:15:48 +00:00
m!(a.x$0)
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#"
fd the_field u32
"#]],
2020-03-07 15:27:03 +01:00
);
}
#[test]
fn works_in_simple_macro_2() {
// this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
2020-07-04 10:36:12 +02:00
check(
r#"
macro_rules! m { ($e:expr) => { $e } }
struct A { the_field: u32 }
fn foo(a: A) {
2021-01-06 20:15:48 +00:00
m!(a.$0)
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#"
fd the_field u32
"#]],
2020-03-07 15:27:03 +01:00
);
}
#[test]
fn works_in_simple_macro_recursive_1() {
2020-07-04 10:36:12 +02:00
check(
r#"
macro_rules! m { ($e:expr) => { $e } }
struct A { the_field: u32 }
fn foo(a: A) {
2021-01-06 20:15:48 +00:00
m!(m!(m!(a.x$0)))
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#"
fd the_field u32
"#]],
2020-03-07 15:27:03 +01:00
);
}
#[test]
fn macro_expansion_resilient() {
2020-07-04 10:36:12 +02:00
check(
r#"
macro_rules! d {
2020-07-04 10:36:12 +02:00
() => {};
($val:expr) => {
match $val { tmp => { tmp } }
};
// Trailing comma with single argument is ignored
($val:expr,) => { $crate::d!($val) };
2020-07-04 10:36:12 +02:00
($($val:expr),+ $(,)?) => {
($($crate::d!($val)),+,)
2020-07-04 10:36:12 +02:00
};
}
struct A { the_field: u32 }
fn foo(a: A) {
d!(a.$0)
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#"
fd the_field u32
"#]],
);
}
#[test]
2020-07-04 10:36:12 +02:00
fn test_method_completion_issue_3547() {
check(
r#"
struct HashSet<T> {}
impl<T> HashSet<T> {
pub fn the_method(&self) {}
}
fn foo() {
let s: HashSet<_>;
2021-01-06 20:15:48 +00:00
s.$0
2020-07-04 10:36:12 +02:00
}
"#,
expect![[r#"
me the_method() fn(&self)
2020-07-04 10:36:12 +02:00
"#]],
);
}
#[test]
fn completes_method_call_when_receiver_is_a_macro_call() {
check(
r#"
struct S;
impl S { fn foo(&self) {} }
macro_rules! make_s { () => { S }; }
2021-01-06 20:15:48 +00:00
fn main() { make_s!().f$0; }
"#,
expect![[r#"
me foo() fn(&self)
"#]],
)
}
2021-02-23 14:54:01 +01:00
#[test]
fn completes_after_macro_call_in_submodule() {
check(
r#"
macro_rules! empty {
() => {};
}
mod foo {
#[derive(Debug, Default)]
struct Template2 {}
impl Template2 {
fn private(&self) {}
}
fn baz() {
let goo: Template2 = Template2 {};
empty!();
goo.$0
}
}
"#,
expect![[r#"
me private() fn(&self)
2021-02-23 14:54:01 +01:00
"#]],
);
}
2021-05-23 12:52:41 +02:00
#[test]
fn issue_8931() {
check(
r#"
2021-06-18 22:14:39 +03:00
//- minicore: fn
2021-05-23 12:52:41 +02:00
struct S;
struct Foo;
impl Foo {
fn foo(&self) -> &[u8] { loop {} }
}
impl S {
fn indented(&mut self, f: impl FnOnce(&mut Self)) {
}
fn f(&mut self, v: Foo) {
self.indented(|this| v.$0)
}
}
"#,
expect![[r#"
me foo() fn(&self) -> &[u8]
2021-05-23 12:52:41 +02:00
"#]],
);
}
#[test]
fn completes_bare_fields_and_methods_in_methods() {
check(
r#"
struct Foo { field: i32 }
impl Foo { fn foo(&self) { $0 } }"#,
expect![[r#"
lc self &Foo
sp Self
st Foo
bt u32
fd self.field i32
me self.foo() fn(&self)
"#]],
);
check(
r#"
struct Foo(i32);
impl Foo { fn foo(&mut self) { $0 } }"#,
expect![[r#"
lc self &mut Foo
sp Self
st Foo
bt u32
fd self.0 i32
me self.foo() fn(&mut self)
"#]],
);
}
#[test]
fn macro_completion_after_dot() {
check(
r#"
macro_rules! m {
($e:expr) => { $e };
}
struct Completable;
impl Completable {
fn method(&self) {}
}
fn f() {
let c = Completable;
m!(c.$0);
}
"#,
expect![[r#"
me method() fn(&self)
"#]],
);
}
#[test]
fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
check(
r#"
//- minicore: iterator, sized
struct Vec<T>;
impl<T> IntoIterator for Vec<T> {
type Item = ();
type IntoIter = ();
fn into_iter(self);
}
fn main() {
let x: Vec<_>;
x.$0;
}
"#,
expect![[r#"
me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
"#]],
)
}
2019-01-08 22:33:36 +03:00
}