ide_completion/render/
union_literal.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Renderer for `union` literals.

use hir::{HirDisplay, Name, StructKind};
use ide_db::SymbolKind;
use itertools::Itertools;
use syntax::ToSmolStr;

use crate::{
    render::{
        variant::{format_literal_label, format_literal_lookup, visible_fields},
        RenderContext,
    },
    CompletionItem, CompletionItemKind,
};

pub(crate) fn render_union_literal(
    ctx: RenderContext<'_>,
    un: hir::Union,
    path: Option<hir::ModPath>,
    local_name: Option<Name>,
) -> Option<CompletionItem> {
    let name = local_name.unwrap_or_else(|| un.name(ctx.db()));

    let (qualified_name, escaped_qualified_name) = match path {
        Some(p) => (
            p.unescaped().display(ctx.db()).to_string(),
            p.display(ctx.db(), ctx.completion.edition).to_string(),
        ),
        None => (
            name.unescaped().display(ctx.db()).to_string(),
            name.display(ctx.db(), ctx.completion.edition).to_string(),
        ),
    };
    let label = format_literal_label(
        &name.display_no_db(ctx.completion.edition).to_smolstr(),
        StructKind::Record,
        ctx.snippet_cap(),
    );
    let lookup = format_literal_lookup(
        &name.display_no_db(ctx.completion.edition).to_smolstr(),
        StructKind::Record,
    );
    let mut item = CompletionItem::new(
        CompletionItemKind::SymbolKind(SymbolKind::Union),
        ctx.source_range(),
        label,
        ctx.completion.edition,
    );

    item.lookup_by(lookup);

    let fields = un.fields(ctx.db());
    let (fields, fields_omitted) = visible_fields(ctx.completion, &fields, un)?;

    if fields.is_empty() {
        return None;
    }

    let literal = if ctx.snippet_cap().is_some() {
        format!(
            "{} {{ ${{1|{}|}}: ${{2:()}} }}$0",
            escaped_qualified_name,
            fields
                .iter()
                .map(|field| field
                    .name(ctx.db())
                    .display_no_db(ctx.completion.edition)
                    .to_smolstr())
                .format(",")
        )
    } else {
        format!(
            "{} {{ {} }}",
            escaped_qualified_name,
            fields.iter().format_with(", ", |field, f| {
                f(&format_args!(
                    "{}: ()",
                    field.name(ctx.db()).display(ctx.db(), ctx.completion.edition)
                ))
            })
        )
    };

    let detail = format!(
        "{} {{ {}{} }}",
        qualified_name,
        fields.iter().format_with(", ", |field, f| {
            f(&format_args!(
                "{}: {}",
                field.name(ctx.db()).display(ctx.db(), ctx.completion.edition),
                field.ty(ctx.db()).display(ctx.db(), ctx.completion.edition)
            ))
        }),
        if fields_omitted { ", .." } else { "" }
    );

    item.set_documentation(ctx.docs(un))
        .set_deprecated(ctx.is_deprecated(un))
        .detail(detail)
        .set_relevance(ctx.completion_relevance());

    match ctx.snippet_cap() {
        Some(snippet_cap) => item.insert_snippet(snippet_cap, literal).trigger_call_info(),
        None => item.insert_text(literal),
    };

    Some(item.build(ctx.db()))
}