Skip to content

Commit ea7ea70

Browse files
committed
fix: escape for enum variant
1 parent 622b516 commit ea7ea70

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

crates/ide-completion/src/render/pattern.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn render_pat(
9696
StructKind::Record => {
9797
render_record_as_pat(ctx.db(), ctx.snippet_cap(), fields, name, fields_omitted)
9898
}
99-
StructKind::Unit => return None,
99+
StructKind::Unit => name.to_string(),
100100
};
101101

102102
let needs_ascription = matches!(
@@ -131,7 +131,7 @@ fn render_record_as_pat(
131131
format!(
132132
"{name} {{ {}{} }}",
133133
fields.enumerate().format_with(", ", |(idx, field), f| {
134-
f(&format_args!("{}${}", field.name(db), idx + 1))
134+
f(&format_args!("{}${}", field.name(db).escaped(), idx + 1))
135135
}),
136136
if fields_omitted { ", .." } else { "" },
137137
name = name
@@ -140,7 +140,7 @@ fn render_record_as_pat(
140140
None => {
141141
format!(
142142
"{name} {{ {}{} }}",
143-
fields.map(|field| field.name(db)).format(", "),
143+
fields.map(|field| field.name(db).escaped().to_smol_str()).format(", "),
144144
if fields_omitted { ", .." } else { "" },
145145
name = name
146146
)

crates/ide-completion/src/tests/pattern.rs

+54
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ fn foo() {
164164
ev Variant
165165
bn Record {…} Record { field$1 }$0
166166
bn Tuple(…) Tuple($1)$0
167+
bn Variant Variant$0
167168
kw mut
168169
kw ref
169170
"#]],
@@ -243,6 +244,7 @@ fn foo() {
243244
expect![[r#"
244245
en E
245246
ma m!(…) macro_rules! m
247+
bn E::X E::X$0
246248
kw mut
247249
kw ref
248250
"#]],
@@ -318,6 +320,7 @@ fn func() {
318320
ct ASSOC_CONST const ASSOC_CONST: ()
319321
bn RecordV {…} RecordV { field$1 }$0
320322
bn TupleV(…) TupleV($1)$0
323+
bn UnitV UnitV$0
321324
"#]],
322325
);
323326
}
@@ -490,6 +493,57 @@ fn foo() {
490493
);
491494
}
492495

496+
#[test]
497+
fn completes_enum_variant_pat_escape() {
498+
cov_mark::check!(enum_variant_pattern_path);
499+
check_empty(
500+
r#"
501+
enum Enum {
502+
A,
503+
B { r#type: i32 },
504+
r#type,
505+
r#struct { r#type: i32 },
506+
}
507+
fn foo() {
508+
match (Enum::A) {
509+
$0
510+
}
511+
}
512+
"#,
513+
expect![[r#"
514+
en Enum
515+
bn Enum::A Enum::A$0
516+
bn Enum::B {…} Enum::B { r#type$1 }$0
517+
bn Enum::struct {…} Enum::r#struct { r#type$1 }$0
518+
bn Enum::type Enum::r#type$0
519+
kw mut
520+
kw ref
521+
"#]],
522+
);
523+
524+
check_empty(
525+
r#"
526+
enum Enum {
527+
A,
528+
B { r#type: i32 },
529+
r#type,
530+
r#struct { r#type: i32 },
531+
}
532+
fn foo() {
533+
match (Enum::A) {
534+
Enum::$0
535+
}
536+
}
537+
"#,
538+
expect![[r#"
539+
bn A A$0
540+
bn B {…} B { r#type$1 }$0
541+
bn struct {…} r#struct { r#type$1 }$0
542+
bn type r#type$0
543+
"#]],
544+
);
545+
}
546+
493547
#[test]
494548
fn completes_associated_const() {
495549
check_empty(

0 commit comments

Comments
 (0)