aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/render/type_alias.rs
blob: e0234171ac19f489732636681f621111db7a44ff (plain)
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
//! Renderer for type aliases.

use hir::HasSource;
use ide_db::SymbolKind;
use syntax::{
    ast::{NameOwner, TypeAlias},
    display::type_label,
};

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

pub(crate) fn render_type_alias<'a>(
    ctx: RenderContext<'a>,
    type_alias: hir::TypeAlias,
) -> Option<CompletionItem> {
    TypeAliasRender::new(ctx, type_alias)?.render(false)
}

pub(crate) fn render_type_alias_with_eq<'a>(
    ctx: RenderContext<'a>,
    type_alias: hir::TypeAlias,
) -> Option<CompletionItem> {
    TypeAliasRender::new(ctx, type_alias)?.render(true)
}

#[derive(Debug)]
struct TypeAliasRender<'a> {
    ctx: RenderContext<'a>,
    type_alias: hir::TypeAlias,
    ast_node: TypeAlias,
}

impl<'a> TypeAliasRender<'a> {
    fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> Option<TypeAliasRender<'a>> {
        let ast_node = type_alias.source(ctx.db())?.value;
        Some(TypeAliasRender { ctx, type_alias, ast_node })
    }

    fn render(self, with_eq: bool) -> Option<CompletionItem> {
        let name = self.ast_node.name().map(|name| {
            if with_eq {
                format!("{} = ", name.text())
            } else {
                name.text().to_string()
            }
        })?;
        let detail = self.detail();

        let mut item =
            CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name);
        item.kind(SymbolKind::TypeAlias)
            .set_documentation(self.ctx.docs(self.type_alias))
            .set_deprecated(
                self.ctx.is_deprecated(self.type_alias)
                    || self.ctx.is_deprecated_assoc_item(self.type_alias),
            )
            .detail(detail);

        Some(item.build())
    }

    fn detail(&self) -> String {
        type_label(&self.ast_node)
    }
}