1 //! Renderer for `enum` variants.
5 use hir::{HasAttrs, HirDisplay};
6 use ide_db::SymbolKind;
7 use itertools::Itertools;
10 item::{CompletionItem, ImportEdit},
11 render::{builder_ext::Params, compute_ref_match, compute_type_match, RenderContext},
15 pub(crate) fn render_variant(
16 ctx: RenderContext<'_>,
17 import_to_add: Option<ImportEdit>,
18 local_name: Option<hir::Name>,
19 variant: hir::Variant,
20 path: Option<hir::ModPath>,
22 let _p = profile::span("render_enum_variant");
23 EnumRender::new(ctx, local_name, variant, path).render(import_to_add)
27 struct EnumRender<'a> {
28 ctx: RenderContext<'a>,
29 variant: hir::Variant,
30 path: Option<hir::ModPath>,
31 qualified_name: hir::ModPath,
32 short_qualified_name: hir::ModPath,
33 variant_kind: hir::StructKind,
36 impl<'a> EnumRender<'a> {
38 ctx: RenderContext<'a>,
39 local_name: Option<hir::Name>,
40 variant: hir::Variant,
41 path: Option<hir::ModPath>,
43 let name = local_name.unwrap_or_else(|| variant.name(ctx.db()));
44 let variant_kind = variant.kind(ctx.db());
46 let (qualified_name, short_qualified_name) = match &path {
48 let short = hir::ModPath::from_segments(
50 path.segments().iter().skip(path.segments().len().saturating_sub(2)).cloned(),
55 hir::ModPath::from_segments(hir::PathKind::Plain, iter::once(name.clone())),
56 hir::ModPath::from_segments(hir::PathKind::Plain, iter::once(name)),
60 EnumRender { ctx, variant, path, qualified_name, short_qualified_name, variant_kind }
62 fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
63 let mut item = CompletionItem::new(
65 self.ctx.source_range(),
66 self.qualified_name.to_string(),
68 item.set_documentation(self.variant.docs(self.ctx.db()))
69 .set_deprecated(self.ctx.is_deprecated(self.variant))
70 .detail(self.detail());
72 if let Some(import_to_add) = import_to_add {
73 item.add_import(import_to_add);
76 if self.variant_kind == hir::StructKind::Tuple {
77 cov_mark::hit!(inserts_parens_for_tuple_enums);
78 let params = Params::Anonymous(self.variant.fields(self.ctx.db()).len());
81 self.short_qualified_name.to_string(),
84 } else if self.path.is_some() {
85 item.lookup_by(self.short_qualified_name.to_string());
88 let ty = self.variant.parent_enum(self.ctx.completion.db).ty(self.ctx.completion.db);
89 item.set_relevance(CompletionRelevance {
90 type_match: compute_type_match(self.ctx.completion, &ty),
91 ..CompletionRelevance::default()
94 if let Some(ref_match) = compute_ref_match(self.ctx.completion, &ty) {
95 item.ref_match(ref_match);
101 fn detail(&self) -> String {
102 let detail_types = self
104 .fields(self.ctx.db())
106 .map(|field| (field.name(self.ctx.db()), field.ty(self.ctx.db())));
108 match self.variant_kind {
109 hir::StructKind::Tuple | hir::StructKind::Unit => format!(
111 detail_types.map(|(_, t)| t.display(self.ctx.db()).to_string()).format(", ")
113 hir::StructKind::Record => format!(
116 .map(|(n, t)| format!("{}: {}", n, t.display(self.ctx.db()).to_string()))
125 use crate::tests::check_edit;
128 fn inserts_parens_for_tuple_enums() {
129 cov_mark::check!(inserts_parens_for_tuple_enums);
133 enum Option<T> { Some(T), None }
135 fn main() -> Option<i32> {
140 enum Option<T> { Some(T), None }
142 fn main() -> Option<i32> {