Skip to content

Commit 253bd3d

Browse files
committed
Change tabstop to method tail_expr
1 parent 070f91e commit 253bd3d

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
@@ -134,8 +134,8 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
134134
// }
135135
//
136136
// impl Foo for ${1:_} {
137-
// $0fn foo(&self) -> i32 {
138-
// todo!()
137+
// fn foo(&self) -> i32 {
138+
// $0todo!()
139139
// }
140140
// }
141141
// ```
@@ -199,8 +199,10 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
199199
edit.add_placeholder_snippet(cap, ty);
200200
}
201201

202-
if let Some(item) = impl_.assoc_item_list().and_then(|it| it.assoc_items().next()) {
203-
edit.add_tabstop_before(cap, item);
202+
if let Some(expr) =
203+
impl_.assoc_item_list().and_then(|it| it.assoc_items().find_map(extract_expr))
204+
{
205+
edit.add_tabstop_before(cap, expr);
204206
} else if let Some(l_curly) =
205207
impl_.assoc_item_list().and_then(|it| it.l_curly_token())
206208
{
@@ -213,6 +215,13 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
213215
)
214216
}
215217

218+
fn extract_expr(item: ast::AssocItem) -> Option<ast::Expr> {
219+
let ast::AssocItem::Fn(f) = item else {
220+
return None;
221+
};
222+
f.body()?.tail_expr()
223+
}
224+
216225
#[cfg(test)]
217226
mod tests {
218227
use crate::tests::{check_assist, check_assist_target};
@@ -609,8 +618,8 @@ mod tests {
609618
}
610619
611620
impl Foo for ${1:_} {
612-
$0fn foo(&self) -> i32 {
613-
todo!()
621+
fn foo(&self) -> i32 {
622+
$0todo!()
614623
}
615624
}
616625
"#,
@@ -640,8 +649,8 @@ mod tests {
640649
}
641650
642651
impl Foo<${1:_}> for ${2:_} {
643-
$0fn foo(&self) -> _ {
644-
todo!()
652+
fn foo(&self) -> _ {
653+
$0todo!()
645654
}
646655
}
647656
"#,
@@ -667,8 +676,8 @@ mod tests {
667676
}
668677
669678
impl Foo<${1:_}, ${2:_}> for ${3:_} {
670-
$0fn foo(&self) -> _ {
671-
todo!()
679+
fn foo(&self) -> _ {
680+
$0todo!()
672681
}
673682
}
674683
"#,
@@ -702,8 +711,8 @@ mod tests {
702711
}
703712
704713
impl Foo for ${1:_} {
705-
$0fn foo(&self) -> i32 {
706-
todo!()
714+
fn foo(&self) -> i32 {
715+
$0todo!()
707716
}
708717
}
709718
"#,
@@ -729,10 +738,10 @@ mod tests {
729738
}
730739
731740
impl Foo for ${1:_} {
732-
$0type Output;
741+
type Output;
733742
734743
fn foo(&self) -> Self::Output {
735-
todo!()
744+
$0todo!()
736745
}
737746
}
738747
"#,

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)