aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-20 01:07:21 +0100
committerAleksey Kladov <[email protected]>2020-05-20 01:07:21 +0100
commit767d169a2ae543f28544e85e15bac1b6aa1cab23 (patch)
tree1a2d5c3c5ccd2abcccb0cae2ca2852790f350bff
parenta04cababaa144d7a6db7b1dd114494b33d281ab9 (diff)
Better cursor placement when adding impl members
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs84
-rw-r--r--crates/ra_assists/src/tests/generated.rs4
-rw-r--r--docs/user/assists.md4
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 {
45// } 45// }
46// 46//
47// impl Trait<u32> for () { 47// impl Trait<u32> for () {
48// $0fn foo(&self) -> u32 { 48// fn foo(&self) -> u32 {
49// todo!() 49// ${0:todo!()}
50// } 50// }
51// 51//
52// } 52// }
@@ -167,15 +167,23 @@ fn add_missing_impl_members_inner(
167 let original_range = impl_item_list.syntax().text_range(); 167 let original_range = impl_item_list.syntax().text_range();
168 match ctx.config.snippet_cap { 168 match ctx.config.snippet_cap {
169 None => builder.replace(original_range, new_impl_item_list.to_string()), 169 None => builder.replace(original_range, new_impl_item_list.to_string()),
170 Some(cap) => builder.replace_snippet( 170 Some(cap) => {
171 cap, 171 let mut cursor = Cursor::Before(first_new_item.syntax());
172 original_range, 172 let placeholder;
173 render_snippet( 173 if let ast::AssocItem::FnDef(func) = &first_new_item {
174 if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast) {
175 if m.syntax().text() == "todo!()" {
176 placeholder = m;
177 cursor = Cursor::Replace(placeholder.syntax());
178 }
179 }
180 }
181 builder.replace_snippet(
174 cap, 182 cap,
175 new_impl_item_list.syntax(), 183 original_range,
176 Cursor::Before(first_new_item.syntax()), 184 render_snippet(cap, new_impl_item_list.syntax(), cursor),
177 ), 185 )
178 ), 186 }
179 }; 187 };
180 }) 188 })
181} 189}
@@ -271,8 +279,8 @@ struct S;
271 279
272impl Foo for S { 280impl Foo for S {
273 fn bar(&self) {} 281 fn bar(&self) {}
274 $0fn foo(&self) { 282 fn foo(&self) {
275 todo!() 283 ${0:todo!()}
276 } 284 }
277 285
278}"#, 286}"#,
@@ -291,8 +299,8 @@ impl Foo for S { <|> }"#,
291trait Foo { fn foo(&self); } 299trait Foo { fn foo(&self); }
292struct S; 300struct S;
293impl Foo for S { 301impl Foo for S {
294 $0fn foo(&self) { 302 fn foo(&self) {
295 todo!() 303 ${0:todo!()}
296 } 304 }
297}"#, 305}"#,
298 ); 306 );
@@ -310,8 +318,8 @@ impl Foo<u32> for S { <|> }"#,
310trait Foo<T> { fn foo(&self, t: T) -> &T; } 318trait Foo<T> { fn foo(&self, t: T) -> &T; }
311struct S; 319struct S;
312impl Foo<u32> for S { 320impl Foo<u32> for S {
313 $0fn foo(&self, t: u32) -> &u32 { 321 fn foo(&self, t: u32) -> &u32 {
314 todo!() 322 ${0:todo!()}
315 } 323 }
316}"#, 324}"#,
317 ); 325 );
@@ -329,8 +337,8 @@ impl<U> Foo<U> for S { <|> }"#,
329trait Foo<T> { fn foo(&self, t: T) -> &T; } 337trait Foo<T> { fn foo(&self, t: T) -> &T; }
330struct S; 338struct S;
331impl<U> Foo<U> for S { 339impl<U> Foo<U> for S {
332 $0fn foo(&self, t: U) -> &U { 340 fn foo(&self, t: U) -> &U {
333 todo!() 341 ${0:todo!()}
334 } 342 }
335}"#, 343}"#,
336 ); 344 );
@@ -348,8 +356,8 @@ impl Foo for S {}<|>"#,
348trait Foo { fn foo(&self); } 356trait Foo { fn foo(&self); }
349struct S; 357struct S;
350impl Foo for S { 358impl Foo for S {
351 $0fn foo(&self) { 359 fn foo(&self) {
352 todo!() 360 ${0:todo!()}
353 } 361 }
354}"#, 362}"#,
355 ) 363 )
@@ -373,8 +381,8 @@ mod foo {
373} 381}
374struct S; 382struct S;
375impl foo::Foo for S { 383impl foo::Foo for S {
376 $0fn foo(&self, bar: foo::Bar) { 384 fn foo(&self, bar: foo::Bar) {
377 todo!() 385 ${0:todo!()}
378 } 386 }
379}"#, 387}"#,
380 ); 388 );
@@ -398,8 +406,8 @@ mod foo {
398} 406}
399struct S; 407struct S;
400impl foo::Foo for S { 408impl foo::Foo for S {
401 $0fn foo(&self, bar: foo::Bar<u32>) { 409 fn foo(&self, bar: foo::Bar<u32>) {
402 todo!() 410 ${0:todo!()}
403 } 411 }
404}"#, 412}"#,
405 ); 413 );
@@ -423,8 +431,8 @@ mod foo {
423} 431}
424struct S; 432struct S;
425impl foo::Foo<u32> for S { 433impl foo::Foo<u32> for S {
426 $0fn foo(&self, bar: foo::Bar<u32>) { 434 fn foo(&self, bar: foo::Bar<u32>) {
427 todo!() 435 ${0:todo!()}
428 } 436 }
429}"#, 437}"#,
430 ); 438 );
@@ -451,8 +459,8 @@ mod foo {
451struct Param; 459struct Param;
452struct S; 460struct S;
453impl foo::Foo<Param> for S { 461impl foo::Foo<Param> for S {
454 $0fn foo(&self, bar: Param) { 462 fn foo(&self, bar: Param) {
455 todo!() 463 ${0:todo!()}
456 } 464 }
457}"#, 465}"#,
458 ); 466 );
@@ -478,8 +486,8 @@ mod foo {
478} 486}
479struct S; 487struct S;
480impl foo::Foo for S { 488impl foo::Foo for S {
481 $0fn foo(&self, bar: foo::Bar<u32>::Assoc) { 489 fn foo(&self, bar: foo::Bar<u32>::Assoc) {
482 todo!() 490 ${0:todo!()}
483 } 491 }
484}"#, 492}"#,
485 ); 493 );
@@ -505,8 +513,8 @@ mod foo {
505} 513}
506struct S; 514struct S;
507impl foo::Foo for S { 515impl foo::Foo for S {
508 $0fn foo(&self, bar: foo::Bar<foo::Baz>) { 516 fn foo(&self, bar: foo::Bar<foo::Baz>) {
509 todo!() 517 ${0:todo!()}
510 } 518 }
511}"#, 519}"#,
512 ); 520 );
@@ -530,8 +538,8 @@ mod foo {
530} 538}
531struct S; 539struct S;
532impl foo::Foo for S { 540impl foo::Foo for S {
533 $0fn foo(&self, bar: dyn Fn(u32) -> i32) { 541 fn foo(&self, bar: dyn Fn(u32) -> i32) {
534 todo!() 542 ${0:todo!()}
535 } 543 }
536}"#, 544}"#,
537 ); 545 );
@@ -645,8 +653,8 @@ trait Foo<T = Self> {
645 653
646struct S; 654struct S;
647impl Foo for S { 655impl Foo for S {
648 $0fn bar(&self, other: &Self) { 656 fn bar(&self, other: &Self) {
649 todo!() 657 ${0:todo!()}
650 } 658 }
651}"#, 659}"#,
652 ) 660 )
@@ -670,8 +678,8 @@ trait Foo<T1, T2 = Self> {
670 678
671struct S<T>; 679struct S<T>;
672impl Foo<T> for S<T> { 680impl Foo<T> for S<T> {
673 $0fn bar(&self, this: &T, that: &Self) { 681 fn bar(&self, this: &T, that: &Self) {
674 todo!() 682 ${0:todo!()}
675 } 683 }
676}"#, 684}"#,
677 ) 685 )
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<T> {
180} 180}
181 181
182impl Trait<u32> for () { 182impl Trait<u32> for () {
183 $0fn foo(&self) -> u32 { 183 fn foo(&self) -> u32 {
184 todo!() 184 ${0:todo!()}
185 } 185 }
186 186
187} 187}
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<T> {
175} 175}
176 176
177impl Trait<u32> for () { 177impl Trait<u32> for () {
178 $0fn foo(&self) -> u32 { 178 fn foo(&self) -> u32 {
179 todo!() 179 ${0:todo!()}
180 } 180 }
181 181
182} 182}