rust/crates/ide_completion/src/render/enum_variant.rs

148 lines
4.4 KiB
Rust
Raw Normal View History

2020-11-01 04:48:42 -06:00
//! Renderer for `enum` variants.
use std::iter;
use hir::{HasAttrs, HirDisplay};
use ide_db::SymbolKind;
2020-11-01 03:35:04 -06:00
use itertools::Itertools;
use crate::{
item::{CompletionItem, CompletionKind, ImportEdit},
render::{builder_ext::Params, compute_ref_match, compute_type_match, RenderContext},
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>,
local_name: Option<hir::Name>,
2020-12-20 01:05:24 -06:00
variant: hir::Variant,
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>,
name: hir::Name,
2020-12-20 01:05:24 -06:00
variant: hir::Variant,
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>,
local_name: Option<hir::Name>,
2020-12-20 01:05:24 -06:00
variant: hir::Variant,
path: Option<hir::ModPath>,
2020-12-20 01:05:24 -06:00
) -> EnumRender<'a> {
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) => {
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
}
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(),
self.qualified_name.to_string(),
);
2021-03-12 03:12:32 -06:00
item.kind(SymbolKind::Variant)
.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
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());
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() {
item.lookup_by(self.short_qualified_name.to_string());
2020-11-01 03:35:04 -06:00
}
let ty = self.variant.parent_enum(self.ctx.completion.db).ty(self.ctx.completion.db);
item.set_relevance(CompletionRelevance {
type_match: compute_type_match(self.ctx.completion, &ty),
..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()
.map(|field| (field.name(self.ctx.db()), field.ty(self.ctx.db())));
2020-11-01 03:35:04 -06:00
match self.variant_kind {
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(", ")
),
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(", ")
),
}
}
}
#[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);
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
}
"#,
r#"
enum Option<T> { Some(T), None }
use Option::*;
fn main() -> Option<i32> {
Some($0)
}
"#,
);
}
}