Snippetify fill_match_arms

This commit is contained in:
Aleksey Kladov 2020-05-20 14:00:37 +02:00
parent a622b54ac0
commit c446fd76a2
3 changed files with 79 additions and 118 deletions

View file

@ -6,7 +6,10 @@ use ra_ide_db::RootDatabase;
use ra_syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat}; use ra_syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat};
use test_utils::mark; use test_utils::mark;
use crate::{utils::FamousDefs, AssistContext, AssistId, Assists}; use crate::{
utils::{render_snippet, Cursor, FamousDefs},
AssistContext, AssistId, Assists,
};
// Assist: fill_match_arms // Assist: fill_match_arms
// //
@ -27,7 +30,7 @@ use crate::{utils::FamousDefs, AssistContext, AssistId, Assists};
// //
// fn handle(action: Action) { // fn handle(action: Action) {
// match action { // match action {
// Action::Move { distance } => {} // $0Action::Move { distance } => {}
// Action::Stop => {} // Action::Stop => {}
// } // }
// } // }
@ -100,10 +103,23 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
} }
let target = match_expr.syntax().text_range(); let target = match_expr.syntax().text_range();
acc.add(AssistId("fill_match_arms"), "Fill match arms", target, |edit| { acc.add(AssistId("fill_match_arms"), "Fill match arms", target, |builder| {
let new_arm_list = match_arm_list.remove_placeholder().append_arms(missing_arms); let new_arm_list = match_arm_list.remove_placeholder();
edit.set_cursor(expr.syntax().text_range().start()); let n_old_arms = new_arm_list.arms().count();
edit.replace_ast(match_arm_list, new_arm_list); let new_arm_list = new_arm_list.append_arms(missing_arms);
let first_new_arm = new_arm_list.arms().nth(n_old_arms);
let old_range = match_arm_list.syntax().text_range();
match (first_new_arm, ctx.config.snippet_cap) {
(Some(first_new_arm), Some(cap)) => {
let snippet = render_snippet(
cap,
new_arm_list.syntax(),
Cursor::Before(first_new_arm.syntax()),
);
builder.replace_snippet(cap, old_range, snippet);
}
_ => builder.replace(old_range, new_arm_list.to_string()),
}
}) })
} }
@ -226,12 +242,12 @@ mod tests {
r#" r#"
enum A { enum A {
As, As,
Bs{x:i32, y:Option<i32>}, Bs { x: i32, y: Option<i32> },
Cs(i32, Option<i32>), Cs(i32, Option<i32>),
} }
fn main() { fn main() {
match A::As<|> { match A::As<|> {
A::Bs{x,y:Some(_)} => {} A::Bs { x, y: Some(_) } => {}
A::Cs(_, Some(_)) => {} A::Cs(_, Some(_)) => {}
} }
} }
@ -239,14 +255,14 @@ mod tests {
r#" r#"
enum A { enum A {
As, As,
Bs{x:i32, y:Option<i32>}, Bs { x: i32, y: Option<i32> },
Cs(i32, Option<i32>), Cs(i32, Option<i32>),
} }
fn main() { fn main() {
match <|>A::As { match A::As {
A::Bs{x,y:Some(_)} => {} A::Bs { x, y: Some(_) } => {}
A::Cs(_, Some(_)) => {} A::Cs(_, Some(_)) => {}
A::As => {} $0A::As => {}
} }
} }
"#, "#,
@ -276,9 +292,9 @@ mod tests {
Cs(Option<i32>), Cs(Option<i32>),
} }
fn main() { fn main() {
match <|>A::As { match A::As {
A::Cs(_) | A::Bs => {} A::Cs(_) | A::Bs => {}
A::As => {} $0A::As => {}
} }
} }
"#, "#,
@ -322,11 +338,11 @@ mod tests {
Ys, Ys,
} }
fn main() { fn main() {
match <|>A::As { match A::As {
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) => (),
A::As => {} $0A::As => {}
A::Cs => {} A::Cs => {}
} }
} }
@ -344,7 +360,7 @@ mod tests {
Bs, Bs,
Cs(String), Cs(String),
Ds(String, String), Ds(String, String),
Es{ x: usize, y: usize } Es { x: usize, y: usize }
} }
fn main() { fn main() {
@ -358,13 +374,13 @@ mod tests {
Bs, Bs,
Cs(String), Cs(String),
Ds(String, String), Ds(String, String),
Es{ x: usize, y: usize } Es { x: usize, y: usize }
} }
fn main() { fn main() {
let a = A::As; let a = A::As;
match <|>a { match a {
A::As => {} $0A::As => {}
A::Bs => {} A::Bs => {}
A::Cs(_) => {} A::Cs(_) => {}
A::Ds(_, _) => {} A::Ds(_, _) => {}
@ -380,14 +396,8 @@ mod tests {
check_assist( check_assist(
fill_match_arms, fill_match_arms,
r#" r#"
enum A { enum A { One, Two }
One, enum B { One, Two }
Two,
}
enum B {
One,
Two,
}
fn main() { fn main() {
let a = A::One; let a = A::One;
@ -396,20 +406,14 @@ mod tests {
} }
"#, "#,
r#" r#"
enum A { enum A { One, Two }
One, enum B { One, Two }
Two,
}
enum B {
One,
Two,
}
fn main() { fn main() {
let a = A::One; let a = A::One;
let b = B::One; let b = B::One;
match <|>(a, b) { match (a, b) {
(A::One, B::One) => {} $0(A::One, B::One) => {}
(A::One, B::Two) => {} (A::One, B::Two) => {}
(A::Two, B::One) => {} (A::Two, B::One) => {}
(A::Two, B::Two) => {} (A::Two, B::Two) => {}
@ -424,14 +428,8 @@ mod tests {
check_assist( check_assist(
fill_match_arms, fill_match_arms,
r#" r#"
enum A { enum A { One, Two }
One, enum B { One, Two }
Two,
}
enum B {
One,
Two,
}
fn main() { fn main() {
let a = A::One; let a = A::One;
@ -440,20 +438,14 @@ mod tests {
} }
"#, "#,
r#" r#"
enum A { enum A { One, Two }
One, enum B { One, Two }
Two,
}
enum B {
One,
Two,
}
fn main() { fn main() {
let a = A::One; let a = A::One;
let b = B::One; let b = B::One;
match <|>(&a, &b) { match (&a, &b) {
(A::One, B::One) => {} $0(A::One, B::One) => {}
(A::One, B::Two) => {} (A::One, B::Two) => {}
(A::Two, B::One) => {} (A::Two, B::One) => {}
(A::Two, B::Two) => {} (A::Two, B::Two) => {}
@ -468,14 +460,8 @@ mod tests {
check_assist_not_applicable( check_assist_not_applicable(
fill_match_arms, fill_match_arms,
r#" r#"
enum A { enum A { One, Two }
One, enum B { One, Two }
Two,
}
enum B {
One,
Two,
}
fn main() { fn main() {
let a = A::One; let a = A::One;
@ -493,14 +479,8 @@ mod tests {
check_assist_not_applicable( check_assist_not_applicable(
fill_match_arms, fill_match_arms,
r#" r#"
enum A { enum A { One, Two }
One, enum B { One, Two }
Two,
}
enum B {
One,
Two,
}
fn main() { fn main() {
let a = A::One; let a = A::One;
@ -524,10 +504,7 @@ mod tests {
check_assist_not_applicable( check_assist_not_applicable(
fill_match_arms, fill_match_arms,
r#" r#"
enum A { enum A { One, Two }
One,
Two,
}
fn main() { fn main() {
let a = A::One; let a = A::One;
@ -543,9 +520,7 @@ mod tests {
check_assist( check_assist(
fill_match_arms, fill_match_arms,
r#" r#"
enum A { enum A { As }
As,
}
fn foo(a: &A) { fn foo(a: &A) {
match a<|> { match a<|> {
@ -553,13 +528,11 @@ mod tests {
} }
"#, "#,
r#" r#"
enum A { enum A { As }
As,
}
fn foo(a: &A) { fn foo(a: &A) {
match <|>a { match a {
A::As => {} $0A::As => {}
} }
} }
"#, "#,
@ -569,7 +542,7 @@ mod tests {
fill_match_arms, fill_match_arms,
r#" r#"
enum A { enum A {
Es{ x: usize, y: usize } Es { x: usize, y: usize }
} }
fn foo(a: &mut A) { fn foo(a: &mut A) {
@ -579,12 +552,12 @@ mod tests {
"#, "#,
r#" r#"
enum A { enum A {
Es{ x: usize, y: usize } Es { x: usize, y: usize }
} }
fn foo(a: &mut A) { fn foo(a: &mut A) {
match <|>a { match a {
A::Es { x, y } => {} $0A::Es { x, y } => {}
} }
} }
"#, "#,
@ -623,8 +596,8 @@ mod tests {
enum E { X, Y } enum E { X, Y }
fn main() { fn main() {
match <|>E::X { match E::X {
E::X => {} $0E::X => {}
E::Y => {} E::Y => {}
} }
} }
@ -651,8 +624,8 @@ mod tests {
use foo::E::X; use foo::E::X;
fn main() { fn main() {
match <|>X { match X {
X => {} $0X => {}
foo::E::Y => {} foo::E::Y => {}
} }
} }
@ -665,10 +638,7 @@ mod tests {
check_assist( check_assist(
fill_match_arms, fill_match_arms,
r#" r#"
enum A { enum A { One, Two }
One,
Two,
}
fn foo(a: A) { fn foo(a: A) {
match a { match a {
// foo bar baz<|> // foo bar baz<|>
@ -678,16 +648,13 @@ mod tests {
} }
"#, "#,
r#" r#"
enum A { enum A { One, Two }
One,
Two,
}
fn foo(a: A) { fn foo(a: A) {
match <|>a { match a {
// foo bar baz // foo bar baz
A::One => {} A::One => {}
// This is where the rest should be // This is where the rest should be
A::Two => {} $0A::Two => {}
} }
} }
"#, "#,
@ -699,10 +666,7 @@ mod tests {
check_assist( check_assist(
fill_match_arms, fill_match_arms,
r#" r#"
enum A { enum A { One, Two }
One,
Two,
}
fn foo(a: A) { fn foo(a: A) {
match a { match a {
// foo bar baz<|> // foo bar baz<|>
@ -710,14 +674,11 @@ mod tests {
} }
"#, "#,
r#" r#"
enum A { enum A { One, Two }
One,
Two,
}
fn foo(a: A) { fn foo(a: A) {
match <|>a { match a {
// foo bar baz // foo bar baz
A::One => {} $0A::One => {}
A::Two => {} A::Two => {}
} }
} }
@ -740,8 +701,8 @@ mod tests {
r#" r#"
enum A { One, Two, } enum A { One, Two, }
fn foo(a: A) { fn foo(a: A) {
match <|>a { match a {
A::One => {} $0A::One => {}
A::Two => {} A::Two => {}
} }
} }
@ -765,8 +726,8 @@ fn foo(opt: Option<i32>) {
before, before,
r#" r#"
fn foo(opt: Option<i32>) { fn foo(opt: Option<i32>) {
match <|>opt { match opt {
Some(_) => {} $0Some(_) => {}
None => {} None => {}
} }
} }

View file

@ -336,7 +336,7 @@ enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) { fn handle(action: Action) {
match action { match action {
Action::Move { distance } => {} $0Action::Move { distance } => {}
Action::Stop => {} Action::Stop => {}
} }
} }

View file

@ -325,7 +325,7 @@ enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) { fn handle(action: Action) {
match action { match action {
Action::Move { distance } => {} $0Action::Move { distance } => {}
Action::Stop => {} Action::Stop => {}
} }
} }