2020-11-01 04:48:42 -06:00
|
|
|
//! Renderer for `enum` variants.
|
|
|
|
|
2020-11-01 03:35:04 -06:00
|
|
|
use hir::{HasAttrs, HirDisplay, ModPath, StructKind};
|
2021-01-20 11:38:12 -06:00
|
|
|
use ide_db::SymbolKind;
|
2020-11-01 03:35:04 -06:00
|
|
|
use itertools::Itertools;
|
|
|
|
use test_utils::mark;
|
|
|
|
|
|
|
|
use crate::{
|
2021-01-20 11:38:12 -06:00
|
|
|
item::{CompletionItem, CompletionKind, ImportEdit},
|
2020-11-01 03:35:04 -06:00
|
|
|
render::{builder_ext::Params, RenderContext},
|
|
|
|
};
|
|
|
|
|
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>,
|
2020-11-03 01:33:13 -06:00
|
|
|
local_name: Option<String>,
|
2020-12-20 01:05:24 -06:00
|
|
|
variant: hir::Variant,
|
2020-11-03 01:33:13 -06:00
|
|
|
path: Option<ModPath>,
|
|
|
|
) -> 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>,
|
|
|
|
name: String,
|
2020-12-20 01:05:24 -06:00
|
|
|
variant: hir::Variant,
|
2020-11-01 03:35:04 -06:00
|
|
|
path: Option<ModPath>,
|
|
|
|
qualified_name: String,
|
|
|
|
short_qualified_name: String,
|
|
|
|
variant_kind: StructKind,
|
|
|
|
}
|
|
|
|
|
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>,
|
|
|
|
local_name: Option<String>,
|
2020-12-20 01:05:24 -06:00
|
|
|
variant: hir::Variant,
|
2020-11-01 03:35:04 -06:00
|
|
|
path: Option<ModPath>,
|
2020-12-20 01:05:24 -06:00
|
|
|
) -> EnumRender<'a> {
|
2020-11-01 03:35:04 -06:00
|
|
|
let name = local_name.unwrap_or_else(|| variant.name(ctx.db()).to_string());
|
|
|
|
let variant_kind = variant.kind(ctx.db());
|
|
|
|
|
|
|
|
let (qualified_name, short_qualified_name) = match &path {
|
|
|
|
Some(path) => {
|
|
|
|
let full = path.to_string();
|
2021-02-04 13:49:24 -06:00
|
|
|
let segments = path.segments();
|
|
|
|
let short = segments[segments.len().saturating_sub(2)..].iter().join("::");
|
2020-11-01 03:35:04 -06:00
|
|
|
(full, short)
|
|
|
|
}
|
|
|
|
None => (name.to_string(), name.to_string()),
|
|
|
|
};
|
|
|
|
|
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 {
|
2020-11-01 03:35:04 -06:00
|
|
|
let mut builder = CompletionItem::new(
|
|
|
|
CompletionKind::Reference,
|
|
|
|
self.ctx.source_range(),
|
|
|
|
self.qualified_name.clone(),
|
|
|
|
)
|
2021-01-20 11:38:12 -06:00
|
|
|
.kind(SymbolKind::Variant)
|
2020-11-01 03:35:04 -06:00
|
|
|
.set_documentation(self.variant.docs(self.ctx.db()))
|
|
|
|
.set_deprecated(self.ctx.is_deprecated(self.variant))
|
2020-12-07 16:46:56 -06:00
|
|
|
.add_import(import_to_add)
|
2020-11-01 03:35:04 -06:00
|
|
|
.detail(self.detail());
|
|
|
|
|
|
|
|
if self.variant_kind == StructKind::Tuple {
|
|
|
|
mark::hit!(inserts_parens_for_tuple_enums);
|
|
|
|
let params = Params::Anonymous(self.variant.fields(self.ctx.db()).len());
|
|
|
|
builder =
|
|
|
|
builder.add_call_parens(self.ctx.completion, self.short_qualified_name, params);
|
|
|
|
} else if self.path.is_some() {
|
|
|
|
builder = builder.lookup_by(self.short_qualified_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn detail(&self) -> String {
|
|
|
|
let detail_types = self
|
|
|
|
.variant
|
|
|
|
.fields(self.ctx.db())
|
|
|
|
.into_iter()
|
|
|
|
.map(|field| (field.name(self.ctx.db()), field.signature_ty(self.ctx.db())));
|
|
|
|
|
|
|
|
match self.variant_kind {
|
|
|
|
StructKind::Tuple | StructKind::Unit => format!(
|
|
|
|
"({})",
|
|
|
|
detail_types.map(|(_, t)| t.display(self.ctx.db()).to_string()).format(", ")
|
|
|
|
),
|
|
|
|
StructKind::Record => format!(
|
|
|
|
"{{ {} }}",
|
|
|
|
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 test_utils::mark;
|
|
|
|
|
|
|
|
use crate::test_utils::check_edit;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inserts_parens_for_tuple_enums() {
|
|
|
|
mark::check!(inserts_parens_for_tuple_enums);
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|