2019-01-25 14:29:56 -05:00
|
|
|
use hir::{Ty, AdtDef, Docs};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-01-19 22:02:50 +08:00
|
|
|
use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind};
|
|
|
|
use crate::completion::completion_item::CompletionKind;
|
2019-01-08 22:33:36 +03:00
|
|
|
|
|
|
|
/// Complete dot accesses, i.e. fields or methods (currently only fields).
|
2019-01-15 21:09:51 +03:00
|
|
|
pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
2019-01-08 22:33:36 +03:00
|
|
|
let (function, receiver) = match (&ctx.function, ctx.dot_receiver) {
|
|
|
|
(Some(function), Some(receiver)) => (function, receiver),
|
2019-01-15 21:09:51 +03:00
|
|
|
_ => return,
|
2019-01-08 22:33:36 +03:00
|
|
|
};
|
2019-01-15 20:54:18 +03:00
|
|
|
let infer_result = function.infer(ctx.db);
|
2019-01-15 19:01:59 +03:00
|
|
|
let syntax_mapping = function.body_syntax_mapping(ctx.db);
|
2019-01-08 22:33:36 +03:00
|
|
|
let expr = match syntax_mapping.node_expr(receiver) {
|
|
|
|
Some(expr) => expr,
|
2019-01-15 21:09:51 +03:00
|
|
|
None => return,
|
2019-01-08 22:33:36 +03:00
|
|
|
};
|
|
|
|
let receiver_ty = infer_result[expr].clone();
|
2019-01-10 21:38:04 +03:00
|
|
|
if !ctx.is_call {
|
2019-01-15 20:43:37 +03:00
|
|
|
complete_fields(acc, ctx, receiver_ty.clone());
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-01-15 20:54:18 +03:00
|
|
|
complete_methods(acc, ctx, receiver_ty);
|
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-01-08 22:33:36 +03:00
|
|
|
for receiver in receiver.autoderef(ctx.db) {
|
|
|
|
match receiver {
|
2019-02-08 14:49:43 +03:00
|
|
|
Ty::Adt { def_id, ref substs, .. } => {
|
2019-01-24 17:54:18 +03:00
|
|
|
match def_id {
|
2019-01-24 19:04:02 +03:00
|
|
|
AdtDef::Struct(s) => {
|
2019-01-15 18:43:25 +03:00
|
|
|
for field in s.fields(ctx.db) {
|
2019-01-08 22:33:36 +03:00
|
|
|
CompletionItem::new(
|
|
|
|
CompletionKind::Reference,
|
2019-01-20 12:02:00 +08:00
|
|
|
ctx.source_range(),
|
2019-01-25 14:21:14 +03:00
|
|
|
field.name(ctx.db).to_string(),
|
2019-01-08 22:33:36 +03:00
|
|
|
)
|
|
|
|
.kind(CompletionItemKind::Field)
|
2019-01-25 14:21:14 +03:00
|
|
|
.detail(field.ty(ctx.db).subst(substs).to_string())
|
2019-01-25 14:29:56 -05:00
|
|
|
.set_documentation(field.docs(ctx.db))
|
2019-01-08 22:33:36 +03:00
|
|
|
.add_to(acc);
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 19:04:02 +03:00
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
// TODO unions
|
2019-01-24 19:04:02 +03:00
|
|
|
AdtDef::Enum(_) => (),
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ty::Tuple(fields) => {
|
2019-01-24 08:25:35 -05:00
|
|
|
for (i, ty) in fields.iter().enumerate() {
|
2019-01-20 12:02:00 +08:00
|
|
|
CompletionItem::new(
|
|
|
|
CompletionKind::Reference,
|
|
|
|
ctx.source_range(),
|
|
|
|
i.to_string(),
|
|
|
|
)
|
|
|
|
.kind(CompletionItemKind::Field)
|
2019-01-24 08:25:35 -05:00
|
|
|
.detail(ty.to_string())
|
2019-01-20 12:02:00 +08:00
|
|
|
.add_to(acc);
|
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-01-07 19:12:19 +01:00
|
|
|
receiver.iterate_methods(ctx.db, |func| {
|
|
|
|
let sig = func.signature(ctx.db);
|
2019-01-12 21:58:16 +01:00
|
|
|
if sig.has_self_param() {
|
2019-01-20 00:38:34 +08:00
|
|
|
CompletionItem::new(
|
|
|
|
CompletionKind::Reference,
|
2019-01-20 12:02:00 +08:00
|
|
|
ctx.source_range(),
|
2019-01-20 00:38:34 +08:00
|
|
|
sig.name().to_string(),
|
|
|
|
)
|
|
|
|
.from_function(ctx, func)
|
|
|
|
.kind(CompletionItemKind::Method)
|
|
|
|
.add_to(acc);
|
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 {
|
|
|
|
use crate::completion::*;
|
2019-01-19 22:02:50 +08:00
|
|
|
use crate::completion::completion_item::check_completion;
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-01-19 22:02:50 +08:00
|
|
|
fn check_ref_completion(name: &str, code: &str) {
|
|
|
|
check_completion(name, code, CompletionKind::Reference);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_field_completion() {
|
|
|
|
check_ref_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"struct_field_completion",
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
struct A { the_field: u32 }
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_field_completion_self() {
|
|
|
|
check_ref_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"struct_field_completion_self",
|
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.<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_field_completion_autoderef() {
|
|
|
|
check_ref_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"struct_field_completion_autoderef",
|
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.<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_struct_field_completion_for_method_call() {
|
|
|
|
check_ref_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"no_struct_field_completion_for_method_call",
|
2019-01-08 22:33:36 +03:00
|
|
|
r"
|
|
|
|
struct A { the_field: u32 }
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-01-07 19:12:19 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_method_completion() {
|
|
|
|
check_ref_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"method_completion",
|
2019-01-07 19:12:19 +01:00
|
|
|
r"
|
|
|
|
struct A {}
|
|
|
|
impl A {
|
|
|
|
fn the_method(&self) {}
|
|
|
|
}
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_non_self_method() {
|
|
|
|
check_ref_completion(
|
2019-01-19 22:02:50 +08:00
|
|
|
"no_non_self_method",
|
2019-01-07 19:12:19 +01:00
|
|
|
r"
|
|
|
|
struct A {}
|
|
|
|
impl A {
|
|
|
|
fn the_method() {}
|
|
|
|
}
|
|
|
|
fn foo(a: A) {
|
|
|
|
a.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-01-24 08:25:35 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple_field_completion() {
|
|
|
|
check_ref_completion(
|
|
|
|
"tuple_field_completion",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let b = (0, 3.14);
|
|
|
|
b.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|