]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/render/compound.rs
- Break out functionality related to rendering struct completions into `crates/ide_co...
[rust.git] / crates / ide_completion / src / render / compound.rs
1 //! Code common to structs, unions, and enum variants.
2
3 use crate::render::RenderContext;
4 use hir::{db::HirDatabase, HasAttrs, HasVisibility, HirDisplay};
5 use ide_db::SnippetCap;
6 use itertools::Itertools;
7
8 /// A rendered struct, union, or enum variant, split into fields for actual
9 /// auto-completion (`literal`, using `field: ()`) and display in the
10 /// completions menu (`detail`, using `field: type`).
11 pub(crate) struct RenderedCompound {
12     pub literal: String,
13     pub detail: String,
14 }
15
16 /// Render a record type (or sub-type) to a `RenderedCompound`. Use `None` for
17 /// the `name` argument for an anonymous type.
18 pub(crate) fn render_record(
19     db: &dyn HirDatabase,
20     snippet_cap: Option<SnippetCap>,
21     fields: &[hir::Field],
22     name: Option<&str>,
23 ) -> RenderedCompound {
24     let fields = fields.iter();
25
26     let (completions, types): (Vec<_>, Vec<_>) = fields
27         .enumerate()
28         .map(|(idx, field)| {
29             (
30                 if snippet_cap.is_some() {
31                     format!("{}: ${{{}:()}}", field.name(db), idx + 1)
32                 } else {
33                     format!("{}: ()", field.name(db))
34                 },
35                 format!("{}: {}", field.name(db), field.ty(db).display(db)),
36             )
37         })
38         .unzip();
39     RenderedCompound {
40         literal: format!("{} {{ {} }}", name.unwrap_or(""), completions.iter().format(", ")),
41         detail: format!("{} {{ {} }}", name.unwrap_or(""), types.iter().format(", ")),
42     }
43 }
44
45 /// Render a tuple type (or sub-type) to a `RenderedCompound`. Use `None` for
46 /// the `name` argument for an anonymous type.
47 pub(crate) fn render_tuple(
48     db: &dyn HirDatabase,
49     snippet_cap: Option<SnippetCap>,
50     fields: &[hir::Field],
51     name: Option<&str>,
52 ) -> RenderedCompound {
53     let fields = fields.iter();
54
55     let (completions, types): (Vec<_>, Vec<_>) = fields
56         .enumerate()
57         .map(|(idx, field)| {
58             (
59                 if snippet_cap.is_some() {
60                     format!("${{{}:()}}", (idx + 1).to_string())
61                 } else {
62                     "()".to_string()
63                 },
64                 field.ty(db).display(db).to_string(),
65             )
66         })
67         .unzip();
68     RenderedCompound {
69         literal: format!("{}({})", name.unwrap_or(""), completions.iter().format(", ")),
70         detail: format!("{}({})", name.unwrap_or(""), types.iter().format(", ")),
71     }
72 }
73
74 /// Find all the visible fields in a `HasAttrs`. Returns the list of visible
75 /// fields, plus a boolean for whether the list is comprehensive (contains no
76 /// private fields and is not marked `#[non_exhaustive]`).
77 pub(crate) fn visible_fields(
78     ctx: &RenderContext<'_>,
79     fields: &[hir::Field],
80     item: impl HasAttrs,
81 ) -> Option<(Vec<hir::Field>, bool)> {
82     let module = ctx.completion.module?;
83     let n_fields = fields.len();
84     let fields = fields
85         .iter()
86         .filter(|field| field.is_visible_from(ctx.db(), module))
87         .copied()
88         .collect::<Vec<_>>();
89
90     let fields_omitted =
91         n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
92     Some((fields, fields_omitted))
93 }