From c446fd76a2a6191adce87b20707a37bd46cb85a9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 14:00:37 +0200 Subject: Snippetify fill_match_arms --- crates/ra_assists/src/handlers/fill_match_arms.rs | 193 +++++++++------------- 1 file changed, 77 insertions(+), 116 deletions(-) (limited to 'crates/ra_assists/src/handlers') diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index bbdcdc626..cc303285b 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs @@ -6,7 +6,10 @@ use ra_ide_db::RootDatabase; use ra_syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat}; 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 // @@ -27,7 +30,7 @@ use crate::{utils::FamousDefs, AssistContext, AssistId, Assists}; // // fn handle(action: Action) { // match action { -// Action::Move { distance } => {} +// $0Action::Move { distance } => {} // 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(); - acc.add(AssistId("fill_match_arms"), "Fill match arms", target, |edit| { - let new_arm_list = match_arm_list.remove_placeholder().append_arms(missing_arms); - edit.set_cursor(expr.syntax().text_range().start()); - edit.replace_ast(match_arm_list, new_arm_list); + acc.add(AssistId("fill_match_arms"), "Fill match arms", target, |builder| { + let new_arm_list = match_arm_list.remove_placeholder(); + let n_old_arms = new_arm_list.arms().count(); + 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#" enum A { As, - Bs{x:i32, y:Option}, + Bs { x: i32, y: Option }, Cs(i32, Option), } fn main() { match A::As<|> { - A::Bs{x,y:Some(_)} => {} + A::Bs { x, y: Some(_) } => {} A::Cs(_, Some(_)) => {} } } @@ -239,14 +255,14 @@ mod tests { r#" enum A { As, - Bs{x:i32, y:Option}, + Bs { x: i32, y: Option }, Cs(i32, Option), } fn main() { - match <|>A::As { - A::Bs{x,y:Some(_)} => {} + match A::As { + A::Bs { x, y: Some(_) } => {} A::Cs(_, Some(_)) => {} - A::As => {} + $0A::As => {} } } "#, @@ -276,9 +292,9 @@ mod tests { Cs(Option), } fn main() { - match <|>A::As { + match A::As { A::Cs(_) | A::Bs => {} - A::As => {} + $0A::As => {} } } "#, @@ -322,11 +338,11 @@ mod tests { Ys, } fn main() { - match <|>A::As { + match A::As { A::Bs if 0 < 1 => {} A::Ds(_value) => { let x = 1; } A::Es(B::Xs) => (), - A::As => {} + $0A::As => {} A::Cs => {} } } @@ -344,7 +360,7 @@ mod tests { Bs, Cs(String), Ds(String, String), - Es{ x: usize, y: usize } + Es { x: usize, y: usize } } fn main() { @@ -358,13 +374,13 @@ mod tests { Bs, Cs(String), Ds(String, String), - Es{ x: usize, y: usize } + Es { x: usize, y: usize } } fn main() { let a = A::As; - match <|>a { - A::As => {} + match a { + $0A::As => {} A::Bs => {} A::Cs(_) => {} A::Ds(_, _) => {} @@ -380,14 +396,8 @@ mod tests { check_assist( fill_match_arms, r#" - enum A { - One, - Two, - } - enum B { - One, - Two, - } + enum A { One, Two } + enum B { One, Two } fn main() { let a = A::One; @@ -396,20 +406,14 @@ mod tests { } "#, r#" - enum A { - One, - Two, - } - enum B { - One, - Two, - } + enum A { One, Two } + enum B { One, Two } fn main() { let a = A::One; let b = B::One; - match <|>(a, b) { - (A::One, B::One) => {} + match (a, b) { + $0(A::One, B::One) => {} (A::One, B::Two) => {} (A::Two, B::One) => {} (A::Two, B::Two) => {} @@ -424,14 +428,8 @@ mod tests { check_assist( fill_match_arms, r#" - enum A { - One, - Two, - } - enum B { - One, - Two, - } + enum A { One, Two } + enum B { One, Two } fn main() { let a = A::One; @@ -440,20 +438,14 @@ mod tests { } "#, r#" - enum A { - One, - Two, - } - enum B { - One, - Two, - } + enum A { One, Two } + enum B { One, Two } fn main() { let a = A::One; let b = B::One; - match <|>(&a, &b) { - (A::One, B::One) => {} + match (&a, &b) { + $0(A::One, B::One) => {} (A::One, B::Two) => {} (A::Two, B::One) => {} (A::Two, B::Two) => {} @@ -468,14 +460,8 @@ mod tests { check_assist_not_applicable( fill_match_arms, r#" - enum A { - One, - Two, - } - enum B { - One, - Two, - } + enum A { One, Two } + enum B { One, Two } fn main() { let a = A::One; @@ -493,14 +479,8 @@ mod tests { check_assist_not_applicable( fill_match_arms, r#" - enum A { - One, - Two, - } - enum B { - One, - Two, - } + enum A { One, Two } + enum B { One, Two } fn main() { let a = A::One; @@ -524,10 +504,7 @@ mod tests { check_assist_not_applicable( fill_match_arms, r#" - enum A { - One, - Two, - } + enum A { One, Two } fn main() { let a = A::One; @@ -543,9 +520,7 @@ mod tests { check_assist( fill_match_arms, r#" - enum A { - As, - } + enum A { As } fn foo(a: &A) { match a<|> { @@ -553,13 +528,11 @@ mod tests { } "#, r#" - enum A { - As, - } + enum A { As } fn foo(a: &A) { - match <|>a { - A::As => {} + match a { + $0A::As => {} } } "#, @@ -569,7 +542,7 @@ mod tests { fill_match_arms, r#" enum A { - Es{ x: usize, y: usize } + Es { x: usize, y: usize } } fn foo(a: &mut A) { @@ -579,12 +552,12 @@ mod tests { "#, r#" enum A { - Es{ x: usize, y: usize } + Es { x: usize, y: usize } } fn foo(a: &mut A) { - match <|>a { - A::Es { x, y } => {} + match a { + $0A::Es { x, y } => {} } } "#, @@ -623,8 +596,8 @@ mod tests { enum E { X, Y } fn main() { - match <|>E::X { - E::X => {} + match E::X { + $0E::X => {} E::Y => {} } } @@ -651,8 +624,8 @@ mod tests { use foo::E::X; fn main() { - match <|>X { - X => {} + match X { + $0X => {} foo::E::Y => {} } } @@ -665,10 +638,7 @@ mod tests { check_assist( fill_match_arms, r#" - enum A { - One, - Two, - } + enum A { One, Two } fn foo(a: A) { match a { // foo bar baz<|> @@ -678,16 +648,13 @@ mod tests { } "#, r#" - enum A { - One, - Two, - } + enum A { One, Two } fn foo(a: A) { - match <|>a { + match a { // foo bar baz A::One => {} // This is where the rest should be - A::Two => {} + $0A::Two => {} } } "#, @@ -699,10 +666,7 @@ mod tests { check_assist( fill_match_arms, r#" - enum A { - One, - Two, - } + enum A { One, Two } fn foo(a: A) { match a { // foo bar baz<|> @@ -710,14 +674,11 @@ mod tests { } "#, r#" - enum A { - One, - Two, - } + enum A { One, Two } fn foo(a: A) { - match <|>a { + match a { // foo bar baz - A::One => {} + $0A::One => {} A::Two => {} } } @@ -740,8 +701,8 @@ mod tests { r#" enum A { One, Two, } fn foo(a: A) { - match <|>a { - A::One => {} + match a { + $0A::One => {} A::Two => {} } } @@ -765,8 +726,8 @@ fn foo(opt: Option) { before, r#" fn foo(opt: Option) { - match <|>opt { - Some(_) => {} + match opt { + $0Some(_) => {} None => {} } } -- cgit v1.2.3