1 //! Renderer for `enum` variants.
3 use hir::{HasAttrs, HirDisplay, ModPath, StructKind};
4 use ide_db::SymbolKind;
5 use itertools::Itertools;
9 item::{CompletionItem, CompletionKind, ImportEdit},
10 render::{builder_ext::Params, RenderContext},
13 pub(crate) fn render_variant<'a>(
14 ctx: RenderContext<'a>,
15 import_to_add: Option<ImportEdit>,
16 local_name: Option<String>,
17 variant: hir::Variant,
18 path: Option<ModPath>,
20 let _p = profile::span("render_enum_variant");
21 EnumRender::new(ctx, local_name, variant, path).render(import_to_add)
25 struct EnumRender<'a> {
26 ctx: RenderContext<'a>,
28 variant: hir::Variant,
29 path: Option<ModPath>,
30 qualified_name: String,
31 short_qualified_name: String,
32 variant_kind: StructKind,
35 impl<'a> EnumRender<'a> {
37 ctx: RenderContext<'a>,
38 local_name: Option<String>,
39 variant: hir::Variant,
40 path: Option<ModPath>,
42 let name = local_name.unwrap_or_else(|| variant.name(ctx.db()).to_string());
43 let variant_kind = variant.kind(ctx.db());
45 let (qualified_name, short_qualified_name) = match &path {
47 let full = path.to_string();
48 let segments = path.segments();
49 let short = segments[segments.len().saturating_sub(2)..].iter().join("::");
52 None => (name.to_string(), name.to_string()),
55 EnumRender { ctx, name, variant, path, qualified_name, short_qualified_name, variant_kind }
58 fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
59 let mut builder = CompletionItem::new(
60 CompletionKind::Reference,
61 self.ctx.source_range(),
62 self.qualified_name.clone(),
64 .kind(SymbolKind::Variant)
65 .set_documentation(self.variant.docs(self.ctx.db()))
66 .set_deprecated(self.ctx.is_deprecated(self.variant))
67 .add_import(import_to_add)
68 .detail(self.detail());
70 if self.variant_kind == StructKind::Tuple {
71 mark::hit!(inserts_parens_for_tuple_enums);
72 let params = Params::Anonymous(self.variant.fields(self.ctx.db()).len());
74 builder.add_call_parens(self.ctx.completion, self.short_qualified_name, params);
75 } else if self.path.is_some() {
76 builder = builder.lookup_by(self.short_qualified_name);
82 fn detail(&self) -> String {
83 let detail_types = self
85 .fields(self.ctx.db())
87 .map(|field| (field.name(self.ctx.db()), field.signature_ty(self.ctx.db())));
89 match self.variant_kind {
90 StructKind::Tuple | StructKind::Unit => format!(
92 detail_types.map(|(_, t)| t.display(self.ctx.db()).to_string()).format(", ")
94 StructKind::Record => format!(
97 .map(|(n, t)| format!("{}: {}", n, t.display(self.ctx.db()).to_string()))
106 use test_utils::mark;
108 use crate::test_utils::check_edit;
111 fn inserts_parens_for_tuple_enums() {
112 mark::check!(inserts_parens_for_tuple_enums);
116 enum Option<T> { Some(T), None }
118 fn main() -> Option<i32> {
123 enum Option<T> { Some(T), None }
125 fn main() -> Option<i32> {