From 74da16f6f95aec839842d1f920fec9e40b6f80a4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 13:41:58 +0200 Subject: Cleanup imports --- crates/ra_assists/src/handlers/unwrap_block.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index e52ec557e..b76182d79 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs @@ -1,8 +1,10 @@ -use crate::{AssistContext, AssistId, Assists}; - -use ast::{ElseBranch, Expr, LoopBodyOwner}; use ra_fmt::unwrap_trivial_block; -use ra_syntax::{ast, match_ast, AstNode, TextRange, T}; +use ra_syntax::{ + ast::{self, ElseBranch, Expr, LoopBodyOwner}, + match_ast, AstNode, TextRange, T, +}; + +use crate::{AssistContext, AssistId, Assists}; // Assist: unwrap_block // -- cgit v1.2.3 From a622b54ac029994328a99e30ee3169c21de498b7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 13:48:31 +0200 Subject: Don't set cursor in change_visibility --- crates/ra_assists/src/handlers/change_visibility.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs index 1d9b8e645..fbe459c9c 100644 --- a/crates/ra_assists/src/handlers/change_visibility.rs +++ b/crates/ra_assists/src/handlers/change_visibility.rs @@ -68,7 +68,6 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { acc.add(AssistId("change_visibility"), "Change visibility to pub(crate)", target, |edit| { edit.insert(offset, "pub(crate) "); - edit.set_cursor(offset); }) } @@ -92,7 +91,6 @@ fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> { target, |edit| { edit.replace(vis.syntax().text_range(), "pub(crate)"); - edit.set_cursor(vis.syntax().text_range().start()) }, ); } @@ -104,7 +102,6 @@ fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> { target, |edit| { edit.replace(vis.syntax().text_range(), "pub"); - edit.set_cursor(vis.syntax().text_range().start()); }, ); } @@ -122,15 +119,15 @@ mod tests { #[test] fn change_visibility_adds_pub_crate_to_items() { check_assist(change_visibility, "<|>fn foo() {}", "<|>pub(crate) fn foo() {}"); - check_assist(change_visibility, "f<|>n foo() {}", "<|>pub(crate) fn foo() {}"); + check_assist(change_visibility, "f<|>n foo() {}", "pub(crate) f<|>n foo() {}"); check_assist(change_visibility, "<|>struct Foo {}", "<|>pub(crate) struct Foo {}"); check_assist(change_visibility, "<|>mod foo {}", "<|>pub(crate) mod foo {}"); check_assist(change_visibility, "<|>trait Foo {}", "<|>pub(crate) trait Foo {}"); - check_assist(change_visibility, "m<|>od {}", "<|>pub(crate) mod {}"); + check_assist(change_visibility, "m<|>od {}", "pub(crate) m<|>od {}"); check_assist( change_visibility, "unsafe f<|>n foo() {}", - "<|>pub(crate) unsafe fn foo() {}", + "pub(crate) unsafe f<|>n foo() {}", ); } -- cgit v1.2.3 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 +++++++++------------- crates/ra_assists/src/tests/generated.rs | 2 +- docs/user/assists.md | 2 +- 3 files changed, 79 insertions(+), 118 deletions(-) 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 => {} } } diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 417ee89a8..d841b6f56 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs @@ -336,7 +336,7 @@ enum Action { Move { distance: u32 }, Stop } fn handle(action: Action) { match action { - Action::Move { distance } => {} + $0Action::Move { distance } => {} Action::Stop => {} } } diff --git a/docs/user/assists.md b/docs/user/assists.md index 0ae242389..a33c490b8 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -325,7 +325,7 @@ enum Action { Move { distance: u32 }, Stop } fn handle(action: Action) { match action { - Action::Move { distance } => {} + $0Action::Move { distance } => {} Action::Stop => {} } } -- cgit v1.2.3 From ba3a58d1b2d76bae2ac84923d12918a32ad680f6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 14:13:17 +0200 Subject: Snippetify fix_visibility --- crates/ra_assists/src/handlers/fix_visibility.rs | 64 +++++++++++++----------- crates/ra_assists/src/tests/generated.rs | 2 +- docs/user/assists.md | 2 +- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs index 48ce07ca5..9ec42f568 100644 --- a/crates/ra_assists/src/handlers/fix_visibility.rs +++ b/crates/ra_assists/src/handlers/fix_visibility.rs @@ -25,7 +25,7 @@ use crate::{AssistContext, AssistId, Assists}; // -> // ``` // mod m { -// pub(crate) fn frobnicate() {} +// $0pub(crate) fn frobnicate() {} // } // fn main() { // m::frobnicate() {} @@ -62,10 +62,12 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext) -> O Some(name) => format!("Change visibility of {} to {}", name, missing_visibility), }; - acc.add(AssistId("fix_visibility"), assist_label, target, |edit| { - edit.set_file(target_file); - edit.insert(offset, format!("{} ", missing_visibility)); - edit.set_cursor(offset); + acc.add(AssistId("fix_visibility"), assist_label, target, |builder| { + builder.set_file(target_file); + match ctx.config.snippet_cap { + Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)), + None => builder.insert(offset, format!("{} ", missing_visibility)), + } }) } @@ -103,10 +105,12 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) -> let assist_label = format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility); - acc.add(AssistId("fix_visibility"), assist_label, target, |edit| { - edit.set_file(target_file); - edit.insert(offset, format!("{} ", missing_visibility)); - edit.set_cursor(offset) + acc.add(AssistId("fix_visibility"), assist_label, target, |builder| { + builder.set_file(target_file); + match ctx.config.snippet_cap { + Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)), + None => builder.insert(offset, format!("{} ", missing_visibility)), + } }) } @@ -196,7 +200,7 @@ mod tests { fix_visibility, r"mod foo { fn foo() {} } fn main() { foo::foo<|>() } ", - r"mod foo { <|>pub(crate) fn foo() {} } + r"mod foo { $0pub(crate) fn foo() {} } fn main() { foo::foo() } ", ); check_assist_not_applicable( @@ -212,7 +216,7 @@ mod tests { fix_visibility, r"mod foo { struct Foo; } fn main() { foo::Foo<|> } ", - r"mod foo { <|>pub(crate) struct Foo; } + r"mod foo { $0pub(crate) struct Foo; } fn main() { foo::Foo } ", ); check_assist_not_applicable( @@ -224,7 +228,7 @@ mod tests { fix_visibility, r"mod foo { enum Foo; } fn main() { foo::Foo<|> } ", - r"mod foo { <|>pub(crate) enum Foo; } + r"mod foo { $0pub(crate) enum Foo; } fn main() { foo::Foo } ", ); check_assist_not_applicable( @@ -236,7 +240,7 @@ mod tests { fix_visibility, r"mod foo { union Foo; } fn main() { foo::Foo<|> } ", - r"mod foo { <|>pub(crate) union Foo; } + r"mod foo { $0pub(crate) union Foo; } fn main() { foo::Foo } ", ); check_assist_not_applicable( @@ -258,7 +262,7 @@ mod tests { //- /foo.rs struct Foo; ", - r"<|>pub(crate) struct Foo; + r"$0pub(crate) struct Foo; ", ); @@ -270,7 +274,7 @@ mod tests { fix_visibility, r"mod foo { pub struct Foo { bar: (), } } fn main() { foo::Foo { <|>bar: () }; } ", - r"mod foo { pub struct Foo { <|>pub(crate) bar: (), } } + r"mod foo { pub struct Foo { $0pub(crate) bar: (), } } fn main() { foo::Foo { bar: () }; } ", ); check_assist( @@ -281,7 +285,7 @@ mod tests { //- /foo.rs pub struct Foo { bar: () } ", - r"pub struct Foo { <|>pub(crate) bar: () } + r"pub struct Foo { $0pub(crate) bar: () } ", ); @@ -307,7 +311,7 @@ mod tests { fix_visibility, r"mod foo { pub enum Foo { Bar { bar: () } } } fn main() { foo::Foo::Bar { <|>bar: () }; } ", - r"mod foo { pub enum Foo { Bar { <|>pub(crate) bar: () } } } + r"mod foo { pub enum Foo { Bar { $0pub(crate) bar: () } } } fn main() { foo::Foo::Bar { bar: () }; } ", ); check_assist( @@ -318,7 +322,7 @@ mod tests { //- /foo.rs pub enum Foo { Bar { bar: () } } ", - r"pub enum Foo { Bar { <|>pub(crate) bar: () } } + r"pub enum Foo { Bar { $0pub(crate) bar: () } } ", ); @@ -346,7 +350,7 @@ mod tests { fix_visibility, r"mod foo { pub union Foo { bar: (), } } fn main() { foo::Foo { <|>bar: () }; } ", - r"mod foo { pub union Foo { <|>pub(crate) bar: (), } } + r"mod foo { pub union Foo { $0pub(crate) bar: (), } } fn main() { foo::Foo { bar: () }; } ", ); check_assist( @@ -357,7 +361,7 @@ mod tests { //- /foo.rs pub union Foo { bar: () } ", - r"pub union Foo { <|>pub(crate) bar: () } + r"pub union Foo { $0pub(crate) bar: () } ", ); @@ -383,7 +387,7 @@ mod tests { fix_visibility, r"mod foo { const FOO: () = (); } fn main() { foo::FOO<|> } ", - r"mod foo { <|>pub(crate) const FOO: () = (); } + r"mod foo { $0pub(crate) const FOO: () = (); } fn main() { foo::FOO } ", ); check_assist_not_applicable( @@ -399,7 +403,7 @@ mod tests { fix_visibility, r"mod foo { static FOO: () = (); } fn main() { foo::FOO<|> } ", - r"mod foo { <|>pub(crate) static FOO: () = (); } + r"mod foo { $0pub(crate) static FOO: () = (); } fn main() { foo::FOO } ", ); check_assist_not_applicable( @@ -415,7 +419,7 @@ mod tests { fix_visibility, r"mod foo { trait Foo { fn foo(&self) {} } } fn main() { let x: &dyn foo::<|>Foo; } ", - r"mod foo { <|>pub(crate) trait Foo { fn foo(&self) {} } } + r"mod foo { $0pub(crate) trait Foo { fn foo(&self) {} } } fn main() { let x: &dyn foo::Foo; } ", ); check_assist_not_applicable( @@ -431,7 +435,7 @@ mod tests { fix_visibility, r"mod foo { type Foo = (); } fn main() { let x: foo::Foo<|>; } ", - r"mod foo { <|>pub(crate) type Foo = (); } + r"mod foo { $0pub(crate) type Foo = (); } fn main() { let x: foo::Foo; } ", ); check_assist_not_applicable( @@ -447,7 +451,7 @@ mod tests { fix_visibility, r"mod foo { mod bar { fn bar() {} } } fn main() { foo::bar<|>::bar(); } ", - r"mod foo { <|>pub(crate) mod bar { fn bar() {} } } + r"mod foo { $0pub(crate) mod bar { fn bar() {} } } fn main() { foo::bar::bar(); } ", ); @@ -463,7 +467,7 @@ mod tests { pub fn baz() {} } ", - r"<|>pub(crate) mod bar { + r"$0pub(crate) mod bar { pub fn baz() {} } @@ -493,7 +497,7 @@ mod tests { pub fn baz() {} } ", - r"<|>pub(crate) mod bar; + r"$0pub(crate) mod bar; ", ); } @@ -510,7 +514,7 @@ mod tests { mod bar { pub fn baz() {} }", - r"<|>pub(crate) mod bar { + r"$0pub(crate) mod bar { pub fn baz() {} } ", @@ -525,7 +529,7 @@ mod tests { foo::Bar<|> //- /lib.rs crate:foo struct Bar;", - r"<|>pub struct Bar; + r"$0pub struct Bar; ", ) } @@ -545,7 +549,7 @@ mod tests { ", r" mod foo { - <|>pub(crate) use bar::Baz; + $0pub(crate) use bar::Baz; mod bar { pub(super) struct Baz; } } foo::Baz diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index d841b6f56..3e6654c17 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs @@ -358,7 +358,7 @@ fn main() { "#####, r#####" mod m { - pub(crate) fn frobnicate() {} + $0pub(crate) fn frobnicate() {} } fn main() { m::frobnicate() {} diff --git a/docs/user/assists.md b/docs/user/assists.md index a33c490b8..51807ffda 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -346,7 +346,7 @@ fn main() { // AFTER mod m { - pub(crate) fn frobnicate() {} + $0pub(crate) fn frobnicate() {} } fn main() { m::frobnicate() {} -- cgit v1.2.3