1 //! This modules takes care of rendering various defenitions as completion items.
2 use test_utils::tested_by;
5 use crate::completion::{
6 Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem,
11 pub(crate) fn add_field(
13 ctx: &CompletionContext,
14 field: hir::StructField,
18 CompletionKind::Reference,
20 field.name(ctx.db).to_string(),
22 .kind(CompletionItemKind::Field)
23 .detail(field.ty(ctx.db).subst(substs).to_string())
24 .set_documentation(field.docs(ctx.db))
28 pub(crate) fn add_pos_field(&mut self, ctx: &CompletionContext, field: usize, ty: &hir::Ty) {
29 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), field.to_string())
30 .kind(CompletionItemKind::Field)
31 .detail(ty.to_string())
35 pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) {
36 let sig = func.signature(ctx.db);
38 let mut builder = CompletionItem::new(
39 CompletionKind::Reference,
41 sig.name().to_string(),
43 .kind(if sig.has_self_param() {
44 CompletionItemKind::Method
46 CompletionItemKind::Function
48 .set_documentation(func.docs(ctx.db))
49 .set_detail(function_item_label(ctx, func));
50 // If not an import, add parenthesis automatically.
51 if ctx.use_item_syntax.is_none() && !ctx.is_call {
52 tested_by!(inserts_parens_for_function_calls);
54 if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 {
55 format!("{}()$0", sig.name())
57 format!("{}($0)", sig.name())
59 builder = builder.insert_snippet(snippet);
65 fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> {
66 let node = function.source(ctx.db).1;
72 use test_utils::covers;
74 use crate::completion::{CompletionKind, completion_item::check_completion};
76 fn check_reference_completion(code: &str, expected_completions: &str) {
77 check_completion(code, expected_completions, CompletionKind::Reference);
81 fn inserts_parens_for_function_calls() {
82 covers!(inserts_parens_for_function_calls);
83 check_reference_completion(
84 "inserts_parens_for_function_calls1",
90 check_reference_completion(
91 "inserts_parens_for_function_calls2",
93 fn with_args(x: i32, y: String) {}
94 fn main() { with_<|> }
97 check_reference_completion(
98 "inserts_parens_for_function_calls3",
112 fn dont_render_function_parens_in_use_item() {
113 check_reference_completion(
114 "dont_render_function_parens_in_use_item",
117 mod m { pub fn foo() {} }
124 fn dont_render_function_parens_if_already_call() {
125 check_reference_completion(
126 "dont_render_function_parens_if_already_call",
135 check_reference_completion(
136 "dont_render_function_parens_if_already_call_assoc_fn",
140 impl Foo { fn new() -> Foo {} }