2020-12-20 17:19:23 +00:00
|
|
|
//! Renderer for patterns.
|
|
|
|
|
2023-09-02 14:26:48 +00:00
|
|
|
use hir::{db::HirDatabase, Name, StructKind};
|
|
|
|
use ide_db::{documentation::HasDocs, SnippetCap};
|
2020-12-20 17:19:23 +00:00
|
|
|
use itertools::Itertools;
|
2024-07-16 10:43:58 +00:00
|
|
|
use syntax::{SmolStr, ToSmolStr};
|
2020-12-20 17:19:23 +00:00
|
|
|
|
2021-08-14 16:18:18 +00:00
|
|
|
use crate::{
|
2022-07-28 13:47:46 +00:00
|
|
|
context::{ParamContext, ParamKind, PathCompletionCtx, PatternContext},
|
2022-06-25 18:33:19 +00:00
|
|
|
render::{
|
2022-08-15 16:24:21 +00:00
|
|
|
variant::{format_literal_label, format_literal_lookup, visible_fields},
|
2022-06-25 18:33:19 +00:00
|
|
|
RenderContext,
|
|
|
|
},
|
2021-08-14 17:06:35 +00:00
|
|
|
CompletionItem, CompletionItemKind,
|
2021-08-14 16:18:18 +00:00
|
|
|
};
|
2020-12-20 17:19:23 +00:00
|
|
|
|
2020-12-20 18:20:55 +00:00
|
|
|
pub(crate) fn render_struct_pat(
|
|
|
|
ctx: RenderContext<'_>,
|
2022-06-20 15:41:04 +00:00
|
|
|
pattern_ctx: &PatternContext,
|
2020-12-20 17:19:23 +00:00
|
|
|
strukt: hir::Struct,
|
|
|
|
local_name: Option<Name>,
|
|
|
|
) -> Option<CompletionItem> {
|
2024-06-06 23:52:25 +00:00
|
|
|
let _p = tracing::info_span!("render_struct_pat").entered();
|
2020-12-20 17:19:23 +00:00
|
|
|
|
|
|
|
let fields = strukt.fields(ctx.db());
|
2022-03-16 15:27:55 +00:00
|
|
|
let (visible_fields, fields_omitted) = visible_fields(ctx.completion, &fields, strukt)?;
|
2020-12-20 17:19:23 +00:00
|
|
|
|
2020-12-22 18:00:38 +00:00
|
|
|
if visible_fields.is_empty() {
|
2020-12-20 17:19:23 +00:00
|
|
|
// Matching a struct without matching its fields is pointless, unlike matching a Variant without its fields
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-06-26 06:45:30 +00:00
|
|
|
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db()));
|
2024-07-16 10:43:58 +00:00
|
|
|
let (name, escaped_name) =
|
|
|
|
(name.unescaped().display(ctx.db()).to_smolstr(), name.display(ctx.db()).to_smolstr());
|
2022-06-25 18:33:19 +00:00
|
|
|
let kind = strukt.kind(ctx.db());
|
2022-12-20 16:27:19 +00:00
|
|
|
let label = format_literal_label(name.as_str(), kind, ctx.snippet_cap());
|
2022-08-15 16:24:21 +00:00
|
|
|
let lookup = format_literal_lookup(name.as_str(), kind);
|
2022-06-25 18:33:19 +00:00
|
|
|
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
|
2020-12-20 17:19:23 +00:00
|
|
|
|
2023-03-14 06:11:25 +00:00
|
|
|
let db = ctx.db();
|
|
|
|
|
2023-03-18 06:13:57 +00:00
|
|
|
Some(build_completion(ctx, label, lookup, pat, strukt, strukt.ty(db), false))
|
2020-12-20 17:19:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 18:20:55 +00:00
|
|
|
pub(crate) fn render_variant_pat(
|
|
|
|
ctx: RenderContext<'_>,
|
2022-06-20 15:41:04 +00:00
|
|
|
pattern_ctx: &PatternContext,
|
2022-07-28 13:47:46 +00:00
|
|
|
path_ctx: Option<&PathCompletionCtx>,
|
2020-12-20 17:19:23 +00:00
|
|
|
variant: hir::Variant,
|
|
|
|
local_name: Option<Name>,
|
2022-03-16 15:27:55 +00:00
|
|
|
path: Option<&hir::ModPath>,
|
2020-12-20 17:19:23 +00:00
|
|
|
) -> Option<CompletionItem> {
|
2024-06-06 23:52:25 +00:00
|
|
|
let _p = tracing::info_span!("render_variant_pat").entered();
|
2020-12-20 17:19:23 +00:00
|
|
|
|
|
|
|
let fields = variant.fields(ctx.db());
|
2022-03-16 15:41:35 +00:00
|
|
|
let (visible_fields, fields_omitted) = visible_fields(ctx.completion, &fields, variant)?;
|
2023-03-14 06:11:25 +00:00
|
|
|
let enum_ty = variant.parent_enum(ctx.db()).ty(ctx.db());
|
2020-12-20 17:19:23 +00:00
|
|
|
|
2022-06-26 06:45:30 +00:00
|
|
|
let (name, escaped_name) = match path {
|
2023-05-24 16:04:29 +00:00
|
|
|
Some(path) => (
|
|
|
|
path.unescaped().display(ctx.db()).to_string().into(),
|
|
|
|
path.display(ctx.db()).to_string().into(),
|
|
|
|
),
|
2022-06-26 06:45:30 +00:00
|
|
|
None => {
|
|
|
|
let name = local_name.unwrap_or_else(|| variant.name(ctx.db()));
|
2024-07-16 10:43:58 +00:00
|
|
|
let it = (
|
|
|
|
name.unescaped().display(ctx.db()).to_smolstr(),
|
|
|
|
name.display(ctx.db()).to_smolstr(),
|
|
|
|
);
|
|
|
|
it
|
2022-06-26 06:45:30 +00:00
|
|
|
}
|
2021-02-09 20:32:05 +00:00
|
|
|
};
|
2022-07-28 13:47:46 +00:00
|
|
|
|
2022-08-15 16:24:21 +00:00
|
|
|
let (label, lookup, pat) = match path_ctx {
|
|
|
|
Some(PathCompletionCtx { has_call_parens: true, .. }) => {
|
|
|
|
(name.clone(), name, escaped_name.to_string())
|
|
|
|
}
|
2022-07-28 13:47:46 +00:00
|
|
|
_ => {
|
|
|
|
let kind = variant.kind(ctx.db());
|
2022-12-20 16:27:19 +00:00
|
|
|
let label = format_literal_label(name.as_str(), kind, ctx.snippet_cap());
|
2022-08-15 16:24:21 +00:00
|
|
|
let lookup = format_literal_lookup(name.as_str(), kind);
|
2022-07-28 13:47:46 +00:00
|
|
|
let pat = render_pat(
|
|
|
|
&ctx,
|
|
|
|
pattern_ctx,
|
|
|
|
&escaped_name,
|
|
|
|
kind,
|
|
|
|
&visible_fields,
|
|
|
|
fields_omitted,
|
|
|
|
)?;
|
2022-08-15 16:24:21 +00:00
|
|
|
(label, lookup, pat)
|
2022-07-28 13:47:46 +00:00
|
|
|
}
|
|
|
|
};
|
2020-12-20 18:20:55 +00:00
|
|
|
|
2023-03-18 06:13:57 +00:00
|
|
|
Some(build_completion(
|
|
|
|
ctx,
|
|
|
|
label,
|
|
|
|
lookup,
|
|
|
|
pat,
|
|
|
|
variant,
|
|
|
|
enum_ty,
|
|
|
|
pattern_ctx.missing_variants.contains(&variant),
|
|
|
|
))
|
2020-12-22 18:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build_completion(
|
|
|
|
ctx: RenderContext<'_>,
|
2022-06-25 18:33:19 +00:00
|
|
|
label: SmolStr,
|
2022-08-15 16:24:21 +00:00
|
|
|
lookup: SmolStr,
|
2020-12-22 18:00:38 +00:00
|
|
|
pat: String,
|
2023-09-02 14:26:48 +00:00
|
|
|
def: impl HasDocs + Copy,
|
2023-03-14 06:11:25 +00:00
|
|
|
adt_ty: hir::Type,
|
2023-03-18 06:13:57 +00:00
|
|
|
// Missing in context of match statement completions
|
|
|
|
is_variant_missing: bool,
|
2020-12-22 18:00:38 +00:00
|
|
|
) -> CompletionItem {
|
2022-12-12 11:18:55 +00:00
|
|
|
let mut relevance = ctx.completion_relevance();
|
|
|
|
|
2023-03-18 06:13:57 +00:00
|
|
|
if is_variant_missing {
|
2023-03-16 17:22:35 +00:00
|
|
|
relevance.type_match = super::compute_type_match(ctx.completion, &adt_ty);
|
|
|
|
}
|
2022-12-12 11:18:55 +00:00
|
|
|
|
2022-06-25 18:33:19 +00:00
|
|
|
let mut item = CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), label);
|
2022-02-02 01:05:49 +00:00
|
|
|
item.set_documentation(ctx.docs(def))
|
|
|
|
.set_deprecated(ctx.is_deprecated(def))
|
|
|
|
.detail(&pat)
|
2022-08-15 16:24:21 +00:00
|
|
|
.lookup_by(lookup)
|
2022-12-12 11:18:55 +00:00
|
|
|
.set_relevance(relevance);
|
2021-10-03 12:53:01 +00:00
|
|
|
match ctx.snippet_cap() {
|
|
|
|
Some(snippet_cap) => item.insert_snippet(snippet_cap, pat),
|
|
|
|
None => item.insert_text(pat),
|
2020-12-22 18:00:38 +00:00
|
|
|
};
|
2023-05-24 16:04:29 +00:00
|
|
|
item.build(ctx.db())
|
2020-12-20 18:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn render_pat(
|
|
|
|
ctx: &RenderContext<'_>,
|
2022-06-20 15:41:04 +00:00
|
|
|
pattern_ctx: &PatternContext,
|
2020-12-20 18:20:55 +00:00
|
|
|
name: &str,
|
|
|
|
kind: StructKind,
|
|
|
|
fields: &[hir::Field],
|
|
|
|
fields_omitted: bool,
|
|
|
|
) -> Option<String> {
|
|
|
|
let mut pat = match kind {
|
2022-06-20 15:41:04 +00:00
|
|
|
StructKind::Tuple => render_tuple_as_pat(ctx.snippet_cap(), fields, name, fields_omitted),
|
|
|
|
StructKind::Record => {
|
2021-06-13 03:54:16 +00:00
|
|
|
render_record_as_pat(ctx.db(), ctx.snippet_cap(), fields, name, fields_omitted)
|
2020-12-20 18:16:28 +00:00
|
|
|
}
|
2024-02-09 15:47:35 +00:00
|
|
|
StructKind::Unit => name.to_owned(),
|
2020-12-20 17:19:23 +00:00
|
|
|
};
|
|
|
|
|
2022-06-20 15:41:04 +00:00
|
|
|
let needs_ascription = matches!(
|
|
|
|
pattern_ctx,
|
|
|
|
PatternContext {
|
2022-07-16 10:17:31 +00:00
|
|
|
param_ctx: Some(ParamContext { kind: ParamKind::Function(_), .. }),
|
2022-06-20 15:41:04 +00:00
|
|
|
has_type_ascription: false,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
);
|
2022-06-18 06:58:47 +00:00
|
|
|
if needs_ascription {
|
2020-12-20 17:19:23 +00:00
|
|
|
pat.push(':');
|
|
|
|
pat.push(' ');
|
2021-06-13 03:54:16 +00:00
|
|
|
pat.push_str(name);
|
2020-12-20 17:19:23 +00:00
|
|
|
}
|
|
|
|
if ctx.snippet_cap().is_some() {
|
|
|
|
pat.push_str("$0");
|
|
|
|
}
|
2020-12-20 18:20:55 +00:00
|
|
|
Some(pat)
|
2020-12-20 17:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn render_record_as_pat(
|
|
|
|
db: &dyn HirDatabase,
|
2020-12-20 18:16:28 +00:00
|
|
|
snippet_cap: Option<SnippetCap>,
|
2020-12-20 17:19:23 +00:00
|
|
|
fields: &[hir::Field],
|
|
|
|
name: &str,
|
|
|
|
fields_omitted: bool,
|
|
|
|
) -> String {
|
2020-12-20 18:16:28 +00:00
|
|
|
let fields = fields.iter();
|
2022-03-16 15:27:55 +00:00
|
|
|
match snippet_cap {
|
|
|
|
Some(_) => {
|
|
|
|
format!(
|
|
|
|
"{name} {{ {}{} }}",
|
|
|
|
fields.enumerate().format_with(", ", |(idx, field), f| {
|
2023-05-24 16:04:29 +00:00
|
|
|
f(&format_args!("{}${}", field.name(db).display(db.upcast()), idx + 1))
|
2022-03-16 15:27:55 +00:00
|
|
|
}),
|
|
|
|
if fields_omitted { ", .." } else { "" },
|
|
|
|
name = name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
format!(
|
|
|
|
"{name} {{ {}{} }}",
|
2024-07-16 10:43:58 +00:00
|
|
|
fields.map(|field| field.name(db).display_no_db().to_smolstr()).format(", "),
|
2022-03-16 15:27:55 +00:00
|
|
|
if fields_omitted { ", .." } else { "" },
|
|
|
|
name = name
|
|
|
|
)
|
|
|
|
}
|
2020-12-20 18:16:28 +00:00
|
|
|
}
|
2020-12-20 17:19:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 15:27:55 +00:00
|
|
|
fn render_tuple_as_pat(
|
|
|
|
snippet_cap: Option<SnippetCap>,
|
2021-03-15 16:23:08 +00:00
|
|
|
fields: &[hir::Field],
|
2022-03-16 15:27:55 +00:00
|
|
|
name: &str,
|
|
|
|
fields_omitted: bool,
|
|
|
|
) -> String {
|
|
|
|
let fields = fields.iter();
|
|
|
|
match snippet_cap {
|
|
|
|
Some(_) => {
|
|
|
|
format!(
|
|
|
|
"{name}({}{})",
|
|
|
|
fields
|
|
|
|
.enumerate()
|
|
|
|
.format_with(", ", |(idx, _), f| { f(&format_args!("${}", idx + 1)) }),
|
|
|
|
if fields_omitted { ", .." } else { "" },
|
|
|
|
name = name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
format!(
|
|
|
|
"{name}({}{})",
|
|
|
|
fields.enumerate().map(|(idx, _)| idx).format(", "),
|
|
|
|
if fields_omitted { ", .." } else { "" },
|
|
|
|
name = name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 16:23:08 +00:00
|
|
|
}
|