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

132 lines
3.8 KiB
Rust
Raw Normal View History

2020-11-01 13:48:42 +03:00
//! Renderer for `enum` variants.
use std::iter;
use hir::{db::HirDatabase, HasAttrs, HirDisplay, StructKind};
use ide_db::SymbolKind;
use itertools::Itertools;
2020-11-01 12:35:04 +03:00
use crate::{
item::{CompletionItem, ImportEdit},
render::{builder_ext::Params, compute_ref_match, compute_type_match, RenderContext},
CompletionRelevance,
2020-11-01 12:35:04 +03:00
};
pub(crate) fn render_variant(
ctx: RenderContext<'_>,
2020-12-03 11:13:28 +02:00
import_to_add: Option<ImportEdit>,
local_name: Option<hir::Name>,
2020-12-20 10:05:24 +03:00
variant: hir::Variant,
path: Option<hir::ModPath>,
2020-11-03 10:33:13 +03:00
) -> CompletionItem {
2020-11-27 18:00:03 +02:00
let _p = profile::span("render_enum_variant");
render(ctx, local_name, variant, path, import_to_add)
2020-11-03 10:33:13 +03:00
}
fn render(
ctx @ RenderContext { completion }: RenderContext<'_>,
local_name: Option<hir::Name>,
2020-12-20 10:05:24 +03:00
variant: hir::Variant,
path: Option<hir::ModPath>,
import_to_add: Option<ImportEdit>,
) -> CompletionItem {
let db = completion.db;
let name = local_name.unwrap_or_else(|| variant.name(db));
let variant_kind = variant.kind(db);
let (qualified_name, short_qualified_name, qualified) = 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, short, true)
2020-11-01 12:35:04 +03:00
}
None => (
hir::ModPath::from_segments(hir::PathKind::Plain, iter::once(name.clone())),
hir::ModPath::from_segments(hir::PathKind::Plain, iter::once(name)),
false,
),
};
// FIXME: ModPath::to_smol_str()?
let mut item =
CompletionItem::new(SymbolKind::Variant, ctx.source_range(), qualified_name.to_string());
item.set_documentation(variant.docs(db))
.set_deprecated(ctx.is_deprecated(variant))
.detail(detail(db, variant, variant_kind));
if let Some(import_to_add) = import_to_add {
item.add_import(import_to_add);
}
2020-11-01 12:35:04 +03:00
// FIXME: ModPath::to_smol_str()?
let short_qualified_name = short_qualified_name.to_string();
if variant_kind == hir::StructKind::Tuple {
cov_mark::hit!(inserts_parens_for_tuple_enums);
let params = Params::Anonymous(variant.fields(db).len());
item.add_call_parens(ctx.completion, short_qualified_name, params);
} else if qualified {
item.lookup_by(short_qualified_name);
}
let ty = variant.parent_enum(ctx.completion.db).ty(ctx.completion.db);
item.set_relevance(CompletionRelevance {
type_match: compute_type_match(ctx.completion, &ty),
..CompletionRelevance::default()
});
if let Some(ref_match) = compute_ref_match(ctx.completion, &ty) {
item.ref_match(ref_match);
2020-11-01 12:35:04 +03:00
}
item.build()
}
fn detail(db: &dyn HirDatabase, variant: hir::Variant, variant_kind: StructKind) -> String {
let detail_types = variant.fields(db).into_iter().map(|field| (field.name(db), field.ty(db)));
match variant_kind {
hir::StructKind::Tuple | hir::StructKind::Unit => {
format!("({})", detail_types.format_with(", ", |(_, t), f| f(&t.display(db))))
}
hir::StructKind::Record => {
format!(
"{{{}}}",
detail_types.format_with(", ", |(n, t), f| {
f(&n)?;
f(&": ")?;
f(&t.display(db))
}),
)
2020-11-01 12:35:04 +03:00
}
}
}
#[cfg(test)]
mod tests {
2021-06-16 21:45:02 +02:00
use crate::tests::check_edit;
#[test]
fn inserts_parens_for_tuple_enums() {
2021-03-08 22:19:44 +02: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 20:15:48 +00:00
Som$0
}
"#,
r#"
enum Option<T> { Some(T), None }
use Option::*;
fn main() -> Option<i32> {
Some($0)
}
"#,
);
}
}