Auto merge of #12898 - Veykril:compl-pat-brace, r=Veykril

fix: Fix pattern completions adding unnecessary braces

Fixes https://github.com/rust-lang/rust-analyzer/issues/12852
This commit is contained in:
bors 2022-07-28 13:48:12 +00:00
commit 02c240ff81
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 { if let PathCompletionCtx { kind: PathKind::Pat { pat_ctx }, .. } = path_ctx {
cov_mark::hit!(enum_variant_pattern_path); 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; return;
} }
@ -484,12 +484,14 @@ impl Completions {
&mut self, &mut self,
ctx: &CompletionContext<'_>, ctx: &CompletionContext<'_>,
pattern_ctx: &PatternContext, pattern_ctx: &PatternContext,
path_ctx: Option<&PathCompletionCtx>,
variant: hir::Variant, variant: hir::Variant,
local_name: Option<hir::Name>, local_name: Option<hir::Name>,
) { ) {
self.add_opt(render_variant_pat( self.add_opt(render_variant_pat(
RenderContext::new(ctx), RenderContext::new(ctx),
pattern_ctx, pattern_ctx,
path_ctx,
variant, variant,
local_name.clone(), local_name.clone(),
None, None,
@ -504,7 +506,14 @@ impl Completions {
path: hir::ModPath, path: hir::ModPath,
) { ) {
let path = Some(&path); 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( pub(crate) fn add_struct_pat(

View file

@ -74,7 +74,7 @@ pub(crate) fn complete_pattern(
hir::ModuleDef::Variant(variant) hir::ModuleDef::Variant(variant)
if refutable || single_variant_enum(variant.parent_enum(ctx.db)) => 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 true
} }
hir::ModuleDef::Adt(hir::Adt::Enum(e)) => refutable || single_variant_enum(e), 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 syntax::SmolStr;
use crate::{ use crate::{
context::{ParamContext, ParamKind, PatternContext}, context::{ParamContext, ParamKind, PathCompletionCtx, PatternContext},
render::{ render::{
variant::{format_literal_label, visible_fields}, variant::{format_literal_label, visible_fields},
RenderContext, RenderContext,
@ -42,6 +42,7 @@ pub(crate) fn render_struct_pat(
pub(crate) fn render_variant_pat( pub(crate) fn render_variant_pat(
ctx: RenderContext<'_>, ctx: RenderContext<'_>,
pattern_ctx: &PatternContext, pattern_ctx: &PatternContext,
path_ctx: Option<&PathCompletionCtx>,
variant: hir::Variant, variant: hir::Variant,
local_name: Option<Name>, local_name: Option<Name>,
path: Option<&hir::ModPath>, path: Option<&hir::ModPath>,
@ -58,9 +59,23 @@ pub(crate) fn render_variant_pat(
(name.to_smol_str(), name.escaped().to_smol_str()) (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 (label, pat) = match path_ctx {
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?; 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)) Some(build_completion(ctx, label, pat, variant))
} }

View file

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