2019-09-30 11:58:53 +03:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-09-13 00:34:52 +03:00
|
|
|
use hir::{Adt, Ty, TypeCtor};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-08-03 02:48:36 +07:00
|
|
|
use crate::completion::completion_item::CompletionKind;
|
2019-08-03 01:16:20 +07:00
|
|
|
use crate::{
|
|
|
|
completion::{completion_context::CompletionContext, completion_item::Completions},
|
|
|
|
CompletionItem,
|
|
|
|
};
|
2019-06-29 12:19:03 +02:00
|
|
|
use rustc_hash::FxHashSet;
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-08-03 01:15:43 +07:00
|
|
|
/// Complete dot accesses, i.e. fields or methods (and .await syntax).
|
2019-01-15 21:09:51 +03:00
|
|
|
pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
2019-08-04 08:03:17 +07:00
|
|
|
let dot_receiver = match &ctx.dot_receiver {
|
|
|
|
Some(expr) => expr,
|
|
|
|
_ => return,
|
|
|
|
};
|
2019-08-03 01:15:43 +07:00
|
|
|
|
2019-08-04 08:03:17 +07:00
|
|
|
let receiver_ty = match ctx.analyzer.type_of(ctx.db, &dot_receiver) {
|
|
|
|
Some(ty) => ty,
|
|
|
|
_ => return,
|
|
|
|
};
|
2019-08-03 01:15:43 +07:00
|
|
|
|
2019-08-04 08:03:17 +07:00
|
|
|
if !ctx.is_call {
|
|
|
|
complete_fields(acc, ctx, receiver_ty.clone());
|
|
|
|
}
|
|
|
|
complete_methods(acc, ctx, receiver_ty.clone());
|
|
|
|
|
|
|
|
// Suggest .await syntax for types that implement Future trait
|
|
|
|
if ctx.analyzer.impls_future(ctx.db, receiver_ty) {
|
|
|
|
CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await")
|
|
|
|
.detail("expr.await")
|
|
|
|
.insert_text("await")
|
|
|
|
.add_to(acc);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:43:37 +03:00
|
|
|
fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) {
|
2019-05-12 18:33:47 +02:00
|
|
|
for receiver in ctx.analyzer.autoderef(ctx.db, receiver) {
|
2019-06-03 10:01:10 -04:00
|
|
|
if let Ty::Apply(a_ty) = receiver {
|
|
|
|
match a_ty.ctor {
|
2019-09-13 00:34:52 +03:00
|
|
|
TypeCtor::Adt(Adt::Struct(s)) => {
|
2019-03-17 19:37:09 +01:00
|
|
|
for field in s.fields(ctx.db) {
|
|
|
|
acc.add_field(ctx, field, &a_ty.parameters);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 10:53:48 +03:00
|
|
|
// FIXME unions
|
2019-05-04 19:07:25 +02:00
|
|
|
TypeCtor::Tuple { .. } => {
|
2019-03-17 19:37:09 +01:00
|
|
|
for (i, ty) in a_ty.parameters.iter().enumerate() {
|
2019-08-23 15:55:21 +03:00
|
|
|
acc.add_tuple_field(ctx, i, ty);
|
2019-03-17 19:37:09 +01:00
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-03-17 19:37:09 +01:00
|
|
|
_ => {}
|
2019-06-03 10:01:10 -04:00
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:54:18 +03:00
|
|
|
fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) {
|
2019-06-29 12:19:03 +02:00
|
|
|
let mut seen_methods = FxHashSet::default();
|
2019-04-14 16:08:10 +02:00
|
|
|
ctx.analyzer.iterate_method_candidates(ctx.db, receiver, None, |_ty, func| {
|
2019-11-22 17:10:51 +03:00
|
|
|
if func.has_self_param(ctx.db) && seen_methods.insert(func.name(ctx.db)) {
|
2019-02-24 20:49:55 +03:00
|
|
|
acc.add_function(ctx, func);
|
2019-01-07 19:12:19 +01:00
|
|
|
}
|
2019-01-15 20:54:18 +03:00
|
|
|
None::<()>
|
|
|
|
});
|
2019-01-07 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-07-04 23:05:17 +03:00
|
|
|
use crate::completion::{do_completion, CompletionItem, CompletionKind};
|
2019-08-29 16:49:10 +03:00
|
|
|
use insta::assert_debug_snapshot;
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-04-10 13:47:20 +03:00
|
|
|
fn do_ref_completion(code: &str) -> Vec<CompletionItem> {
|
|
|
|
do_completion(code, CompletionKind::Reference)
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_field_completion() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-10 13:47:20 +03:00
|
|
|
do_ref_completion(
|
|
|
|
r"
|
|
|
|
struct A { the_field: u32 }
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
),
|
2019-05-24 01:46:23 +03:00
|
|
|
@r###"
|
2019-11-15 12:56:24 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "the_field",
|
|
|
|
source_range: [94; 94),
|
|
|
|
delete: [94; 94),
|
|
|
|
insert: "the_field",
|
|
|
|
kind: Field,
|
|
|
|
detail: "u32",
|
|
|
|
},
|
|
|
|
]
|
2019-05-24 01:46:23 +03:00
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_field_completion_self() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-10 13:47:20 +03:00
|
|
|
do_ref_completion(
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
2019-01-25 14:29:56 -05:00
|
|
|
struct A {
|
|
|
|
/// This is the_field
|
|
|
|
the_field: (u32,)
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
impl A {
|
|
|
|
fn foo(self) {
|
|
|
|
self.<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
2019-04-10 13:47:20 +03:00
|
|
|
),
|
2019-05-24 01:46:23 +03:00
|
|
|
@r###"
|
2019-10-10 13:03:20 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "foo()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [187; 187),
|
|
|
|
delete: [187; 187),
|
|
|
|
insert: "foo()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "foo",
|
|
|
|
detail: "fn foo(self)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "the_field",
|
|
|
|
source_range: [187; 187),
|
|
|
|
delete: [187; 187),
|
|
|
|
insert: "the_field",
|
|
|
|
kind: Field,
|
|
|
|
detail: "(u32,)",
|
|
|
|
documentation: Documentation(
|
|
|
|
"This is the_field",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
2019-05-24 01:46:23 +03:00
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_field_completion_autoderef() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-10 13:47:20 +03:00
|
|
|
do_ref_completion(
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
2019-01-09 18:46:02 +03:00
|
|
|
struct A { the_field: (u32, i32) }
|
2019-01-08 22:33:36 +03:00
|
|
|
impl A {
|
|
|
|
fn foo(&self) {
|
|
|
|
self.<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
2019-04-10 13:47:20 +03:00
|
|
|
),
|
2019-05-24 01:46:23 +03:00
|
|
|
@r###"
|
2019-10-10 13:03:20 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "foo()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [126; 126),
|
|
|
|
delete: [126; 126),
|
|
|
|
insert: "foo()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "foo",
|
|
|
|
detail: "fn foo(&self)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "the_field",
|
|
|
|
source_range: [126; 126),
|
|
|
|
delete: [126; 126),
|
|
|
|
insert: "the_field",
|
|
|
|
kind: Field,
|
|
|
|
detail: "(u32, i32)",
|
|
|
|
},
|
|
|
|
]
|
2019-05-24 01:46:23 +03:00
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_struct_field_completion_for_method_call() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-10 13:47:20 +03:00
|
|
|
do_ref_completion(
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
struct A { the_field: u32 }
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>()
|
|
|
|
}
|
|
|
|
",
|
2019-04-10 13:47:20 +03:00
|
|
|
),
|
|
|
|
@"[]"
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
2019-01-07 19:12:19 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_method_completion() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-10 13:47:20 +03:00
|
|
|
do_ref_completion(
|
2019-01-07 19:12:19 +01:00
|
|
|
r"
|
|
|
|
struct A {}
|
|
|
|
impl A {
|
|
|
|
fn the_method(&self) {}
|
|
|
|
}
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
|
|
|
",
|
2019-04-10 13:47:20 +03:00
|
|
|
),
|
2019-05-24 01:46:23 +03:00
|
|
|
@r###"
|
2019-10-10 13:03:20 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "the_method()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [144; 144),
|
|
|
|
delete: [144; 144),
|
|
|
|
insert: "the_method()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "the_method",
|
|
|
|
detail: "fn the_method(&self)",
|
|
|
|
},
|
|
|
|
]
|
2019-05-24 01:46:23 +03:00
|
|
|
"###
|
2019-01-07 19:12:19 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:08:10 +02:00
|
|
|
#[test]
|
|
|
|
fn test_trait_method_completion() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-14 16:08:10 +02:00
|
|
|
do_ref_completion(
|
|
|
|
r"
|
|
|
|
struct A {}
|
|
|
|
trait Trait { fn the_method(&self); }
|
|
|
|
impl Trait for A {}
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
),
|
2019-05-24 01:46:23 +03:00
|
|
|
@r###"
|
2019-10-10 13:03:20 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "the_method()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [151; 151),
|
|
|
|
delete: [151; 151),
|
|
|
|
insert: "the_method()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "the_method",
|
|
|
|
detail: "fn the_method(&self)",
|
|
|
|
},
|
|
|
|
]
|
2019-05-24 01:46:23 +03:00
|
|
|
"###
|
2019-04-14 16:08:10 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-29 12:19:03 +02:00
|
|
|
#[test]
|
|
|
|
fn test_trait_method_completion_deduplicated() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-29 12:19:03 +02:00
|
|
|
do_ref_completion(
|
|
|
|
r"
|
|
|
|
struct A {}
|
|
|
|
trait Trait { fn the_method(&self); }
|
|
|
|
impl<T> Trait for T {}
|
|
|
|
fn foo(a: &A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
),
|
|
|
|
@r###"
|
2019-10-10 13:03:20 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "the_method()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [155; 155),
|
|
|
|
delete: [155; 155),
|
|
|
|
insert: "the_method()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "the_method",
|
|
|
|
detail: "fn the_method(&self)",
|
|
|
|
},
|
|
|
|
]
|
2019-06-29 12:19:03 +02:00
|
|
|
"###
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-07 19:12:19 +01:00
|
|
|
#[test]
|
|
|
|
fn test_no_non_self_method() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-10 13:47:20 +03:00
|
|
|
do_ref_completion(
|
2019-01-07 19:12:19 +01:00
|
|
|
r"
|
|
|
|
struct A {}
|
|
|
|
impl A {
|
|
|
|
fn the_method() {}
|
|
|
|
}
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
|
|
|
",
|
2019-04-10 13:47:20 +03:00
|
|
|
),
|
|
|
|
@"[]"
|
2019-01-07 19:12:19 +01:00
|
|
|
);
|
|
|
|
}
|
2019-01-24 08:25:35 -05:00
|
|
|
|
2019-02-12 19:58:36 +02:00
|
|
|
#[test]
|
|
|
|
fn test_method_attr_filtering() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-10 13:47:20 +03:00
|
|
|
do_ref_completion(
|
2019-02-12 19:58:36 +02:00
|
|
|
r"
|
|
|
|
struct A {}
|
|
|
|
impl A {
|
|
|
|
#[inline]
|
|
|
|
fn the_method(&self) {
|
|
|
|
let x = 1;
|
|
|
|
let y = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
|
|
|
",
|
2019-04-10 13:47:20 +03:00
|
|
|
),
|
2019-05-24 01:46:23 +03:00
|
|
|
@r###"
|
2019-10-10 13:03:20 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "the_method()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [249; 249),
|
|
|
|
delete: [249; 249),
|
|
|
|
insert: "the_method()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "the_method",
|
|
|
|
detail: "fn the_method(&self)",
|
|
|
|
},
|
|
|
|
]
|
2019-05-24 01:46:23 +03:00
|
|
|
"###
|
2019-02-12 19:58:36 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-24 08:25:35 -05:00
|
|
|
#[test]
|
|
|
|
fn test_tuple_field_completion() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-10 13:47:20 +03:00
|
|
|
do_ref_completion(
|
2019-01-24 08:25:35 -05:00
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let b = (0, 3.14);
|
|
|
|
b.<|>
|
|
|
|
}
|
|
|
|
",
|
2019-04-10 13:47:20 +03:00
|
|
|
),
|
2019-05-24 01:46:23 +03:00
|
|
|
@r###"
|
2019-11-15 12:56:24 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "0",
|
|
|
|
source_range: [75; 75),
|
|
|
|
delete: [75; 75),
|
|
|
|
insert: "0",
|
|
|
|
kind: Field,
|
|
|
|
detail: "i32",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "1",
|
|
|
|
source_range: [75; 75),
|
|
|
|
delete: [75; 75),
|
|
|
|
insert: "1",
|
|
|
|
kind: Field,
|
|
|
|
detail: "f64",
|
|
|
|
},
|
|
|
|
]
|
2019-05-24 01:46:23 +03:00
|
|
|
"###
|
2019-01-24 08:25:35 -05:00
|
|
|
);
|
|
|
|
}
|
2019-04-05 22:59:55 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple_field_inference() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-10 13:47:20 +03:00
|
|
|
do_ref_completion(
|
2019-04-05 22:59:55 +02:00
|
|
|
r"
|
|
|
|
pub struct S;
|
|
|
|
impl S {
|
|
|
|
pub fn blah(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T(S);
|
|
|
|
|
|
|
|
impl T {
|
|
|
|
fn foo(&self) {
|
|
|
|
// FIXME: This doesn't work without the trailing `a` as `0.` is a float
|
|
|
|
self.0.a<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
2019-04-10 13:47:20 +03:00
|
|
|
),
|
2019-05-24 01:46:23 +03:00
|
|
|
@r###"
|
2019-10-10 13:03:20 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "blah()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [299; 300),
|
|
|
|
delete: [299; 300),
|
|
|
|
insert: "blah()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "blah",
|
|
|
|
detail: "pub fn blah(&self)",
|
|
|
|
},
|
|
|
|
]
|
2019-05-24 01:46:23 +03:00
|
|
|
"###
|
2019-04-11 16:22:10 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_completion_works_in_consts() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-04-11 16:22:10 +03:00
|
|
|
do_ref_completion(
|
|
|
|
r"
|
|
|
|
struct A { the_field: u32 }
|
|
|
|
const X: u32 = {
|
|
|
|
A { the_field: 92 }.<|>
|
|
|
|
};
|
|
|
|
",
|
|
|
|
),
|
2019-05-24 01:46:23 +03:00
|
|
|
@r###"
|
2019-11-15 12:56:24 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "the_field",
|
|
|
|
source_range: [106; 106),
|
|
|
|
delete: [106; 106),
|
|
|
|
insert: "the_field",
|
|
|
|
kind: Field,
|
|
|
|
detail: "u32",
|
|
|
|
},
|
|
|
|
]
|
2019-05-24 01:46:23 +03:00
|
|
|
"###
|
2019-04-05 22:59:55 +02:00
|
|
|
);
|
|
|
|
}
|
2019-08-03 01:15:43 +07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_completion_await_impls_future() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-08-03 02:40:59 +07:00
|
|
|
do_completion(
|
2019-08-03 01:53:51 +07:00
|
|
|
r###"
|
2019-08-03 17:07:20 +07:00
|
|
|
//- /main.rs
|
2019-08-03 01:15:43 +07:00
|
|
|
use std::future::*;
|
|
|
|
struct A {}
|
|
|
|
impl Future for A {}
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
2019-08-03 17:07:20 +07:00
|
|
|
|
|
|
|
//- /std/lib.rs
|
|
|
|
pub mod future {
|
|
|
|
pub trait Future {}
|
|
|
|
}
|
2019-08-03 02:40:59 +07:00
|
|
|
"###, CompletionKind::Keyword),
|
2019-08-03 01:15:43 +07:00
|
|
|
@r###"
|
2019-11-15 12:56:24 +03:00
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "await",
|
|
|
|
source_range: [74; 74),
|
|
|
|
delete: [74; 74),
|
|
|
|
insert: "await",
|
|
|
|
detail: "expr.await",
|
|
|
|
},
|
|
|
|
]
|
2019-08-03 01:15:43 +07:00
|
|
|
"###
|
|
|
|
)
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|