fix: Fix pattern completions adding unnecessary braces

This commit is contained in:
Lukas Wirth 2022-07-28 15:47:46 +02:00
parent e782e59d3d
commit 7c59d7c75c
4 changed files with 33 additions and 9 deletions

View File

@ -400,7 +400,7 @@ impl Completions {
) {
if let PathCompletionCtx { kind: PathKind::Pat { pat_ctx }, .. } = path_ctx {
cov_mark::hit!(enum_variant_pattern_path);
self.add_variant_pat(ctx, pat_ctx, variant, local_name);
self.add_variant_pat(ctx, pat_ctx, Some(path_ctx), variant, local_name);
return;
}
@ -484,12 +484,14 @@ impl Completions {
&mut self,
ctx: &CompletionContext<'_>,
pattern_ctx: &PatternContext,
path_ctx: Option<&PathCompletionCtx>,
variant: hir::Variant,
local_name: Option<hir::Name>,
) {
self.add_opt(render_variant_pat(
RenderContext::new(ctx),
pattern_ctx,
path_ctx,
variant,
local_name.clone(),
None,
@ -504,7 +506,14 @@ impl Completions {
path: hir::ModPath,
) {
let path = Some(&path);
self.add_opt(render_variant_pat(RenderContext::new(ctx), pattern_ctx, variant, None, path));
self.add_opt(render_variant_pat(
RenderContext::new(ctx),
pattern_ctx,
None,
variant,
None,
path,
));
}
pub(crate) fn add_struct_pat(

View File

@ -74,7 +74,7 @@ pub(crate) fn complete_pattern(
hir::ModuleDef::Variant(variant)
if refutable || single_variant_enum(variant.parent_enum(ctx.db)) =>
{
acc.add_variant_pat(ctx, pattern_ctx, variant, Some(name.clone()));
acc.add_variant_pat(ctx, pattern_ctx, None, variant, Some(name.clone()));
true
}
hir::ModuleDef::Adt(hir::Adt::Enum(e)) => refutable || single_variant_enum(e),

View File

@ -6,7 +6,7 @@ use itertools::Itertools;
use syntax::SmolStr;
use crate::{
context::{ParamContext, ParamKind, PatternContext},
context::{ParamContext, ParamKind, PathCompletionCtx, PatternContext},
render::{
variant::{format_literal_label, visible_fields},
RenderContext,
@ -42,6 +42,7 @@ pub(crate) fn render_struct_pat(
pub(crate) fn render_variant_pat(
ctx: RenderContext<'_>,
pattern_ctx: &PatternContext,
path_ctx: Option<&PathCompletionCtx>,
variant: hir::Variant,
local_name: Option<Name>,
path: Option<&hir::ModPath>,
@ -58,9 +59,23 @@ pub(crate) fn render_variant_pat(
(name.to_smol_str(), name.escaped().to_smol_str())
}
};
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)?;
let (label, pat) = match path_ctx {
Some(PathCompletionCtx { has_call_parens: true, .. }) => (name, escaped_name.to_string()),
_ => {
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,
)?;
(label, pat)
}
};
Some(build_completion(ctx, label, pat, variant))
}

View File

@ -443,7 +443,7 @@ fn foo() {
}
"#,
expect![[r#"
bn TupleVariant() TupleVariant($1)$0
bn TupleVariant TupleVariant
"#]],
);
check_empty(
@ -458,7 +458,7 @@ fn foo() {
}
"#,
expect![[r#"
bn RecordVariant {} RecordVariant { field$1 }$0
bn RecordVariant RecordVariant
"#]],
);
}