2020-11-01 04:48:42 -06:00
|
|
|
//! Renderer for `enum` variants.
|
|
|
|
|
2021-05-31 07:13:09 -05:00
|
|
|
use std::iter;
|
|
|
|
|
|
|
|
use hir::{HasAttrs, HirDisplay};
|
2021-01-20 11:38:12 -06:00
|
|
|
use ide_db::SymbolKind;
|
2020-11-01 03:35:04 -06:00
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
|
|
use crate::{
|
2021-01-20 11:38:12 -06:00
|
|
|
item::{CompletionItem, CompletionKind, ImportEdit},
|
2021-03-22 23:18:34 -05:00
|
|
|
render::{builder_ext::Params, compute_ref_match, compute_type_match, RenderContext},
|
2021-03-15 21:26:59 -05:00
|
|
|
CompletionRelevance,
|
2020-11-01 03:35:04 -06:00
|
|
|
};
|
|
|
|
|
2020-12-20 01:05:24 -06:00
|
|
|
pub(crate) fn render_variant<'a>(
|
2020-11-03 01:33:13 -06:00
|
|
|
ctx: RenderContext<'a>,
|
2020-12-03 03:13:28 -06:00
|
|
|
import_to_add: Option<ImportEdit>,
|
2021-05-31 07:13:09 -05:00
|
|
|
local_name: Option<hir::Name>,
|
2020-12-20 01:05:24 -06:00
|
|
|
variant: hir::Variant,
|
2021-05-31 07:13:09 -05:00
|
|
|
path: Option<hir::ModPath>,
|
2020-11-03 01:33:13 -06:00
|
|
|
) -> CompletionItem {
|
2020-11-27 10:00:03 -06:00
|
|
|
let _p = profile::span("render_enum_variant");
|
2020-12-20 01:05:24 -06:00
|
|
|
EnumRender::new(ctx, local_name, variant, path).render(import_to_add)
|
2020-11-03 01:33:13 -06:00
|
|
|
}
|
|
|
|
|
2020-11-01 03:35:04 -06:00
|
|
|
#[derive(Debug)]
|
2020-12-20 01:05:24 -06:00
|
|
|
struct EnumRender<'a> {
|
2020-11-01 03:35:04 -06:00
|
|
|
ctx: RenderContext<'a>,
|
2021-05-31 07:13:09 -05:00
|
|
|
name: hir::Name,
|
2020-12-20 01:05:24 -06:00
|
|
|
variant: hir::Variant,
|
2021-05-31 07:13:09 -05:00
|
|
|
path: Option<hir::ModPath>,
|
|
|
|
qualified_name: hir::ModPath,
|
|
|
|
short_qualified_name: hir::ModPath,
|
|
|
|
variant_kind: hir::StructKind,
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
2020-12-20 01:05:24 -06:00
|
|
|
impl<'a> EnumRender<'a> {
|
2020-11-03 01:33:13 -06:00
|
|
|
fn new(
|
2020-11-01 03:35:04 -06:00
|
|
|
ctx: RenderContext<'a>,
|
2021-05-31 07:13:09 -05:00
|
|
|
local_name: Option<hir::Name>,
|
2020-12-20 01:05:24 -06:00
|
|
|
variant: hir::Variant,
|
2021-05-31 07:13:09 -05:00
|
|
|
path: Option<hir::ModPath>,
|
2020-12-20 01:05:24 -06:00
|
|
|
) -> EnumRender<'a> {
|
2021-05-31 07:13:09 -05:00
|
|
|
let name = local_name.unwrap_or_else(|| variant.name(ctx.db()));
|
2020-11-01 03:35:04 -06:00
|
|
|
let variant_kind = variant.kind(ctx.db());
|
|
|
|
|
|
|
|
let (qualified_name, short_qualified_name) = match &path {
|
|
|
|
Some(path) => {
|
2021-05-31 07:13:09 -05:00
|
|
|
let short = hir::ModPath::from_segments(
|
|
|
|
hir::PathKind::Plain,
|
|
|
|
path.segments().iter().skip(path.segments().len().saturating_sub(2)).cloned(),
|
|
|
|
);
|
|
|
|
(path.clone(), short)
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
2021-05-31 07:13:09 -05:00
|
|
|
None => (
|
|
|
|
hir::ModPath::from_segments(hir::PathKind::Plain, iter::once(name.clone())),
|
|
|
|
hir::ModPath::from_segments(hir::PathKind::Plain, iter::once(name.clone())),
|
|
|
|
),
|
2020-11-01 03:35:04 -06:00
|
|
|
};
|
|
|
|
|
2020-12-20 01:05:24 -06:00
|
|
|
EnumRender { ctx, name, variant, path, qualified_name, short_qualified_name, variant_kind }
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
2020-12-03 03:13:28 -06:00
|
|
|
fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
|
2021-03-12 03:12:32 -06:00
|
|
|
let mut item = CompletionItem::new(
|
2020-11-01 03:35:04 -06:00
|
|
|
CompletionKind::Reference,
|
|
|
|
self.ctx.source_range(),
|
2021-05-31 07:13:09 -05:00
|
|
|
self.qualified_name.to_string(),
|
2021-03-11 09:46:41 -06:00
|
|
|
);
|
2021-03-12 03:12:32 -06:00
|
|
|
item.kind(SymbolKind::Variant)
|
2021-03-11 09:46:41 -06:00
|
|
|
.set_documentation(self.variant.docs(self.ctx.db()))
|
|
|
|
.set_deprecated(self.ctx.is_deprecated(self.variant))
|
|
|
|
.add_import(import_to_add)
|
|
|
|
.detail(self.detail());
|
2020-11-01 03:35:04 -06:00
|
|
|
|
2021-05-31 07:13:09 -05:00
|
|
|
if self.variant_kind == hir::StructKind::Tuple {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(inserts_parens_for_tuple_enums);
|
2020-11-01 03:35:04 -06:00
|
|
|
let params = Params::Anonymous(self.variant.fields(self.ctx.db()).len());
|
2021-05-31 07:13:09 -05:00
|
|
|
item.add_call_parens(
|
|
|
|
self.ctx.completion,
|
|
|
|
self.short_qualified_name.to_string(),
|
|
|
|
params,
|
|
|
|
);
|
2020-11-01 03:35:04 -06:00
|
|
|
} else if self.path.is_some() {
|
2021-05-31 07:13:09 -05:00
|
|
|
item.lookup_by(self.short_qualified_name.to_string());
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
2021-03-15 21:26:59 -05:00
|
|
|
let ty = self.variant.parent_enum(self.ctx.completion.db).ty(self.ctx.completion.db);
|
|
|
|
item.set_relevance(CompletionRelevance {
|
2021-03-22 23:18:34 -05:00
|
|
|
type_match: compute_type_match(self.ctx.completion, &ty),
|
2021-03-15 21:26:59 -05:00
|
|
|
..CompletionRelevance::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(ref_match) = compute_ref_match(self.ctx.completion, &ty) {
|
|
|
|
item.ref_match(ref_match);
|
|
|
|
}
|
|
|
|
|
2021-03-12 03:12:32 -06:00
|
|
|
item.build()
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn detail(&self) -> String {
|
|
|
|
let detail_types = self
|
|
|
|
.variant
|
|
|
|
.fields(self.ctx.db())
|
|
|
|
.into_iter()
|
2021-04-30 03:52:31 -05:00
|
|
|
.map(|field| (field.name(self.ctx.db()), field.ty(self.ctx.db())));
|
2020-11-01 03:35:04 -06:00
|
|
|
|
|
|
|
match self.variant_kind {
|
2021-05-31 07:13:09 -05:00
|
|
|
hir::StructKind::Tuple | hir::StructKind::Unit => format!(
|
2020-11-01 03:35:04 -06:00
|
|
|
"({})",
|
|
|
|
detail_types.map(|(_, t)| t.display(self.ctx.db()).to_string()).format(", ")
|
|
|
|
),
|
2021-05-31 07:13:09 -05:00
|
|
|
hir::StructKind::Record => format!(
|
2020-11-01 03:35:04 -06:00
|
|
|
"{{ {} }}",
|
|
|
|
detail_types
|
|
|
|
.map(|(n, t)| format!("{}: {}", n, t.display(self.ctx.db()).to_string()))
|
|
|
|
.format(", ")
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::test_utils::check_edit;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inserts_parens_for_tuple_enums() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(inserts_parens_for_tuple_enums);
|
2020-11-01 04:36:30 -06:00
|
|
|
check_edit(
|
|
|
|
"Some",
|
|
|
|
r#"
|
|
|
|
enum Option<T> { Some(T), None }
|
|
|
|
use Option::*;
|
|
|
|
fn main() -> Option<i32> {
|
2021-01-06 14:15:48 -06:00
|
|
|
Som$0
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
enum Option<T> { Some(T), None }
|
|
|
|
use Option::*;
|
|
|
|
fn main() -> Option<i32> {
|
|
|
|
Some($0)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|