2020-12-20 17:19:23 +00:00
|
|
|
//! Renderer for patterns.
|
|
|
|
|
2020-12-20 19:13:06 +00:00
|
|
|
use hir::{db::HirDatabase, HasAttrs, HasVisibility, Name, StructKind};
|
2021-01-06 17:43:46 +00:00
|
|
|
use ide_db::helpers::SnippetCap;
|
2020-12-20 17:19:23 +00:00
|
|
|
use itertools::Itertools;
|
|
|
|
|
2021-08-14 16:18:18 +00:00
|
|
|
use crate::{
|
|
|
|
context::ParamKind, item::CompletionKind, render::RenderContext, CompletionItem,
|
|
|
|
CompletionItemKind,
|
|
|
|
};
|
2020-12-20 17:19:23 +00:00
|
|
|
|
2020-12-20 18:20:55 +00:00
|
|
|
pub(crate) fn render_struct_pat(
|
|
|
|
ctx: RenderContext<'_>,
|
2020-12-20 17:19:23 +00:00
|
|
|
strukt: hir::Struct,
|
|
|
|
local_name: Option<Name>,
|
|
|
|
) -> Option<CompletionItem> {
|
|
|
|
let _p = profile::span("render_struct_pat");
|
|
|
|
|
|
|
|
let fields = strukt.fields(ctx.db());
|
2020-12-22 18:00:38 +00:00
|
|
|
let (visible_fields, fields_omitted) = visible_fields(&ctx, &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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_string();
|
2020-12-22 18:00:38 +00:00
|
|
|
let pat = render_pat(&ctx, &name, strukt.kind(ctx.db()), &visible_fields, fields_omitted)?;
|
2020-12-20 17:19:23 +00:00
|
|
|
|
2020-12-22 18:00:38 +00:00
|
|
|
Some(build_completion(ctx, name, pat, strukt))
|
2020-12-20 17:19:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 18:20:55 +00:00
|
|
|
pub(crate) fn render_variant_pat(
|
|
|
|
ctx: RenderContext<'_>,
|
2020-12-20 17:19:23 +00:00
|
|
|
variant: hir::Variant,
|
|
|
|
local_name: Option<Name>,
|
2021-02-09 20:32:05 +00:00
|
|
|
path: Option<hir::ModPath>,
|
2020-12-20 17:19:23 +00:00
|
|
|
) -> Option<CompletionItem> {
|
|
|
|
let _p = profile::span("render_variant_pat");
|
|
|
|
|
|
|
|
let fields = variant.fields(ctx.db());
|
2020-12-22 18:00:38 +00:00
|
|
|
let (visible_fields, fields_omitted) = visible_fields(&ctx, &fields, variant)?;
|
2020-12-20 17:19:23 +00:00
|
|
|
|
2021-02-09 20:32:05 +00:00
|
|
|
let name = match &path {
|
|
|
|
Some(path) => path.to_string(),
|
|
|
|
None => local_name.unwrap_or_else(|| variant.name(ctx.db())).to_string(),
|
|
|
|
};
|
2020-12-22 18:00:38 +00:00
|
|
|
let pat = render_pat(&ctx, &name, variant.kind(ctx.db()), &visible_fields, fields_omitted)?;
|
2020-12-20 18:20:55 +00:00
|
|
|
|
2020-12-22 18:00:38 +00:00
|
|
|
Some(build_completion(ctx, name, pat, variant))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_completion(
|
|
|
|
ctx: RenderContext<'_>,
|
|
|
|
name: String,
|
|
|
|
pat: String,
|
2021-03-12 09:12:32 +00:00
|
|
|
def: impl HasAttrs + Copy,
|
2020-12-22 18:00:38 +00:00
|
|
|
) -> CompletionItem {
|
2021-03-12 09:12:32 +00:00
|
|
|
let mut item = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name);
|
|
|
|
item.kind(CompletionItemKind::Binding)
|
|
|
|
.set_documentation(ctx.docs(def))
|
|
|
|
.set_deprecated(ctx.is_deprecated(def))
|
2020-12-20 18:20:55 +00:00
|
|
|
.detail(&pat);
|
2021-03-11 15:46:41 +00:00
|
|
|
if let Some(snippet_cap) = ctx.snippet_cap() {
|
2021-03-12 09:12:32 +00:00
|
|
|
item.insert_snippet(snippet_cap, pat);
|
2020-12-20 18:20:55 +00:00
|
|
|
} else {
|
2021-03-12 09:12:32 +00:00
|
|
|
item.insert_text(pat);
|
2020-12-22 18:00:38 +00:00
|
|
|
};
|
2021-03-12 09:12:32 +00:00
|
|
|
item.build()
|
2020-12-20 18:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn render_pat(
|
|
|
|
ctx: &RenderContext<'_>,
|
|
|
|
name: &str,
|
|
|
|
kind: StructKind,
|
|
|
|
fields: &[hir::Field],
|
|
|
|
fields_omitted: bool,
|
|
|
|
) -> Option<String> {
|
|
|
|
let mut pat = match kind {
|
2020-12-20 17:19:23 +00:00
|
|
|
StructKind::Tuple if ctx.snippet_cap().is_some() => {
|
2021-06-13 03:54:16 +00:00
|
|
|
render_tuple_as_pat(fields, name, fields_omitted)
|
2020-12-20 17:19:23 +00:00
|
|
|
}
|
2020-12-20 18:16:28 +00:00
|
|
|
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
|
|
|
}
|
2020-12-20 17:19:23 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
2021-08-14 16:18:18 +00:00
|
|
|
if ctx.completion.is_param == Some(ParamKind::Function) {
|
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();
|
|
|
|
if snippet_cap.is_some() {
|
|
|
|
format!(
|
|
|
|
"{name} {{ {}{} }}",
|
|
|
|
fields
|
|
|
|
.enumerate()
|
2020-12-21 14:10:27 +00:00
|
|
|
.map(|(idx, field)| format!("{}${}", field.name(db), idx + 1))
|
2020-12-20 18:16:28 +00:00
|
|
|
.format(", "),
|
|
|
|
if fields_omitted { ", .." } else { "" },
|
|
|
|
name = name
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"{name} {{ {}{} }}",
|
|
|
|
fields.map(|field| field.name(db)).format(", "),
|
|
|
|
if fields_omitted { ", .." } else { "" },
|
|
|
|
name = name
|
|
|
|
)
|
|
|
|
}
|
2020-12-20 17:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool) -> String {
|
|
|
|
format!(
|
|
|
|
"{name}({}{})",
|
2020-12-20 18:16:28 +00:00
|
|
|
fields.iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "),
|
2020-12-20 17:19:23 +00:00
|
|
|
if fields_omitted { ", .." } else { "" },
|
|
|
|
name = name
|
|
|
|
)
|
|
|
|
}
|
2021-03-15 16:23:08 +00:00
|
|
|
|
|
|
|
fn visible_fields(
|
|
|
|
ctx: &RenderContext<'_>,
|
|
|
|
fields: &[hir::Field],
|
|
|
|
item: impl HasAttrs,
|
|
|
|
) -> Option<(Vec<hir::Field>, bool)> {
|
|
|
|
let module = ctx.completion.scope.module()?;
|
|
|
|
let n_fields = fields.len();
|
|
|
|
let fields = fields
|
2021-07-21 17:50:28 +00:00
|
|
|
.iter()
|
2021-03-15 16:23:08 +00:00
|
|
|
.filter(|field| field.is_visible_from(ctx.db(), module))
|
|
|
|
.copied()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let fields_omitted =
|
|
|
|
n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
|
|
|
|
Some((fields, fields_omitted))
|
|
|
|
}
|