Use snippet placeholders for generated match arms

This commit is contained in:
Giga Bowser 2024-11-01 12:35:01 -04:00
parent d1fbfc676b
commit 92b2b3d26b
2 changed files with 114 additions and 108 deletions

View file

@ -30,8 +30,8 @@ use crate::{utils, AssistContext, AssistId, AssistKind, Assists};
// //
// fn handle(action: Action) { // fn handle(action: Action) {
// match action { // match action {
// $0Action::Move { distance } => todo!(), // Action::Move { distance } => ${1:todo!()},
// Action::Stop => todo!(), // Action::Stop => ${0:todo!()},
// } // }
// } // }
// ``` // ```
@ -234,8 +234,10 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
} }
} }
let mut todo_placeholders = Vec::new();
let mut first_new_arm = None; let mut first_new_arm = None;
for arm in missing_arms { for arm in missing_arms {
todo_placeholders.push(arm.expr().unwrap());
first_new_arm.get_or_insert_with(|| arm.clone()); first_new_arm.get_or_insert_with(|| arm.clone());
new_match_arm_list.add_arm(arm); new_match_arm_list.add_arm(arm);
} }
@ -248,14 +250,18 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
make::ext::expr_todo(), make::ext::expr_todo(),
) )
.clone_for_update(); .clone_for_update();
todo_placeholders.push(arm.expr().unwrap());
first_new_arm.get_or_insert_with(|| arm.clone()); first_new_arm.get_or_insert_with(|| arm.clone());
new_match_arm_list.add_arm(arm); new_match_arm_list.add_arm(arm);
} }
if let (Some(first_new_arm), Some(cap)) = (first_new_arm, ctx.config.snippet_cap) { if let (Some(first_new_arm), Some(cap)) = (first_new_arm, ctx.config.snippet_cap) {
match first_new_arm.syntax().descendants().find_map(ast::WildcardPat::cast) { if let Some(it) = first_new_arm.syntax().descendants().find_map(ast::WildcardPat::cast) {
Some(it) => edit.add_placeholder_snippet(cap, it), edit.add_placeholder_snippet(cap, it);
None => edit.add_tabstop_before(cap, first_new_arm), }
for placeholder in todo_placeholders {
edit.add_placeholder_snippet(cap, placeholder);
} }
} }
@ -581,8 +587,8 @@ fn foo(a: bool) {
r#" r#"
fn foo(a: bool) { fn foo(a: bool) {
match a { match a {
$0true => todo!(), true => ${1:todo!()},
false => todo!(), false => ${0:todo!()},
} }
} }
"#, "#,
@ -604,7 +610,7 @@ fn foo(a: bool) {
fn foo(a: bool) { fn foo(a: bool) {
match a { match a {
true => {} true => {}
$0false => todo!(), false => ${0:todo!()},
} }
} }
"#, "#,
@ -654,10 +660,10 @@ fn foo(a: bool) {
r#" r#"
fn foo(a: bool) { fn foo(a: bool) {
match (a, a) { match (a, a) {
$0(true, true) => todo!(), (true, true) => ${1:todo!()},
(true, false) => todo!(), (true, false) => ${2:todo!()},
(false, true) => todo!(), (false, true) => ${3:todo!()},
(false, false) => todo!(), (false, false) => ${0:todo!()},
} }
} }
"#, "#,
@ -677,8 +683,8 @@ fn foo(a: bool) {
r#" r#"
fn foo(a: bool) { fn foo(a: bool) {
match [a] { match [a] {
$0[true] => todo!(), [true] => ${1:todo!()},
[false] => todo!(), [false] => ${0:todo!()},
} }
} }
"#, "#,
@ -695,8 +701,8 @@ fn foo(a: bool) {
r#" r#"
fn foo(a: bool) { fn foo(a: bool) {
match [a,] { match [a,] {
$0[true] => todo!(), [true] => ${1:todo!()},
[false] => todo!(), [false] => ${0:todo!()},
} }
} }
"#, "#,
@ -715,9 +721,9 @@ fn foo(a: bool) {
fn foo(a: bool) { fn foo(a: bool) {
match [a, a] { match [a, a] {
[true, true] => todo!(), [true, true] => todo!(),
$0[true, false] => todo!(), [true, false] => ${1:todo!()},
[false, true] => todo!(), [false, true] => ${2:todo!()},
[false, false] => todo!(), [false, false] => ${0:todo!()},
} }
} }
"#, "#,
@ -734,10 +740,10 @@ fn foo(a: bool) {
r#" r#"
fn foo(a: bool) { fn foo(a: bool) {
match [a, a] { match [a, a] {
$0[true, true] => todo!(), [true, true] => ${1:todo!()},
[true, false] => todo!(), [true, false] => ${2:todo!()},
[false, true] => todo!(), [false, true] => ${3:todo!()},
[false, false] => todo!(), [false, false] => ${0:todo!()},
} }
} }
"#, "#,
@ -759,8 +765,8 @@ fn foo(a: bool) {
fn foo(a: bool) { fn foo(a: bool) {
match (a, a) { match (a, a) {
(true | false, true) => {} (true | false, true) => {}
$0(true, false) => todo!(), (true, false) => ${1:todo!()},
(false, false) => todo!(), (false, false) => ${0:todo!()},
} }
} }
"#, "#,
@ -779,9 +785,9 @@ fn foo(a: bool) {
fn foo(a: bool) { fn foo(a: bool) {
match (a, a) { match (a, a) {
(false, true) => {} (false, true) => {}
$0(true, true) => todo!(), (true, true) => ${1:todo!()},
(true, false) => todo!(), (true, false) => ${2:todo!()},
(false, false) => todo!(), (false, false) => ${0:todo!()},
} }
} }
"#, "#,
@ -815,7 +821,7 @@ fn main() {
match A::As { match A::As {
A::Bs { x, y: Some(_) } => {} A::Bs { x, y: Some(_) } => {}
A::Cs(_, Some(_)) => {} A::Cs(_, Some(_)) => {}
$0A::As => todo!(), A::As => ${0:todo!()},
} }
} }
"#, "#,
@ -838,7 +844,7 @@ fn main() {
fn main() { fn main() {
match None { match None {
None => {} None => {}
Some(${0:_}) => todo!(), Some(${1:_}) => ${0:todo!()},
} }
} }
"#, "#,
@ -862,7 +868,7 @@ enum A { As, Bs, Cs(Option<i32>) }
fn main() { fn main() {
match A::As { match A::As {
A::Cs(_) | A::Bs => {} A::Cs(_) | A::Bs => {}
$0A::As => todo!(), A::As => ${0:todo!()},
} }
} }
"#, "#,
@ -892,8 +898,8 @@ fn main() {
A::Bs if 0 < 1 => {} A::Bs if 0 < 1 => {}
A::Ds(_value) => { let x = 1; } A::Ds(_value) => { let x = 1; }
A::Es(B::Xs) => (), A::Es(B::Xs) => (),
$0A::As => todo!(), A::As => ${1:todo!()},
A::Cs => todo!(), A::Cs => ${0:todo!()},
} }
} }
"#, "#,
@ -919,7 +925,7 @@ fn main() {
match A::As { match A::As {
A::As(_) => {} A::As(_) => {}
a @ A::Bs(_) => {} a @ A::Bs(_) => {}
A::Cs(${0:_}) => todo!(), A::Cs(${1:_}) => ${0:todo!()},
} }
} }
"#, "#,
@ -945,11 +951,11 @@ enum A { As, Bs, Cs(String), Ds(String, String), Es { x: usize, y: usize } }
fn main() { fn main() {
let a = A::As; let a = A::As;
match a { match a {
$0A::As => todo!(), A::As => ${1:todo!()},
A::Bs => todo!(), A::Bs => ${2:todo!()},
A::Cs(_) => todo!(), A::Cs(_) => ${3:todo!()},
A::Ds(_, _) => todo!(), A::Ds(_, _) => ${4:todo!()},
A::Es { x, y } => todo!(), A::Es { x, y } => ${0:todo!()},
} }
} }
"#, "#,
@ -982,9 +988,9 @@ fn main() {
let b = B::One; let b = B::One;
match (a, b) { match (a, b) {
(A::Two, B::One) => {}, (A::Two, B::One) => {},
$0(A::One, B::One) => todo!(), (A::One, B::One) => ${1:todo!()},
(A::One, B::Two) => todo!(), (A::One, B::Two) => ${2:todo!()},
(A::Two, B::Two) => todo!(), (A::Two, B::Two) => ${0:todo!()},
} }
} }
"#, "#,
@ -1013,10 +1019,10 @@ fn main() {
let a = A::One; let a = A::One;
let b = B::One; let b = B::One;
match (a, b) { match (a, b) {
$0(A::One, B::One) => todo!(), (A::One, B::One) => ${1:todo!()},
(A::One, B::Two) => todo!(), (A::One, B::Two) => ${2:todo!()},
(A::Two, B::One) => todo!(), (A::Two, B::One) => ${3:todo!()},
(A::Two, B::Two) => todo!(), (A::Two, B::Two) => ${0:todo!()},
} }
} }
"#, "#,
@ -1045,10 +1051,10 @@ fn main() {
let a = A::One; let a = A::One;
let b = B::One; let b = B::One;
match (&a, &b) { match (&a, &b) {
$0(A::One, B::One) => todo!(), (A::One, B::One) => ${1:todo!()},
(A::One, B::Two) => todo!(), (A::One, B::Two) => ${2:todo!()},
(A::Two, B::One) => todo!(), (A::Two, B::One) => ${3:todo!()},
(A::Two, B::Two) => todo!(), (A::Two, B::Two) => ${0:todo!()},
} }
} }
"#, "#,
@ -1080,9 +1086,9 @@ fn main() {
let b = B::One; let b = B::One;
match (a, b) { match (a, b) {
(A::Two, B::One) => {} (A::Two, B::One) => {}
$0(A::One, B::One) => todo!(), (A::One, B::One) => ${1:todo!()},
(A::One, B::Two) => todo!(), (A::One, B::Two) => ${2:todo!()},
(A::Two, B::Two) => todo!(), (A::Two, B::Two) => ${0:todo!()},
} }
} }
"#, "#,
@ -1107,9 +1113,9 @@ fn main() {
match (A, B, C) { match (A, B, C) {
(A | B , A, A | B | C) => (), (A | B , A, A | B | C) => (),
(A | B | C , B | C, A | B | C) => (), (A | B | C , B | C, A | B | C) => (),
$0(C, A, A) => todo!(), (C, A, A) => ${1:todo!()},
(C, A, B) => todo!(), (C, A, B) => ${2:todo!()},
(C, A, C) => todo!(), (C, A, C) => ${0:todo!()},
} }
} }
"#, "#,
@ -1138,7 +1144,7 @@ fn main() {
match (a, b) { match (a, b) {
(Some(_), _) => {} (Some(_), _) => {}
(None, Some(_)) => {} (None, Some(_)) => {}
$0(None, None) => todo!(), (None, None) => ${0:todo!()},
} }
} }
"#, "#,
@ -1203,8 +1209,8 @@ enum A { One, Two }
fn main() { fn main() {
let a = A::One; let a = A::One;
match (a, ) { match (a, ) {
$0(A::One,) => todo!(), (A::One,) => ${1:todo!()},
(A::Two,) => todo!(), (A::Two,) => ${0:todo!()},
} }
} }
"#, "#,
@ -1228,7 +1234,7 @@ enum A { As }
fn foo(a: &A) { fn foo(a: &A) {
match a { match a {
$0A::As => todo!(), A::As => ${0:todo!()},
} }
} }
"#, "#,
@ -1253,7 +1259,7 @@ enum A {
fn foo(a: &mut A) { fn foo(a: &mut A) {
match a { match a {
$0A::Es { x, y } => todo!(), A::Es { x, y } => ${0:todo!()},
} }
} }
"#, "#,
@ -1313,8 +1319,8 @@ enum E { X, Y }
fn main() { fn main() {
match E::X { match E::X {
$0E::X => todo!(), E::X => ${1:todo!()},
E::Y => todo!(), E::Y => ${0:todo!()},
} }
} }
"#, "#,
@ -1360,8 +1366,8 @@ use foo::E::X;
fn main() { fn main() {
match X { match X {
$0X => todo!(), X => ${1:todo!()},
foo::E::Y => todo!(), foo::E::Y => ${0:todo!()},
} }
} }
"#, "#,
@ -1388,7 +1394,7 @@ fn foo(a: A) {
match a { match a {
// foo bar baz // foo bar baz
A::One => {} A::One => {}
$0A::Two => todo!(), A::Two => ${0:todo!()},
// This is where the rest should be // This is where the rest should be
} }
} }
@ -1412,8 +1418,8 @@ fn foo(a: A) {
enum A { One, Two } enum A { One, Two }
fn foo(a: A) { fn foo(a: A) {
match a { match a {
$0A::One => todo!(), A::One => ${1:todo!()},
A::Two => todo!(), A::Two => ${0:todo!()},
// foo bar baz // foo bar baz
} }
} }
@ -1437,8 +1443,8 @@ fn foo(a: A) {
enum A { One, Two, } enum A { One, Two, }
fn foo(a: A) { fn foo(a: A) {
match a { match a {
$0A::One => todo!(), A::One => ${1:todo!()},
A::Two => todo!(), A::Two => ${0:todo!()},
} }
} }
"#, "#,
@ -1460,8 +1466,8 @@ fn foo(opt: Option<i32>) {
r#" r#"
fn foo(opt: Option<i32>) { fn foo(opt: Option<i32>) {
match opt { match opt {
Some(${0:_}) => todo!(), Some(${1:_}) => ${2:todo!()},
None => todo!(), None => ${0:todo!()},
} }
} }
"#, "#,
@ -1493,9 +1499,9 @@ enum Test {
fn foo(t: Test) { fn foo(t: Test) {
m!(match t { m!(match t {
$0Test::A => todo!(), Test::A => ${1:todo!()},
Test::B => todo!(), Test::B => ${2:todo!()},
Test::C => todo!(), Test::C => ${0:todo!()},
}); });
}"#, }"#,
); );
@ -1530,7 +1536,7 @@ fn foo(t: bool) {
fn foo(t: bool) { fn foo(t: bool) {
match t { match t {
true => 1 + 2, true => 1 + 2,
$0false => todo!(), false => ${0:todo!()},
} }
}"#, }"#,
); );
@ -1550,7 +1556,7 @@ fn foo(t: bool) {
fn foo(t: bool) { fn foo(t: bool) {
match t { match t {
true => 1 + 2, true => 1 + 2,
$0false => todo!(), false => ${0:todo!()},
} }
}"#, }"#,
); );
@ -1571,8 +1577,8 @@ fn foo(t: bool) {
fn foo(t: bool) { fn foo(t: bool) {
match t { match t {
_ => 1 + 2, _ => 1 + 2,
$0true => todo!(), true => ${1:todo!()},
false => todo!(), false => ${0:todo!()},
} }
}"#, }"#,
); );
@ -1595,8 +1601,8 @@ pub enum E { A, #[doc(hidden)] B, }
r#" r#"
fn foo(t: ::e::E) { fn foo(t: ::e::E) {
match t { match t {
$0e::E::A => todo!(), e::E::A => ${1:todo!()},
_ => todo!(), _ => ${0:todo!()},
} }
} }
"#, "#,
@ -1620,9 +1626,9 @@ pub enum E { A, #[doc(hidden)] B, }
r#" r#"
fn foo(t: (bool, ::e::E)) { fn foo(t: (bool, ::e::E)) {
match t { match t {
$0(true, e::E::A) => todo!(), (true, e::E::A) => ${1:todo!()},
(false, e::E::A) => todo!(), (false, e::E::A) => ${2:todo!()},
_ => todo!(), _ => ${0:todo!()},
} }
} }
"#, "#,
@ -1646,7 +1652,7 @@ pub enum E { #[doc(hidden)] A, }
r#" r#"
fn foo(t: ::e::E) { fn foo(t: ::e::E) {
match t { match t {
${0:_} => todo!(), ${1:_} => ${0:todo!()},
} }
} }
"#, "#,
@ -1707,7 +1713,7 @@ pub enum E { A, }
fn foo(t: ::e::E) { fn foo(t: ::e::E) {
match t { match t {
e::E::A => todo!(), e::E::A => todo!(),
${0:_} => todo!(), ${1:_} => ${0:todo!()},
} }
} }
"#, "#,
@ -1732,8 +1738,8 @@ pub enum E { A, }
r#" r#"
fn foo(t: ::e::E) { fn foo(t: ::e::E) {
match t { match t {
$0e::E::A => todo!(), e::E::A => ${1:todo!()},
_ => todo!(), _ => ${0:todo!()},
} }
} }
"#, "#,
@ -1757,8 +1763,8 @@ pub enum E { A, #[doc(hidden)] B }"#,
r#" r#"
fn foo(t: ::e::E) { fn foo(t: ::e::E) {
match t { match t {
$0e::E::A => todo!(), e::E::A => ${1:todo!()},
_ => todo!(), _ => ${0:todo!()},
} }
} }
"#, "#,
@ -1784,7 +1790,7 @@ pub enum E { A, #[doc(hidden)] B }"#,
fn foo(t: ::e::E) { fn foo(t: ::e::E) {
match t { match t {
e::E::A => todo!(), e::E::A => todo!(),
${0:_} => todo!(), ${1:_} => ${0:todo!()},
} }
} }
"#, "#,
@ -1809,7 +1815,7 @@ pub enum E { #[doc(hidden)] A, }"#,
fn foo(t: ::e::E, b: bool) { fn foo(t: ::e::E, b: bool) {
match t { match t {
_ if b => todo!(), _ if b => todo!(),
${0:_} => todo!(), ${1:_} => ${0:todo!()},
} }
} }
"#, "#,
@ -1850,8 +1856,8 @@ pub enum E { A, #[doc(hidden)] B, }"#,
r#" r#"
fn foo(t: ::e::E) { fn foo(t: ::e::E) {
match t { match t {
$0e::E::A => todo!(), e::E::A => ${1:todo!()},
_ => todo!(), _ => ${0:todo!()},
} }
} }
"#, "#,
@ -1874,8 +1880,8 @@ enum E { A, #[doc(hidden)] B, }
fn foo(t: E) { fn foo(t: E) {
match t { match t {
$0E::A => todo!(), E::A => ${1:todo!()},
E::B => todo!(), E::B => ${0:todo!()},
} }
}"#, }"#,
); );
@ -1899,8 +1905,8 @@ enum E { A, B, }
fn foo(t: E) { fn foo(t: E) {
match t { match t {
$0E::A => todo!(), E::A => ${1:todo!()},
E::B => todo!(), E::B => ${0:todo!()},
} }
}"#, }"#,
); );
@ -1924,8 +1930,8 @@ enum E { A, #[doc(hidden)] B, }
fn foo(t: E) { fn foo(t: E) {
match t { match t {
$0E::A => todo!(), E::A => ${1:todo!()},
E::B => todo!(), E::B => ${0:todo!()},
} }
}"#, }"#,
); );
@ -1975,8 +1981,8 @@ enum A {
fn a() { fn a() {
let b = A::A; let b = A::A;
match b { match b {
$0A::A => todo!(), A::A => ${1:todo!()},
A::Missing { a, u32, c } => todo!(), A::Missing { a, u32, c } => ${0:todo!()},
} }
}"#, }"#,
) )
@ -2013,8 +2019,8 @@ pub enum E {
fn f() { fn f() {
let value = E::A; let value = E::A;
match value { match value {
$0E::A => todo!(), E::A => ${1:todo!()},
E::B(s) => todo!(), E::B(s) => ${0:todo!()},
} }
} }
"#, "#,
@ -2051,8 +2057,8 @@ pub enum E {
fn f() { fn f() {
let value = E::A; let value = E::A;
match value { match value {
$0E::A => todo!(), E::A => ${1:todo!()},
E::B(s1, s2) => todo!(), E::B(s1, s2) => ${0:todo!()},
} }
} }
"#, "#,

View file

@ -187,8 +187,8 @@ enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) { fn handle(action: Action) {
match action { match action {
$0Action::Move { distance } => todo!(), Action::Move { distance } => ${1:todo!()},
Action::Stop => todo!(), Action::Stop => ${0:todo!()},
} }
} }
"#####, "#####,