Skip to content

Commit efac093

Browse files
bors[bot]matklad
andauthored
Merge #4525
4525: Better cursor placement when adding impl members r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents b26dbf8 + 767d169 commit efac093

File tree

3 files changed

+50
-42
lines changed

3 files changed

+50
-42
lines changed

crates/ra_assists/src/handlers/add_missing_impl_members.rs

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ enum AddMissingImplMembersMode {
4545
// }
4646
//
4747
// impl Trait<u32> for () {
48-
// $0fn foo(&self) -> u32 {
49-
// todo!()
48+
// fn foo(&self) -> u32 {
49+
// ${0:todo!()}
5050
// }
5151
//
5252
// }
@@ -167,15 +167,23 @@ fn add_missing_impl_members_inner(
167167
let original_range = impl_item_list.syntax().text_range();
168168
match ctx.config.snippet_cap {
169169
None => builder.replace(original_range, new_impl_item_list.to_string()),
170-
Some(cap) => builder.replace_snippet(
171-
cap,
172-
original_range,
173-
render_snippet(
170+
Some(cap) => {
171+
let mut cursor = Cursor::Before(first_new_item.syntax());
172+
let placeholder;
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(
174182
cap,
175-
new_impl_item_list.syntax(),
176-
Cursor::Before(first_new_item.syntax()),
177-
),
178-
),
183+
original_range,
184+
render_snippet(cap, new_impl_item_list.syntax(), cursor),
185+
)
186+
}
179187
};
180188
})
181189
}
@@ -271,8 +279,8 @@ struct S;
271279
272280
impl Foo for S {
273281
fn bar(&self) {}
274-
$0fn foo(&self) {
275-
todo!()
282+
fn foo(&self) {
283+
${0:todo!()}
276284
}
277285
278286
}"#,
@@ -291,8 +299,8 @@ impl Foo for S { <|> }"#,
291299
trait Foo { fn foo(&self); }
292300
struct S;
293301
impl Foo for S {
294-
$0fn foo(&self) {
295-
todo!()
302+
fn foo(&self) {
303+
${0:todo!()}
296304
}
297305
}"#,
298306
);
@@ -310,8 +318,8 @@ impl Foo<u32> for S { <|> }"#,
310318
trait Foo<T> { fn foo(&self, t: T) -> &T; }
311319
struct S;
312320
impl Foo<u32> for S {
313-
$0fn foo(&self, t: u32) -> &u32 {
314-
todo!()
321+
fn foo(&self, t: u32) -> &u32 {
322+
${0:todo!()}
315323
}
316324
}"#,
317325
);
@@ -329,8 +337,8 @@ impl<U> Foo<U> for S { <|> }"#,
329337
trait Foo<T> { fn foo(&self, t: T) -> &T; }
330338
struct S;
331339
impl<U> Foo<U> for S {
332-
$0fn foo(&self, t: U) -> &U {
333-
todo!()
340+
fn foo(&self, t: U) -> &U {
341+
${0:todo!()}
334342
}
335343
}"#,
336344
);
@@ -348,8 +356,8 @@ impl Foo for S {}<|>"#,
348356
trait Foo { fn foo(&self); }
349357
struct S;
350358
impl Foo for S {
351-
$0fn foo(&self) {
352-
todo!()
359+
fn foo(&self) {
360+
${0:todo!()}
353361
}
354362
}"#,
355363
)
@@ -373,8 +381,8 @@ mod foo {
373381
}
374382
struct S;
375383
impl foo::Foo for S {
376-
$0fn foo(&self, bar: foo::Bar) {
377-
todo!()
384+
fn foo(&self, bar: foo::Bar) {
385+
${0:todo!()}
378386
}
379387
}"#,
380388
);
@@ -398,8 +406,8 @@ mod foo {
398406
}
399407
struct S;
400408
impl foo::Foo for S {
401-
$0fn foo(&self, bar: foo::Bar<u32>) {
402-
todo!()
409+
fn foo(&self, bar: foo::Bar<u32>) {
410+
${0:todo!()}
403411
}
404412
}"#,
405413
);
@@ -423,8 +431,8 @@ mod foo {
423431
}
424432
struct S;
425433
impl foo::Foo<u32> for S {
426-
$0fn foo(&self, bar: foo::Bar<u32>) {
427-
todo!()
434+
fn foo(&self, bar: foo::Bar<u32>) {
435+
${0:todo!()}
428436
}
429437
}"#,
430438
);
@@ -451,8 +459,8 @@ mod foo {
451459
struct Param;
452460
struct S;
453461
impl foo::Foo<Param> for S {
454-
$0fn foo(&self, bar: Param) {
455-
todo!()
462+
fn foo(&self, bar: Param) {
463+
${0:todo!()}
456464
}
457465
}"#,
458466
);
@@ -478,8 +486,8 @@ mod foo {
478486
}
479487
struct S;
480488
impl foo::Foo for S {
481-
$0fn foo(&self, bar: foo::Bar<u32>::Assoc) {
482-
todo!()
489+
fn foo(&self, bar: foo::Bar<u32>::Assoc) {
490+
${0:todo!()}
483491
}
484492
}"#,
485493
);
@@ -505,8 +513,8 @@ mod foo {
505513
}
506514
struct S;
507515
impl foo::Foo for S {
508-
$0fn foo(&self, bar: foo::Bar<foo::Baz>) {
509-
todo!()
516+
fn foo(&self, bar: foo::Bar<foo::Baz>) {
517+
${0:todo!()}
510518
}
511519
}"#,
512520
);
@@ -530,8 +538,8 @@ mod foo {
530538
}
531539
struct S;
532540
impl foo::Foo for S {
533-
$0fn foo(&self, bar: dyn Fn(u32) -> i32) {
534-
todo!()
541+
fn foo(&self, bar: dyn Fn(u32) -> i32) {
542+
${0:todo!()}
535543
}
536544
}"#,
537545
);
@@ -645,8 +653,8 @@ trait Foo<T = Self> {
645653
646654
struct S;
647655
impl Foo for S {
648-
$0fn bar(&self, other: &Self) {
649-
todo!()
656+
fn bar(&self, other: &Self) {
657+
${0:todo!()}
650658
}
651659
}"#,
652660
)
@@ -670,8 +678,8 @@ trait Foo<T1, T2 = Self> {
670678
671679
struct S<T>;
672680
impl Foo<T> for S<T> {
673-
$0fn bar(&self, this: &T, that: &Self) {
674-
todo!()
681+
fn bar(&self, this: &T, that: &Self) {
682+
${0:todo!()}
675683
}
676684
}"#,
677685
)

crates/ra_assists/src/tests/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ trait Trait<T> {
180180
}
181181
182182
impl Trait<u32> for () {
183-
$0fn foo(&self) -> u32 {
184-
todo!()
183+
fn foo(&self) -> u32 {
184+
${0:todo!()}
185185
}
186186
187187
}

docs/user/assists.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ trait Trait<T> {
175175
}
176176

177177
impl Trait<u32> for () {
178-
$0fn foo(&self) -> u32 {
179-
todo!()
178+
fn foo(&self) -> u32 {
179+
${0:todo!()}
180180
}
181181

182182
}

0 commit comments

Comments
 (0)