2022-03-11 17:17:01 -08:00
|
|
|
//! Code common to structs, unions, and enum variants.
|
|
|
|
|
|
|
|
use crate::render::RenderContext;
|
2022-03-12 04:40:05 -08:00
|
|
|
use hir::{db::HirDatabase, HasAttrs, HasVisibility, HirDisplay, StructKind};
|
2022-03-11 17:17:01 -08:00
|
|
|
use ide_db::SnippetCap;
|
|
|
|
use itertools::Itertools;
|
2022-03-12 04:40:05 -08:00
|
|
|
use syntax::SmolStr;
|
2022-03-11 17:17:01 -08:00
|
|
|
|
|
|
|
/// A rendered struct, union, or enum variant, split into fields for actual
|
|
|
|
/// auto-completion (`literal`, using `field: ()`) and display in the
|
|
|
|
/// completions menu (`detail`, using `field: type`).
|
|
|
|
pub(crate) struct RenderedCompound {
|
2022-03-11 19:10:43 -08:00
|
|
|
pub(crate) literal: String,
|
|
|
|
pub(crate) detail: String,
|
2022-03-11 17:17:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Render a record type (or sub-type) to a `RenderedCompound`. Use `None` for
|
|
|
|
/// the `name` argument for an anonymous type.
|
|
|
|
pub(crate) fn render_record(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
snippet_cap: Option<SnippetCap>,
|
|
|
|
fields: &[hir::Field],
|
|
|
|
name: Option<&str>,
|
|
|
|
) -> RenderedCompound {
|
|
|
|
let fields = fields.iter();
|
|
|
|
|
|
|
|
let (completions, types): (Vec<_>, Vec<_>) = fields
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, field)| {
|
|
|
|
(
|
|
|
|
if snippet_cap.is_some() {
|
|
|
|
format!("{}: ${{{}:()}}", field.name(db), idx + 1)
|
|
|
|
} else {
|
|
|
|
format!("{}: ()", field.name(db))
|
|
|
|
},
|
|
|
|
format!("{}: {}", field.name(db), field.ty(db).display(db)),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unzip();
|
|
|
|
RenderedCompound {
|
|
|
|
literal: format!("{} {{ {} }}", name.unwrap_or(""), completions.iter().format(", ")),
|
|
|
|
detail: format!("{} {{ {} }}", name.unwrap_or(""), types.iter().format(", ")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Render a tuple type (or sub-type) to a `RenderedCompound`. Use `None` for
|
|
|
|
/// the `name` argument for an anonymous type.
|
|
|
|
pub(crate) fn render_tuple(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
snippet_cap: Option<SnippetCap>,
|
|
|
|
fields: &[hir::Field],
|
|
|
|
name: Option<&str>,
|
|
|
|
) -> RenderedCompound {
|
|
|
|
let fields = fields.iter();
|
|
|
|
|
|
|
|
let (completions, types): (Vec<_>, Vec<_>) = fields
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, field)| {
|
|
|
|
(
|
|
|
|
if snippet_cap.is_some() {
|
|
|
|
format!("${{{}:()}}", (idx + 1).to_string())
|
|
|
|
} else {
|
|
|
|
"()".to_string()
|
|
|
|
},
|
|
|
|
field.ty(db).display(db).to_string(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unzip();
|
|
|
|
RenderedCompound {
|
|
|
|
literal: format!("{}({})", name.unwrap_or(""), completions.iter().format(", ")),
|
|
|
|
detail: format!("{}({})", name.unwrap_or(""), types.iter().format(", ")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 19:23:04 -08:00
|
|
|
/// Find all the visible fields in a given list. Returns the list of visible
|
2022-03-11 17:17:01 -08:00
|
|
|
/// fields, plus a boolean for whether the list is comprehensive (contains no
|
2022-03-11 19:23:04 -08:00
|
|
|
/// private fields and its item is not marked `#[non_exhaustive]`).
|
2022-03-11 17:17:01 -08:00
|
|
|
pub(crate) fn visible_fields(
|
|
|
|
ctx: &RenderContext<'_>,
|
|
|
|
fields: &[hir::Field],
|
|
|
|
item: impl HasAttrs,
|
|
|
|
) -> Option<(Vec<hir::Field>, bool)> {
|
|
|
|
let module = ctx.completion.module?;
|
|
|
|
let n_fields = fields.len();
|
|
|
|
let fields = fields
|
|
|
|
.iter()
|
|
|
|
.filter(|field| field.is_visible_from(ctx.db(), module))
|
|
|
|
.copied()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let fields_omitted =
|
|
|
|
n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
|
|
|
|
Some((fields, fields_omitted))
|
|
|
|
}
|
2022-03-12 04:40:05 -08:00
|
|
|
|
|
|
|
/// Format a struct, etc. literal option for display in the completions menu.
|
|
|
|
pub(crate) fn format_literal_label(name: &str, kind: StructKind) -> SmolStr {
|
|
|
|
match kind {
|
|
|
|
StructKind::Tuple => SmolStr::from_iter([name, "(…)"]),
|
|
|
|
StructKind::Record => SmolStr::from_iter([name, " {…}"]),
|
|
|
|
StructKind::Unit => name.into(),
|
|
|
|
}
|
|
|
|
}
|