@@ -172,19 +172,26 @@ struct FunctionTemplate {
172
172
leading_ws : String ,
173
173
fn_def : ast:: Fn ,
174
174
ret_type : ast:: RetType ,
175
- should_render_snippet : bool ,
175
+ should_focus_tail_expr : bool ,
176
176
trailing_ws : String ,
177
177
file : FileId ,
178
+ tail_expr : ast:: Expr ,
178
179
}
179
180
180
181
impl FunctionTemplate {
181
182
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) )
185
191
}
186
- _ => self . fn_def . to_string ( ) ,
192
+ None => self . fn_def . to_string ( ) ,
187
193
} ;
194
+
188
195
format ! ( "{}{}{}" , self . leading_ws, f, self . trailing_ws)
189
196
}
190
197
}
@@ -195,7 +202,7 @@ struct FunctionBuilder {
195
202
type_params : Option < ast:: GenericParamList > ,
196
203
params : ast:: ParamList ,
197
204
ret_type : ast:: RetType ,
198
- should_render_snippet : bool ,
205
+ should_focus_tail_expr : bool ,
199
206
file : FileId ,
200
207
needs_pub : bool ,
201
208
is_async : bool ,
@@ -228,7 +235,7 @@ impl FunctionBuilder {
228
235
let await_expr = call. syntax ( ) . parent ( ) . and_then ( ast:: AwaitExpr :: cast) ;
229
236
let is_async = await_expr. is_some ( ) ;
230
237
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
232
239
// the correctness of the return type.
233
240
//
234
241
// If we are able to infer some return type, and that return type is not unit, we
@@ -240,7 +247,7 @@ impl FunctionBuilder {
240
247
// user does in fact intend for this generated function to return some non unit
241
248
// type, but that the current state of their code doesn't allow that return type
242
249
// to be accurately inferred.
243
- let ( ret_ty, should_render_snippet ) = {
250
+ let ( ret_ty, should_focus_tail_expr ) = {
244
251
match ctx. sema . type_of_expr ( & ast:: Expr :: CallExpr ( call. clone ( ) ) ) . map ( TypeInfo :: original)
245
252
{
246
253
Some ( ty) if ty. is_unknown ( ) || ty. is_unit ( ) => ( make:: ty_unit ( ) , true ) ,
@@ -262,7 +269,7 @@ impl FunctionBuilder {
262
269
type_params,
263
270
params,
264
271
ret_type,
265
- should_render_snippet ,
272
+ should_focus_tail_expr ,
266
273
file,
267
274
needs_pub,
268
275
is_async,
@@ -335,7 +342,7 @@ impl FunctionBuilder {
335
342
type_params,
336
343
params,
337
344
ret_type,
338
- should_render_snippet,
345
+ should_focus_tail_expr : should_render_snippet,
339
346
file,
340
347
needs_pub,
341
348
is_async,
@@ -378,8 +385,11 @@ impl FunctionBuilder {
378
385
FunctionTemplate {
379
386
insert_offset,
380
387
leading_ws,
388
+ // PANIC: we guarantee we always create a function with a return type
381
389
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 ,
383
393
fn_def,
384
394
trailing_ws,
385
395
file : self . file ,
@@ -795,7 +805,7 @@ impl Baz {
795
805
}
796
806
797
807
fn bar(baz: Baz) -> Baz {
798
- todo!()
808
+ ${0: todo!()}
799
809
}
800
810
" ,
801
811
)
@@ -1316,7 +1326,7 @@ fn main() {
1316
1326
}
1317
1327
1318
1328
fn foo() -> u32 {
1319
- todo!()
1329
+ ${0: todo!()}
1320
1330
}
1321
1331
" ,
1322
1332
)
0 commit comments