fix: variants rendering in pattern path

This commit is contained in:
yue4u 2022-06-26 03:33:19 +09:00
parent ec78b6b08a
commit 622b516c74
9 changed files with 83 additions and 73 deletions

View file

@ -7,7 +7,10 @@ use syntax::SmolStr;
use crate::{
context::{ParamKind, PatternContext},
render::{variant::visible_fields, RenderContext},
render::{
variant::{format_literal_label, visible_fields},
RenderContext,
},
CompletionItem, CompletionItemKind,
};
@ -29,16 +32,11 @@ pub(crate) fn render_struct_pat(
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db()));
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
let pat = render_pat(
&ctx,
pattern_ctx,
&escaped_name,
strukt.kind(ctx.db()),
&visible_fields,
fields_omitted,
)?;
let kind = strukt.kind(ctx.db());
let label = format_literal_label(name.as_str(), kind);
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
Some(build_completion(ctx, name, pat, strukt))
Some(build_completion(ctx, label, pat, strukt))
}
pub(crate) fn render_variant_pat(
@ -60,25 +58,20 @@ pub(crate) fn render_variant_pat(
(name.to_smol_str(), name.escaped().to_smol_str())
}
};
let pat = render_pat(
&ctx,
pattern_ctx,
&escaped_name,
variant.kind(ctx.db()),
&visible_fields,
fields_omitted,
)?;
let kind = variant.kind(ctx.db());
let label = format_literal_label(name.as_str(), kind);
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
Some(build_completion(ctx, name, pat, variant))
Some(build_completion(ctx, label, pat, variant))
}
fn build_completion(
ctx: RenderContext<'_>,
name: SmolStr,
label: SmolStr,
pat: String,
def: impl HasAttrs + Copy,
) -> CompletionItem {
let mut item = CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), name);
let mut item = CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), label);
item.set_documentation(ctx.docs(def))
.set_deprecated(ctx.is_deprecated(def))
.detail(&pat)