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(
14 ctx: &CompletionContext,
15 field: hir::StructField,
18 CompletionItem::new(kind, ctx.source_range(), field.name(ctx.db).to_string())
19 .kind(CompletionItemKind::Field)
20 .detail(field.ty(ctx.db).subst(substs).to_string())
21 .set_documentation(field.docs(ctx.db))
25 pub(crate) fn add_pos_field(
28 ctx: &CompletionContext,
32 CompletionItem::new(kind, ctx.source_range(), field.to_string())
33 .kind(CompletionItemKind::Field)
34 .detail(ty.to_string())
38 pub(crate) fn add_function(
41 ctx: &CompletionContext,
44 let sig = func.signature(ctx.db);
46 let mut builder = CompletionItem::new(kind, ctx.source_range(), sig.name().to_string())
47 .kind(if sig.has_self_param() {
48 CompletionItemKind::Method
50 CompletionItemKind::Function
52 .set_documentation(func.docs(ctx.db))
53 .set_detail(function_item_label(ctx, func));
54 // If not an import, add parenthesis automatically.
55 if ctx.use_item_syntax.is_none() && !ctx.is_call {
56 tested_by!(inserts_parens_for_function_calls);
58 if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 {
59 format!("{}()$0", sig.name())
61 format!("{}($0)", sig.name())
63 builder = builder.insert_snippet(snippet);
69 fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> {
70 let node = function.source(ctx.db).1;