Skip to content

Commit 6b8e927

Browse files
committed
Change tabstop to method tail_expr
1 parent 0b9114f commit 6b8e927

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

crates/ide-assists/src/handlers/generate_impl.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
141141
// }
142142
//
143143
// impl Foo for ${1:_} {
144-
// $0fn foo(&self) -> i32 {
145-
// todo!()
144+
// fn foo(&self) -> i32 {
145+
// $0todo!()
146146
// }
147147
// }
148148
// ```
@@ -206,8 +206,10 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
206206
edit.add_placeholder_snippet(cap, ty);
207207
}
208208

209-
if let Some(item) = impl_.assoc_item_list().and_then(|it| it.assoc_items().next()) {
210-
edit.add_tabstop_before(cap, item);
209+
if let Some(expr) =
210+
impl_.assoc_item_list().and_then(|it| it.assoc_items().find_map(extract_expr))
211+
{
212+
edit.add_tabstop_before(cap, expr);
211213
} else if let Some(l_curly) =
212214
impl_.assoc_item_list().and_then(|it| it.l_curly_token())
213215
{
@@ -220,6 +222,13 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
220222
)
221223
}
222224

225+
fn extract_expr(item: ast::AssocItem) -> Option<ast::Expr> {
226+
let ast::AssocItem::Fn(f) = item else {
227+
return None;
228+
};
229+
f.body()?.tail_expr()
230+
}
231+
223232
#[cfg(test)]
224233
mod tests {
225234
use crate::tests::{check_assist, check_assist_target};
@@ -616,8 +625,8 @@ mod tests {
616625
}
617626
618627
impl Foo for ${1:_} {
619-
$0fn foo(&self) -> i32 {
620-
todo!()
628+
fn foo(&self) -> i32 {
629+
$0todo!()
621630
}
622631
}
623632
"#,
@@ -647,8 +656,8 @@ mod tests {
647656
}
648657
649658
impl Foo<${1:_}> for ${2:_} {
650-
$0fn foo(&self) -> _ {
651-
todo!()
659+
fn foo(&self) -> _ {
660+
$0todo!()
652661
}
653662
}
654663
"#,
@@ -674,8 +683,8 @@ mod tests {
674683
}
675684
676685
impl Foo<${1:_}, ${2:_}> for ${3:_} {
677-
$0fn foo(&self) -> _ {
678-
todo!()
686+
fn foo(&self) -> _ {
687+
$0todo!()
679688
}
680689
}
681690
"#,
@@ -709,8 +718,8 @@ mod tests {
709718
}
710719
711720
impl Foo for ${1:_} {
712-
$0fn foo(&self) -> i32 {
713-
todo!()
721+
fn foo(&self) -> i32 {
722+
$0todo!()
714723
}
715724
}
716725
"#,
@@ -736,10 +745,10 @@ mod tests {
736745
}
737746
738747
impl Foo for ${1:_} {
739-
$0type Output;
748+
type Output;
740749
741750
fn foo(&self) -> Self::Output {
742-
todo!()
751+
$0todo!()
743752
}
744753
}
745754
"#,

crates/ide-assists/src/tests/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,8 +1895,8 @@ trait Foo {
18951895
}
18961896
18971897
impl Foo for ${1:_} {
1898-
$0fn foo(&self) -> i32 {
1899-
todo!()
1898+
fn foo(&self) -> i32 {
1899+
$0todo!()
19001900
}
19011901
}
19021902
"#####,

0 commit comments

Comments
 (0)