From 767d169a2ae543f28544e85e15bac1b6aa1cab23 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 02:07:21 +0200 Subject: Better cursor placement when adding impl members --- .../src/handlers/add_missing_impl_members.rs | 84 ++++++++++++---------- crates/ra_assists/src/tests/generated.rs | 4 +- docs/user/assists.md | 4 +- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs index d7aa06947..abacd4065 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -45,8 +45,8 @@ enum AddMissingImplMembersMode { // } // // impl Trait for () { -// $0fn foo(&self) -> u32 { -// todo!() +// fn foo(&self) -> u32 { +// ${0:todo!()} // } // // } @@ -167,15 +167,23 @@ fn add_missing_impl_members_inner( let original_range = impl_item_list.syntax().text_range(); match ctx.config.snippet_cap { None => builder.replace(original_range, new_impl_item_list.to_string()), - Some(cap) => builder.replace_snippet( - cap, - original_range, - render_snippet( + Some(cap) => { + let mut cursor = Cursor::Before(first_new_item.syntax()); + let placeholder; + if let ast::AssocItem::FnDef(func) = &first_new_item { + if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast) { + if m.syntax().text() == "todo!()" { + placeholder = m; + cursor = Cursor::Replace(placeholder.syntax()); + } + } + } + builder.replace_snippet( cap, - new_impl_item_list.syntax(), - Cursor::Before(first_new_item.syntax()), - ), - ), + original_range, + render_snippet(cap, new_impl_item_list.syntax(), cursor), + ) + } }; }) } @@ -271,8 +279,8 @@ struct S; impl Foo for S { fn bar(&self) {} - $0fn foo(&self) { - todo!() + fn foo(&self) { + ${0:todo!()} } }"#, @@ -291,8 +299,8 @@ impl Foo for S { <|> }"#, trait Foo { fn foo(&self); } struct S; impl Foo for S { - $0fn foo(&self) { - todo!() + fn foo(&self) { + ${0:todo!()} } }"#, ); @@ -310,8 +318,8 @@ impl Foo for S { <|> }"#, trait Foo { fn foo(&self, t: T) -> &T; } struct S; impl Foo for S { - $0fn foo(&self, t: u32) -> &u32 { - todo!() + fn foo(&self, t: u32) -> &u32 { + ${0:todo!()} } }"#, ); @@ -329,8 +337,8 @@ impl Foo for S { <|> }"#, trait Foo { fn foo(&self, t: T) -> &T; } struct S; impl Foo for S { - $0fn foo(&self, t: U) -> &U { - todo!() + fn foo(&self, t: U) -> &U { + ${0:todo!()} } }"#, ); @@ -348,8 +356,8 @@ impl Foo for S {}<|>"#, trait Foo { fn foo(&self); } struct S; impl Foo for S { - $0fn foo(&self) { - todo!() + fn foo(&self) { + ${0:todo!()} } }"#, ) @@ -373,8 +381,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar) { - todo!() + fn foo(&self, bar: foo::Bar) { + ${0:todo!()} } }"#, ); @@ -398,8 +406,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar) { - todo!() + fn foo(&self, bar: foo::Bar) { + ${0:todo!()} } }"#, ); @@ -423,8 +431,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar) { - todo!() + fn foo(&self, bar: foo::Bar) { + ${0:todo!()} } }"#, ); @@ -451,8 +459,8 @@ mod foo { struct Param; struct S; impl foo::Foo for S { - $0fn foo(&self, bar: Param) { - todo!() + fn foo(&self, bar: Param) { + ${0:todo!()} } }"#, ); @@ -478,8 +486,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar::Assoc) { - todo!() + fn foo(&self, bar: foo::Bar::Assoc) { + ${0:todo!()} } }"#, ); @@ -505,8 +513,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar) { - todo!() + fn foo(&self, bar: foo::Bar) { + ${0:todo!()} } }"#, ); @@ -530,8 +538,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: dyn Fn(u32) -> i32) { - todo!() + fn foo(&self, bar: dyn Fn(u32) -> i32) { + ${0:todo!()} } }"#, ); @@ -645,8 +653,8 @@ trait Foo { struct S; impl Foo for S { - $0fn bar(&self, other: &Self) { - todo!() + fn bar(&self, other: &Self) { + ${0:todo!()} } }"#, ) @@ -670,8 +678,8 @@ trait Foo { struct S; impl Foo for S { - $0fn bar(&self, this: &T, that: &Self) { - todo!() + fn bar(&self, this: &T, that: &Self) { + ${0:todo!()} } }"#, ) diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 2522ec5df..3808aded1 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs @@ -180,8 +180,8 @@ trait Trait { } impl Trait for () { - $0fn foo(&self) -> u32 { - todo!() + fn foo(&self) -> u32 { + ${0:todo!()} } } diff --git a/docs/user/assists.md b/docs/user/assists.md index b687330f3..f329fcc10 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -175,8 +175,8 @@ trait Trait { } impl Trait for () { - $0fn foo(&self) -> u32 { - todo!() + fn foo(&self) -> u32 { + ${0:todo!()} } } -- cgit v1.2.3