ide_completion/render/
type_alias.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
//! Renderer for type aliases.

use hir::{AsAssocItem, HirDisplay};
use ide_db::SymbolKind;
use syntax::{SmolStr, ToSmolStr};

use crate::{item::CompletionItem, render::RenderContext};

pub(crate) fn render_type_alias(
    ctx: RenderContext<'_>,
    type_alias: hir::TypeAlias,
) -> Option<CompletionItem> {
    let _p = tracing::info_span!("render_type_alias").entered();
    render(ctx, type_alias, false)
}

pub(crate) fn render_type_alias_with_eq(
    ctx: RenderContext<'_>,
    type_alias: hir::TypeAlias,
) -> Option<CompletionItem> {
    let _p = tracing::info_span!("render_type_alias_with_eq").entered();
    render(ctx, type_alias, true)
}

fn render(
    ctx: RenderContext<'_>,
    type_alias: hir::TypeAlias,
    with_eq: bool,
) -> Option<CompletionItem> {
    let db = ctx.db();

    let name = type_alias.name(db);
    let (name, escaped_name) = if with_eq {
        (
            SmolStr::from_iter([&name.unescaped().display(db).to_smolstr(), " = "]),
            SmolStr::from_iter([&name.display_no_db(ctx.completion.edition).to_smolstr(), " = "]),
        )
    } else {
        (
            name.unescaped().display(db).to_smolstr(),
            name.display_no_db(ctx.completion.edition).to_smolstr(),
        )
    };
    let detail = type_alias.display(db, ctx.completion.edition).to_string();

    let mut item = CompletionItem::new(
        SymbolKind::TypeAlias,
        ctx.source_range(),
        name,
        ctx.completion.edition,
    );
    item.set_documentation(ctx.docs(type_alias))
        .set_deprecated(ctx.is_deprecated(type_alias) || ctx.is_deprecated_assoc_item(type_alias))
        .detail(detail)
        .set_relevance(ctx.completion_relevance());

    if let Some(actm) = type_alias.as_assoc_item(db) {
        if let Some(trt) = actm.container_or_implemented_trait(db) {
            item.trait_name(trt.name(db).display_no_db(ctx.completion.edition).to_smolstr());
        }
    }
    item.insert_text(escaped_name);

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