2019-02-24 18:51:38 +03:00
|
|
|
//! This modules takes care of rendering various defenitions as completion items.
|
2019-09-03 13:10:00 +02:00
|
|
|
use hir::{Docs, HasSource, HirDisplay, PerNs, Resolution, Ty, TypeWalk};
|
2019-02-24 20:56:19 +03:00
|
|
|
use join_to_string::join;
|
2019-02-24 21:21:31 +03:00
|
|
|
use ra_syntax::ast::NameOwner;
|
2019-07-04 23:05:17 +03:00
|
|
|
use test_utils::tested_by;
|
2019-02-24 18:51:38 +03:00
|
|
|
|
2019-02-24 19:34:27 +03:00
|
|
|
use crate::completion::{
|
2019-07-04 23:05:17 +03:00
|
|
|
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
|
2019-04-08 11:44:23 +03:00
|
|
|
};
|
|
|
|
|
2019-07-04 23:05:17 +03:00
|
|
|
use crate::display::{const_label, function_label, type_label};
|
2019-02-24 18:51:38 +03:00
|
|
|
|
|
|
|
impl Completions {
|
|
|
|
pub(crate) fn add_field(
|
|
|
|
&mut self,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
field: hir::StructField,
|
|
|
|
substs: &hir::Substs,
|
|
|
|
) {
|
2019-02-24 20:49:55 +03:00
|
|
|
CompletionItem::new(
|
|
|
|
CompletionKind::Reference,
|
|
|
|
ctx.source_range(),
|
|
|
|
field.name(ctx.db).to_string(),
|
|
|
|
)
|
|
|
|
.kind(CompletionItemKind::Field)
|
2019-03-14 22:03:39 +01:00
|
|
|
.detail(field.ty(ctx.db).subst(substs).display(ctx.db).to_string())
|
2019-02-24 20:49:55 +03:00
|
|
|
.set_documentation(field.docs(ctx.db))
|
|
|
|
.add_to(self);
|
2019-02-24 18:51:38 +03:00
|
|
|
}
|
|
|
|
|
2019-08-23 15:55:21 +03:00
|
|
|
pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &hir::Ty) {
|
2019-02-24 20:49:55 +03:00
|
|
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), field.to_string())
|
2019-02-24 18:51:38 +03:00
|
|
|
.kind(CompletionItemKind::Field)
|
2019-03-14 22:03:39 +01:00
|
|
|
.detail(ty.display(ctx.db).to_string())
|
2019-02-24 18:51:38 +03:00
|
|
|
.add_to(self);
|
|
|
|
}
|
2019-02-24 19:34:27 +03:00
|
|
|
|
2019-02-24 21:21:31 +03:00
|
|
|
pub(crate) fn add_resolution(
|
|
|
|
&mut self,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
local_name: String,
|
2019-02-24 21:34:38 +03:00
|
|
|
resolution: &PerNs<Resolution>,
|
2019-02-24 21:21:31 +03:00
|
|
|
) {
|
2019-02-24 21:34:38 +03:00
|
|
|
use hir::ModuleDef::*;
|
|
|
|
|
|
|
|
let def = resolution.as_ref().take_types().or_else(|| resolution.as_ref().take_values());
|
|
|
|
let def = match def {
|
|
|
|
None => {
|
|
|
|
self.add(CompletionItem::new(
|
|
|
|
CompletionKind::Reference,
|
|
|
|
ctx.source_range(),
|
|
|
|
local_name,
|
|
|
|
));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
2019-05-30 16:10:07 +03:00
|
|
|
let mut completion_kind = CompletionKind::Reference;
|
2019-02-24 21:34:38 +03:00
|
|
|
let (kind, docs) = match def {
|
|
|
|
Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)),
|
|
|
|
Resolution::Def(Function(func)) => {
|
|
|
|
return self.add_function_with_name(ctx, Some(local_name), *func);
|
|
|
|
}
|
|
|
|
Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)),
|
2019-05-23 20:18:47 +03:00
|
|
|
Resolution::Def(Union(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)),
|
2019-02-24 21:34:38 +03:00
|
|
|
Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)),
|
|
|
|
Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
|
|
|
|
Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)),
|
|
|
|
Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)),
|
|
|
|
Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)),
|
2019-02-24 21:36:49 +01:00
|
|
|
Resolution::Def(TypeAlias(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
|
2019-05-30 16:10:07 +03:00
|
|
|
Resolution::Def(BuiltinType(..)) => {
|
|
|
|
completion_kind = CompletionKind::BuiltinType;
|
|
|
|
(CompletionItemKind::BuiltinType, None)
|
|
|
|
}
|
2019-02-24 21:34:38 +03:00
|
|
|
Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None),
|
|
|
|
Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None),
|
|
|
|
Resolution::SelfType(..) => (
|
|
|
|
CompletionItemKind::TypeParam, // (does this need its own kind?)
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
};
|
2019-07-23 19:24:54 +03:00
|
|
|
|
|
|
|
let mut completion_item =
|
|
|
|
CompletionItem::new(completion_kind, ctx.source_range(), local_name);
|
|
|
|
if let Resolution::LocalBinding(pat_id) = def {
|
|
|
|
let ty = ctx
|
|
|
|
.analyzer
|
|
|
|
.type_of_pat_by_id(ctx.db, pat_id.clone())
|
|
|
|
.filter(|t| t != &Ty::Unknown)
|
|
|
|
.map(|t| t.display(ctx.db).to_string());
|
|
|
|
completion_item = completion_item.set_detail(ty);
|
|
|
|
};
|
|
|
|
completion_item.kind(kind).set_documentation(docs).add_to(self)
|
2019-02-24 21:21:31 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 20:49:55 +03:00
|
|
|
pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) {
|
2019-02-24 21:34:38 +03:00
|
|
|
self.add_function_with_name(ctx, None, func)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_function_with_name(
|
|
|
|
&mut self,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
name: Option<String>,
|
|
|
|
func: hir::Function,
|
|
|
|
) {
|
2019-06-18 20:07:35 +03:00
|
|
|
let data = func.data(ctx.db);
|
|
|
|
let name = name.unwrap_or_else(|| data.name().to_string());
|
2019-06-11 16:49:56 +03:00
|
|
|
let ast_node = func.source(ctx.db).ast;
|
2019-02-24 21:46:04 +03:00
|
|
|
let detail = function_label(&ast_node);
|
2019-02-24 19:34:27 +03:00
|
|
|
|
2019-02-24 21:34:38 +03:00
|
|
|
let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name)
|
2019-06-18 20:07:35 +03:00
|
|
|
.kind(if data.has_self_param() {
|
2019-02-24 21:34:38 +03:00
|
|
|
CompletionItemKind::Method
|
|
|
|
} else {
|
|
|
|
CompletionItemKind::Function
|
|
|
|
})
|
|
|
|
.set_documentation(func.docs(ctx.db))
|
2019-04-08 11:44:23 +03:00
|
|
|
.detail(detail);
|
2019-02-24 19:34:27 +03:00
|
|
|
// If not an import, add parenthesis automatically.
|
2019-08-22 14:44:16 +03:00
|
|
|
if ctx.use_item_syntax.is_none()
|
|
|
|
&& !ctx.is_call
|
|
|
|
&& ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
|
|
|
|
{
|
2019-02-24 19:34:27 +03:00
|
|
|
tested_by!(inserts_parens_for_function_calls);
|
|
|
|
let snippet =
|
2019-06-18 20:07:35 +03:00
|
|
|
if data.params().is_empty() || data.has_self_param() && data.params().len() == 1 {
|
|
|
|
format!("{}()$0", data.name())
|
2019-02-24 19:34:27 +03:00
|
|
|
} else {
|
2019-06-18 20:07:35 +03:00
|
|
|
format!("{}($0)", data.name())
|
2019-02-24 19:34:27 +03:00
|
|
|
};
|
|
|
|
builder = builder.insert_snippet(snippet);
|
|
|
|
}
|
|
|
|
self.add(builder)
|
|
|
|
}
|
2019-02-24 20:56:19 +03:00
|
|
|
|
2019-02-24 21:21:31 +03:00
|
|
|
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
|
2019-06-11 17:13:20 +03:00
|
|
|
let ast_node = constant.source(ctx.db).ast;
|
2019-02-24 21:46:04 +03:00
|
|
|
let name = match ast_node.name() {
|
2019-02-24 21:21:31 +03:00
|
|
|
Some(name) => name,
|
|
|
|
_ => return,
|
|
|
|
};
|
2019-02-24 21:46:04 +03:00
|
|
|
let detail = const_label(&ast_node);
|
|
|
|
|
2019-02-24 21:21:31 +03:00
|
|
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
|
2019-02-24 21:46:04 +03:00
|
|
|
.kind(CompletionItemKind::Const)
|
|
|
|
.set_documentation(constant.docs(ctx.db))
|
|
|
|
.detail(detail)
|
2019-02-24 21:21:31 +03:00
|
|
|
.add_to(self);
|
|
|
|
}
|
|
|
|
|
2019-02-24 21:36:49 +01:00
|
|
|
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
|
2019-06-11 17:25:55 +03:00
|
|
|
let type_def = type_alias.source(ctx.db).ast;
|
2019-02-24 21:21:31 +03:00
|
|
|
let name = match type_def.name() {
|
|
|
|
Some(name) => name,
|
|
|
|
_ => return,
|
|
|
|
};
|
2019-06-11 17:25:55 +03:00
|
|
|
let detail = type_label(&type_def);
|
2019-02-24 21:46:04 +03:00
|
|
|
|
2019-02-24 21:21:31 +03:00
|
|
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
|
2019-02-24 21:46:04 +03:00
|
|
|
.kind(CompletionItemKind::TypeAlias)
|
|
|
|
.set_documentation(type_alias.docs(ctx.db))
|
|
|
|
.detail(detail)
|
2019-02-24 21:21:31 +03:00
|
|
|
.add_to(self);
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:56:19 +03:00
|
|
|
pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) {
|
|
|
|
let name = match variant.name(ctx.db) {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
let detail_types = variant.fields(ctx.db).into_iter().map(|field| field.ty(ctx.db));
|
2019-03-14 22:03:39 +01:00
|
|
|
let detail = join(detail_types.map(|t| t.display(ctx.db).to_string()))
|
|
|
|
.separator(", ")
|
|
|
|
.surround_with("(", ")")
|
|
|
|
.to_string();
|
2019-02-24 20:56:19 +03:00
|
|
|
|
|
|
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string())
|
|
|
|
.kind(CompletionItemKind::EnumVariant)
|
|
|
|
.set_documentation(variant.docs(ctx.db))
|
|
|
|
.detail(detail)
|
|
|
|
.add_to(self);
|
|
|
|
}
|
2019-02-24 19:34:27 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 19:37:22 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-07-28 11:35:48 +01:00
|
|
|
use crate::completion::{do_completion, CompletionItem, CompletionKind};
|
2019-08-29 16:49:10 +03:00
|
|
|
use insta::assert_debug_snapshot;
|
2019-07-28 12:08:06 +01:00
|
|
|
use test_utils::covers;
|
2019-02-24 19:37:22 +03:00
|
|
|
|
2019-07-28 11:35:48 +01:00
|
|
|
fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
|
|
|
|
do_completion(code, CompletionKind::Reference)
|
2019-02-24 19:37:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inserts_parens_for_function_calls() {
|
|
|
|
covers!(inserts_parens_for_function_calls);
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:33:21 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:35:48 +01:00
|
|
|
fn no_args() {}
|
|
|
|
fn main() { no_<|> }
|
|
|
|
"
|
2019-07-28 12:33:21 +01:00
|
|
|
),
|
|
|
|
@r###"[
|
|
|
|
CompletionItem {
|
|
|
|
label: "main",
|
|
|
|
source_range: [61; 64),
|
|
|
|
delete: [61; 64),
|
|
|
|
insert: "main()$0",
|
|
|
|
kind: Function,
|
|
|
|
detail: "fn main()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "no_args",
|
|
|
|
source_range: [61; 64),
|
|
|
|
delete: [61; 64),
|
|
|
|
insert: "no_args()$0",
|
|
|
|
kind: Function,
|
|
|
|
detail: "fn no_args()",
|
|
|
|
},
|
|
|
|
]"###
|
|
|
|
);
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:33:21 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:35:48 +01:00
|
|
|
fn with_args(x: i32, y: String) {}
|
|
|
|
fn main() { with_<|> }
|
|
|
|
"
|
2019-07-28 12:33:21 +01:00
|
|
|
),
|
|
|
|
@r###"[
|
|
|
|
CompletionItem {
|
|
|
|
label: "main",
|
|
|
|
source_range: [80; 85),
|
|
|
|
delete: [80; 85),
|
|
|
|
insert: "main()$0",
|
|
|
|
kind: Function,
|
|
|
|
detail: "fn main()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "with_args",
|
|
|
|
source_range: [80; 85),
|
|
|
|
delete: [80; 85),
|
|
|
|
insert: "with_args($0)",
|
|
|
|
kind: Function,
|
|
|
|
detail: "fn with_args(x: i32, y: String)",
|
|
|
|
},
|
|
|
|
]"###
|
|
|
|
);
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:33:21 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:35:48 +01:00
|
|
|
struct S {}
|
|
|
|
impl S {
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
fn bar(s: &S) {
|
|
|
|
s.f<|>
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:33:21 +01:00
|
|
|
),
|
|
|
|
@r###"[
|
|
|
|
CompletionItem {
|
|
|
|
label: "foo",
|
|
|
|
source_range: [163; 164),
|
|
|
|
delete: [163; 164),
|
|
|
|
insert: "foo()$0",
|
|
|
|
kind: Method,
|
|
|
|
detail: "fn foo(&self)",
|
|
|
|
},
|
|
|
|
]"###
|
|
|
|
);
|
2019-02-24 19:37:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_render_function_parens_in_use_item() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:33:21 +01:00
|
|
|
do_reference_completion(
|
|
|
|
"
|
2019-07-28 11:35:48 +01:00
|
|
|
//- /lib.rs
|
|
|
|
mod m { pub fn foo() {} }
|
|
|
|
use crate::m::f<|>;
|
|
|
|
"
|
2019-07-28 12:33:21 +01:00
|
|
|
),
|
|
|
|
@r#"[
|
2019-07-28 11:35:48 +01:00
|
|
|
CompletionItem {
|
|
|
|
label: "foo",
|
|
|
|
source_range: [40; 41),
|
|
|
|
delete: [40; 41),
|
|
|
|
insert: "foo",
|
|
|
|
kind: Function,
|
|
|
|
detail: "pub fn foo()",
|
|
|
|
},
|
|
|
|
]"#
|
2019-07-28 12:33:21 +01:00
|
|
|
);
|
2019-02-24 19:37:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_render_function_parens_if_already_call() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:33:21 +01:00
|
|
|
do_reference_completion(
|
|
|
|
"
|
2019-07-28 11:35:48 +01:00
|
|
|
//- /lib.rs
|
|
|
|
fn frobnicate() {}
|
|
|
|
fn main() {
|
|
|
|
frob<|>();
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:33:21 +01:00
|
|
|
),
|
|
|
|
@r#"[
|
2019-07-28 11:35:48 +01:00
|
|
|
CompletionItem {
|
|
|
|
label: "frobnicate",
|
|
|
|
source_range: [35; 39),
|
|
|
|
delete: [35; 39),
|
|
|
|
insert: "frobnicate",
|
|
|
|
kind: Function,
|
|
|
|
detail: "fn frobnicate()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "main",
|
|
|
|
source_range: [35; 39),
|
|
|
|
delete: [35; 39),
|
|
|
|
insert: "main",
|
|
|
|
kind: Function,
|
|
|
|
detail: "fn main()",
|
|
|
|
},
|
|
|
|
]"#
|
2019-07-28 12:33:21 +01:00
|
|
|
);
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:33:21 +01:00
|
|
|
do_reference_completion(
|
|
|
|
"
|
2019-07-28 11:35:48 +01:00
|
|
|
//- /lib.rs
|
|
|
|
struct Foo {}
|
|
|
|
impl Foo { fn new() -> Foo {} }
|
|
|
|
fn main() {
|
|
|
|
Foo::ne<|>();
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:33:21 +01:00
|
|
|
),
|
|
|
|
@r#"[
|
2019-07-28 11:35:48 +01:00
|
|
|
CompletionItem {
|
|
|
|
label: "new",
|
|
|
|
source_range: [67; 69),
|
|
|
|
delete: [67; 69),
|
|
|
|
insert: "new",
|
|
|
|
kind: Function,
|
|
|
|
detail: "fn new() -> Foo",
|
|
|
|
},
|
|
|
|
]"#
|
2019-07-28 12:33:21 +01:00
|
|
|
);
|
2019-02-24 19:37:22 +03:00
|
|
|
}
|
|
|
|
}
|