mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-01 07:48:45 +00:00
fix: escape for enum variant
This commit is contained in:
parent
622b516c74
commit
ea7ea7079c
2 changed files with 57 additions and 3 deletions
|
@ -96,7 +96,7 @@ fn render_pat(
|
||||||
StructKind::Record => {
|
StructKind::Record => {
|
||||||
render_record_as_pat(ctx.db(), ctx.snippet_cap(), fields, name, fields_omitted)
|
render_record_as_pat(ctx.db(), ctx.snippet_cap(), fields, name, fields_omitted)
|
||||||
}
|
}
|
||||||
StructKind::Unit => return None,
|
StructKind::Unit => name.to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let needs_ascription = matches!(
|
let needs_ascription = matches!(
|
||||||
|
@ -131,7 +131,7 @@ fn render_record_as_pat(
|
||||||
format!(
|
format!(
|
||||||
"{name} {{ {}{} }}",
|
"{name} {{ {}{} }}",
|
||||||
fields.enumerate().format_with(", ", |(idx, field), f| {
|
fields.enumerate().format_with(", ", |(idx, field), f| {
|
||||||
f(&format_args!("{}${}", field.name(db), idx + 1))
|
f(&format_args!("{}${}", field.name(db).escaped(), idx + 1))
|
||||||
}),
|
}),
|
||||||
if fields_omitted { ", .." } else { "" },
|
if fields_omitted { ", .." } else { "" },
|
||||||
name = name
|
name = name
|
||||||
|
@ -140,7 +140,7 @@ fn render_record_as_pat(
|
||||||
None => {
|
None => {
|
||||||
format!(
|
format!(
|
||||||
"{name} {{ {}{} }}",
|
"{name} {{ {}{} }}",
|
||||||
fields.map(|field| field.name(db)).format(", "),
|
fields.map(|field| field.name(db).escaped().to_smol_str()).format(", "),
|
||||||
if fields_omitted { ", .." } else { "" },
|
if fields_omitted { ", .." } else { "" },
|
||||||
name = name
|
name = name
|
||||||
)
|
)
|
||||||
|
|
|
@ -164,6 +164,7 @@ fn foo() {
|
||||||
ev Variant
|
ev Variant
|
||||||
bn Record {…} Record { field$1 }$0
|
bn Record {…} Record { field$1 }$0
|
||||||
bn Tuple(…) Tuple($1)$0
|
bn Tuple(…) Tuple($1)$0
|
||||||
|
bn Variant Variant$0
|
||||||
kw mut
|
kw mut
|
||||||
kw ref
|
kw ref
|
||||||
"#]],
|
"#]],
|
||||||
|
@ -243,6 +244,7 @@ fn foo() {
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
en E
|
en E
|
||||||
ma m!(…) macro_rules! m
|
ma m!(…) macro_rules! m
|
||||||
|
bn E::X E::X$0
|
||||||
kw mut
|
kw mut
|
||||||
kw ref
|
kw ref
|
||||||
"#]],
|
"#]],
|
||||||
|
@ -318,6 +320,7 @@ fn func() {
|
||||||
ct ASSOC_CONST const ASSOC_CONST: ()
|
ct ASSOC_CONST const ASSOC_CONST: ()
|
||||||
bn RecordV {…} RecordV { field$1 }$0
|
bn RecordV {…} RecordV { field$1 }$0
|
||||||
bn TupleV(…) TupleV($1)$0
|
bn TupleV(…) TupleV($1)$0
|
||||||
|
bn UnitV UnitV$0
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -490,6 +493,57 @@ fn foo() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn completes_enum_variant_pat_escape() {
|
||||||
|
cov_mark::check!(enum_variant_pattern_path);
|
||||||
|
check_empty(
|
||||||
|
r#"
|
||||||
|
enum Enum {
|
||||||
|
A,
|
||||||
|
B { r#type: i32 },
|
||||||
|
r#type,
|
||||||
|
r#struct { r#type: i32 },
|
||||||
|
}
|
||||||
|
fn foo() {
|
||||||
|
match (Enum::A) {
|
||||||
|
$0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
en Enum
|
||||||
|
bn Enum::A Enum::A$0
|
||||||
|
bn Enum::B {…} Enum::B { r#type$1 }$0
|
||||||
|
bn Enum::struct {…} Enum::r#struct { r#type$1 }$0
|
||||||
|
bn Enum::type Enum::r#type$0
|
||||||
|
kw mut
|
||||||
|
kw ref
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
|
||||||
|
check_empty(
|
||||||
|
r#"
|
||||||
|
enum Enum {
|
||||||
|
A,
|
||||||
|
B { r#type: i32 },
|
||||||
|
r#type,
|
||||||
|
r#struct { r#type: i32 },
|
||||||
|
}
|
||||||
|
fn foo() {
|
||||||
|
match (Enum::A) {
|
||||||
|
Enum::$0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
bn A A$0
|
||||||
|
bn B {…} B { r#type$1 }$0
|
||||||
|
bn struct {…} r#struct { r#type$1 }$0
|
||||||
|
bn type r#type$0
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn completes_associated_const() {
|
fn completes_associated_const() {
|
||||||
check_empty(
|
check_empty(
|
||||||
|
|
Loading…
Reference in a new issue