ide_completion/render/
variant.rsuse crate::context::CompletionContext;
use hir::{db::HirDatabase, sym, HasAttrs, HasCrate, HasVisibility, HirDisplay, StructKind};
use ide_db::SnippetCap;
use itertools::Itertools;
use syntax::{Edition, SmolStr};
pub(crate) struct RenderedLiteral {
pub(crate) literal: String,
pub(crate) detail: String,
}
pub(crate) fn render_record_lit(
db: &dyn HirDatabase,
snippet_cap: Option<SnippetCap>,
fields: &[hir::Field],
path: &str,
edition: Edition,
) -> RenderedLiteral {
if snippet_cap.is_none() {
return RenderedLiteral { literal: path.to_owned(), detail: path.to_owned() };
}
let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| {
if snippet_cap.is_some() {
f(&format_args!(
"{}: ${{{}:()}}",
field.name(db).display(db.upcast(), edition),
idx + 1
))
} else {
f(&format_args!("{}: ()", field.name(db).display(db.upcast(), edition)))
}
});
let types = fields.iter().format_with(", ", |field, f| {
f(&format_args!(
"{}: {}",
field.name(db).display(db.upcast(), edition),
field.ty(db).display(db, edition)
))
});
RenderedLiteral {
literal: format!("{path} {{ {completions} }}"),
detail: format!("{path} {{ {types} }}"),
}
}
pub(crate) fn render_tuple_lit(
db: &dyn HirDatabase,
snippet_cap: Option<SnippetCap>,
fields: &[hir::Field],
path: &str,
edition: Edition,
) -> RenderedLiteral {
if snippet_cap.is_none() {
return RenderedLiteral { literal: path.to_owned(), detail: path.to_owned() };
}
let completions = fields.iter().enumerate().format_with(", ", |(idx, _), f| {
if snippet_cap.is_some() {
f(&format_args!("${{{}:()}}", idx + 1))
} else {
f(&format_args!("()"))
}
});
let types = fields.iter().format_with(", ", |field, f| f(&field.ty(db).display(db, edition)));
RenderedLiteral {
literal: format!("{path}({completions})"),
detail: format!("{path}({types})"),
}
}
pub(crate) fn visible_fields(
ctx: &CompletionContext<'_>,
fields: &[hir::Field],
item: impl HasAttrs + HasCrate + Copy,
) -> Option<(Vec<hir::Field>, bool)> {
let module = ctx.module;
let n_fields = fields.len();
let fields = fields
.iter()
.filter(|field| field.is_visible_from(ctx.db, module))
.copied()
.collect::<Vec<_>>();
let has_invisible_field = n_fields - fields.len() > 0;
let is_foreign_non_exhaustive = item.attrs(ctx.db).by_key(&sym::non_exhaustive).exists()
&& item.krate(ctx.db) != module.krate();
let fields_omitted = has_invisible_field || is_foreign_non_exhaustive;
Some((fields, fields_omitted))
}
pub(crate) fn format_literal_label(
name: &str,
kind: StructKind,
snippet_cap: Option<SnippetCap>,
) -> SmolStr {
if snippet_cap.is_none() {
return name.into();
}
match kind {
StructKind::Tuple => SmolStr::from_iter([name, "(…)"]),
StructKind::Record => SmolStr::from_iter([name, " {…}"]),
StructKind::Unit => name.into(),
}
}
pub(crate) fn format_literal_lookup(name: &str, kind: StructKind) -> SmolStr {
match kind {
StructKind::Tuple => SmolStr::from_iter([name, "()"]),
StructKind::Record => SmolStr::from_iter([name, "{}"]),
StructKind::Unit => name.into(),
}
}