Skip to content

Commit 0834e05

Browse files
committed
Always fall back to focusing on generated function body
1 parent c619cb1 commit 0834e05

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,26 @@ struct FunctionTemplate {
172172
leading_ws: String,
173173
fn_def: ast::Fn,
174174
ret_type: ast::RetType,
175-
should_render_snippet: bool,
175+
should_focus_tail_expr: bool,
176176
trailing_ws: String,
177177
file: FileId,
178+
tail_expr: ast::Expr,
178179
}
179180

180181
impl FunctionTemplate {
181182
fn to_string(&self, cap: Option<SnippetCap>) -> String {
182-
let f = match (cap, self.should_render_snippet) {
183-
(Some(cap), true) => {
184-
render_snippet(cap, self.fn_def.syntax(), Cursor::Replace(self.ret_type.syntax()))
183+
let f = match cap {
184+
Some(cap) => {
185+
let cursor = if self.should_focus_tail_expr {
186+
self.ret_type.syntax()
187+
} else {
188+
self.tail_expr.syntax()
189+
};
190+
render_snippet(cap, self.fn_def.syntax(), Cursor::Replace(cursor))
185191
}
186-
_ => self.fn_def.to_string(),
192+
None => self.fn_def.to_string(),
187193
};
194+
188195
format!("{}{}{}", self.leading_ws, f, self.trailing_ws)
189196
}
190197
}
@@ -195,7 +202,7 @@ struct FunctionBuilder {
195202
type_params: Option<ast::GenericParamList>,
196203
params: ast::ParamList,
197204
ret_type: ast::RetType,
198-
should_render_snippet: bool,
205+
should_focus_tail_expr: bool,
199206
file: FileId,
200207
needs_pub: bool,
201208
is_async: bool,
@@ -228,7 +235,7 @@ impl FunctionBuilder {
228235
let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
229236
let is_async = await_expr.is_some();
230237

231-
// should_render_snippet intends to express a rough level of confidence about
238+
// should_focus_tail_expr intends to express a rough level of confidence about
232239
// the correctness of the return type.
233240
//
234241
// If we are able to infer some return type, and that return type is not unit, we
@@ -240,7 +247,7 @@ impl FunctionBuilder {
240247
// user does in fact intend for this generated function to return some non unit
241248
// type, but that the current state of their code doesn't allow that return type
242249
// to be accurately inferred.
243-
let (ret_ty, should_render_snippet) = {
250+
let (ret_ty, should_focus_tail_expr) = {
244251
match ctx.sema.type_of_expr(&ast::Expr::CallExpr(call.clone())).map(TypeInfo::original)
245252
{
246253
Some(ty) if ty.is_unknown() || ty.is_unit() => (make::ty_unit(), true),
@@ -262,7 +269,7 @@ impl FunctionBuilder {
262269
type_params,
263270
params,
264271
ret_type,
265-
should_render_snippet,
272+
should_focus_tail_expr,
266273
file,
267274
needs_pub,
268275
is_async,
@@ -335,7 +342,7 @@ impl FunctionBuilder {
335342
type_params,
336343
params,
337344
ret_type,
338-
should_render_snippet,
345+
should_focus_tail_expr: should_render_snippet,
339346
file,
340347
needs_pub,
341348
is_async,
@@ -378,8 +385,11 @@ impl FunctionBuilder {
378385
FunctionTemplate {
379386
insert_offset,
380387
leading_ws,
388+
// PANIC: we guarantee we always create a function with a return type
381389
ret_type: fn_def.ret_type().unwrap(),
382-
should_render_snippet: self.should_render_snippet,
390+
// PANIC: we guarantee we always create a function body with a tail expr
391+
tail_expr: fn_def.body().unwrap().tail_expr().unwrap(),
392+
should_focus_tail_expr: self.should_focus_tail_expr,
383393
fn_def,
384394
trailing_ws,
385395
file: self.file,
@@ -795,7 +805,7 @@ impl Baz {
795805
}
796806
797807
fn bar(baz: Baz) -> Baz {
798-
todo!()
808+
${0:todo!()}
799809
}
800810
",
801811
)
@@ -1316,7 +1326,7 @@ fn main() {
13161326
}
13171327
13181328
fn foo() -> u32 {
1319-
todo!()
1329+
${0:todo!()}
13201330
}
13211331
",
13221332
)

0 commit comments

Comments
 (0)