@@ -171,7 +171,7 @@ struct FunctionTemplate {
171
171
insert_offset : TextSize ,
172
172
leading_ws : String ,
173
173
fn_def : ast:: Fn ,
174
- ret_type : ast:: RetType ,
174
+ ret_type : Option < ast:: RetType > ,
175
175
should_focus_tail_expr : bool ,
176
176
trailing_ws : String ,
177
177
file : FileId ,
@@ -183,7 +183,11 @@ impl FunctionTemplate {
183
183
let f = match cap {
184
184
Some ( cap) => {
185
185
let cursor = if self . should_focus_tail_expr {
186
- self . ret_type . syntax ( )
186
+ if let Some ( ref ret_type) = self . ret_type {
187
+ ret_type. syntax ( )
188
+ } else {
189
+ self . tail_expr . syntax ( )
190
+ }
187
191
} else {
188
192
self . tail_expr . syntax ( )
189
193
} ;
@@ -201,7 +205,7 @@ struct FunctionBuilder {
201
205
fn_name : ast:: Name ,
202
206
type_params : Option < ast:: GenericParamList > ,
203
207
params : ast:: ParamList ,
204
- ret_type : ast:: RetType ,
208
+ ret_type : Option < ast:: RetType > ,
205
209
should_focus_tail_expr : bool ,
206
210
file : FileId ,
207
211
needs_pub : bool ,
@@ -235,33 +239,8 @@ impl FunctionBuilder {
235
239
let await_expr = call. syntax ( ) . parent ( ) . and_then ( ast:: AwaitExpr :: cast) ;
236
240
let is_async = await_expr. is_some ( ) ;
237
241
238
- // should_focus_tail_expr intends to express a rough level of confidence about
239
- // the correctness of the return type.
240
- //
241
- // If we are able to infer some return type, and that return type is not unit, we
242
- // don't want to render the snippet. The assumption here is in this situation the
243
- // return type is just as likely to be correct as any other part of the generated
244
- // function.
245
- //
246
- // In the case where the return type is inferred as unit it is likely that the
247
- // user does in fact intend for this generated function to return some non unit
248
- // type, but that the current state of their code doesn't allow that return type
249
- // to be accurately inferred.
250
- let ( ret_ty, should_focus_tail_expr) = {
251
- match ctx. sema . type_of_expr ( & ast:: Expr :: CallExpr ( call. clone ( ) ) ) . map ( TypeInfo :: original)
252
- {
253
- Some ( ty) if ty. is_unknown ( ) || ty. is_unit ( ) => ( make:: ty_unit ( ) , true ) ,
254
- Some ( ty) => {
255
- let rendered = ty. display_source_code ( ctx. db ( ) , target_module. into ( ) ) ;
256
- match rendered {
257
- Ok ( rendered) => ( make:: ty ( & rendered) , false ) ,
258
- Err ( _) => ( make:: ty_unit ( ) , true ) ,
259
- }
260
- }
261
- None => ( make:: ty_unit ( ) , true ) ,
262
- }
263
- } ;
264
- let ret_type = make:: ret_type ( ret_ty) ;
242
+ let ( ret_type, should_focus_tail_expr) =
243
+ make_return_type ( ctx, & ast:: Expr :: CallExpr ( call. clone ( ) ) , target_module) ;
265
244
266
245
Some ( Self {
267
246
target,
@@ -305,44 +284,16 @@ impl FunctionBuilder {
305
284
let await_expr = call. syntax ( ) . parent ( ) . and_then ( ast:: AwaitExpr :: cast) ;
306
285
let is_async = await_expr. is_some ( ) ;
307
286
308
- // should_render_snippet intends to express a rough level of confidence about
309
- // the correctness of the return type.
310
- //
311
- // If we are able to infer some return type, and that return type is not unit, we
312
- // don't want to render the snippet. The assumption here is in this situation the
313
- // return type is just as likely to be correct as any other part of the generated
314
- // function.
315
- //
316
- // In the case where the return type is inferred as unit it is likely that the
317
- // user does in fact intend for this generated function to return some non unit
318
- // type, but that the current state of their code doesn't allow that return type
319
- // to be accurately inferred.
320
- let ( ret_ty, should_render_snippet) = {
321
- match ctx
322
- . sema
323
- . type_of_expr ( & ast:: Expr :: MethodCallExpr ( call. clone ( ) ) )
324
- . map ( TypeInfo :: original)
325
- {
326
- Some ( ty) if ty. is_unknown ( ) || ty. is_unit ( ) => ( make:: ty_unit ( ) , true ) ,
327
- Some ( ty) => {
328
- let rendered = ty. display_source_code ( ctx. db ( ) , target_module. into ( ) ) ;
329
- match rendered {
330
- Ok ( rendered) => ( make:: ty ( & rendered) , false ) ,
331
- Err ( _) => ( make:: ty_unit ( ) , true ) ,
332
- }
333
- }
334
- None => ( make:: ty_unit ( ) , true ) ,
335
- }
336
- } ;
337
- let ret_type = make:: ret_type ( ret_ty) ;
287
+ let ( ret_type, should_focus_tail_expr) =
288
+ make_return_type ( ctx, & ast:: Expr :: MethodCallExpr ( call. clone ( ) ) , target_module) ;
338
289
339
290
Some ( Self {
340
291
target,
341
292
fn_name,
342
293
type_params,
343
294
params,
344
295
ret_type,
345
- should_focus_tail_expr : should_render_snippet ,
296
+ should_focus_tail_expr,
346
297
file,
347
298
needs_pub,
348
299
is_async,
@@ -359,7 +310,7 @@ impl FunctionBuilder {
359
310
self . type_params ,
360
311
self . params ,
361
312
fn_body,
362
- Some ( self . ret_type ) ,
313
+ self . ret_type ,
363
314
self . is_async ,
364
315
) ;
365
316
let leading_ws;
@@ -386,7 +337,7 @@ impl FunctionBuilder {
386
337
insert_offset,
387
338
leading_ws,
388
339
// PANIC: we guarantee we always create a function with a return type
389
- ret_type : fn_def. ret_type ( ) . unwrap ( ) ,
340
+ ret_type : fn_def. ret_type ( ) ,
390
341
// PANIC: we guarantee we always create a function body with a tail expr
391
342
tail_expr : fn_def. body ( ) . unwrap ( ) . tail_expr ( ) . unwrap ( ) ,
392
343
should_focus_tail_expr : self . should_focus_tail_expr ,
@@ -397,6 +348,29 @@ impl FunctionBuilder {
397
348
}
398
349
}
399
350
351
+ fn make_return_type (
352
+ ctx : & AssistContext ,
353
+ call : & ast:: Expr ,
354
+ target_module : Module ,
355
+ ) -> ( Option < ast:: RetType > , bool ) {
356
+ let ( ret_ty, should_focus_tail_expr) = {
357
+ match ctx. sema . type_of_expr ( call) . map ( TypeInfo :: original) {
358
+ Some ( ty) if ty. is_unit ( ) => ( None , false ) ,
359
+ Some ( ty) if ty. is_unknown ( ) => ( Some ( make:: ty_unit ( ) ) , true ) ,
360
+ None => ( Some ( make:: ty_unit ( ) ) , true ) ,
361
+ Some ( ty) => {
362
+ let rendered = ty. display_source_code ( ctx. db ( ) , target_module. into ( ) ) ;
363
+ match rendered {
364
+ Ok ( rendered) => ( Some ( make:: ty ( & rendered) ) , false ) ,
365
+ Err ( _) => ( Some ( make:: ty_unit ( ) ) , true ) ,
366
+ }
367
+ }
368
+ }
369
+ } ;
370
+ let ret_type = ret_ty. map ( |rt| make:: ret_type ( rt) ) ;
371
+ ( ret_type, should_focus_tail_expr)
372
+ }
373
+
400
374
enum GeneratedFunctionTarget {
401
375
BehindItem ( SyntaxNode ) ,
402
376
InEmptyItemList ( SyntaxNode ) ,
@@ -825,8 +799,8 @@ fn foo() {
825
799
bar("bar")
826
800
}
827
801
828
- fn bar(arg: &str) ${0:-> ()} {
829
- todo!()
802
+ fn bar(arg: &str) {
803
+ ${0: todo!()}
830
804
}
831
805
"# ,
832
806
)
@@ -846,8 +820,8 @@ fn foo() {
846
820
bar('x')
847
821
}
848
822
849
- fn bar(arg: char) ${0:-> ()} {
850
- todo!()
823
+ fn bar(arg: char) {
824
+ ${0: todo!()}
851
825
}
852
826
"# ,
853
827
)
@@ -867,8 +841,8 @@ fn foo() {
867
841
bar(42)
868
842
}
869
843
870
- fn bar(arg: i32) ${0:-> ()} {
871
- todo!()
844
+ fn bar(arg: i32) {
845
+ ${0: todo!()}
872
846
}
873
847
" ,
874
848
)
@@ -888,8 +862,8 @@ fn foo() {
888
862
bar(42 as u8)
889
863
}
890
864
891
- fn bar(arg: u8) ${0:-> ()} {
892
- todo!()
865
+ fn bar(arg: u8) {
866
+ ${0: todo!()}
893
867
}
894
868
" ,
895
869
)
@@ -913,8 +887,8 @@ fn foo() {
913
887
bar(x as u8)
914
888
}
915
889
916
- fn bar(x: u8) ${0:-> ()} {
917
- todo!()
890
+ fn bar(x: u8) {
891
+ ${0: todo!()}
918
892
}
919
893
" ,
920
894
)
@@ -936,8 +910,8 @@ fn foo() {
936
910
bar(worble)
937
911
}
938
912
939
- fn bar(worble: ()) ${0:-> ()} {
940
- todo!()
913
+ fn bar(worble: ()) {
914
+ ${0: todo!()}
941
915
}
942
916
" ,
943
917
)
@@ -965,8 +939,8 @@ fn baz() {
965
939
bar(foo())
966
940
}
967
941
968
- fn bar(foo: impl Foo) ${0:-> ()} {
969
- todo!()
942
+ fn bar(foo: impl Foo) {
943
+ ${0: todo!()}
970
944
}
971
945
" ,
972
946
)
@@ -992,8 +966,8 @@ fn foo() {
992
966
bar(&baz())
993
967
}
994
968
995
- fn bar(baz: &Baz) ${0:-> ()} {
996
- todo!()
969
+ fn bar(baz: &Baz) {
970
+ ${0: todo!()}
997
971
}
998
972
" ,
999
973
)
@@ -1021,8 +995,8 @@ fn foo() {
1021
995
bar(Baz::baz())
1022
996
}
1023
997
1024
- fn bar(baz: Baz::Bof) ${0:-> ()} {
1025
- todo!()
998
+ fn bar(baz: Baz::Bof) {
999
+ ${0: todo!()}
1026
1000
}
1027
1001
" ,
1028
1002
)
@@ -1043,8 +1017,8 @@ fn foo<T>(t: T) {
1043
1017
bar(t)
1044
1018
}
1045
1019
1046
- fn bar(t: T) ${0:-> ()} {
1047
- todo!()
1020
+ fn bar(t: T) {
1021
+ ${0: todo!()}
1048
1022
}
1049
1023
" ,
1050
1024
)
@@ -1097,8 +1071,8 @@ fn foo() {
1097
1071
bar(closure)
1098
1072
}
1099
1073
1100
- fn bar(closure: ()) ${0:-> ()} {
1101
- todo!()
1074
+ fn bar(closure: ()) {
1075
+ ${0: todo!()}
1102
1076
}
1103
1077
" ,
1104
1078
)
@@ -1118,8 +1092,8 @@ fn foo() {
1118
1092
bar(baz)
1119
1093
}
1120
1094
1121
- fn bar(baz: ()) ${0:-> ()} {
1122
- todo!()
1095
+ fn bar(baz: ()) {
1096
+ ${0: todo!()}
1123
1097
}
1124
1098
" ,
1125
1099
)
@@ -1143,8 +1117,8 @@ fn foo() {
1143
1117
bar(baz(), baz())
1144
1118
}
1145
1119
1146
- fn bar(baz_1: Baz, baz_2: Baz) ${0:-> ()} {
1147
- todo!()
1120
+ fn bar(baz_1: Baz, baz_2: Baz) {
1121
+ ${0: todo!()}
1148
1122
}
1149
1123
" ,
1150
1124
)
@@ -1168,8 +1142,8 @@ fn foo() {
1168
1142
bar(baz(), baz(), "foo", "bar")
1169
1143
}
1170
1144
1171
- fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) ${0:-> ()} {
1172
- todo!()
1145
+ fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
1146
+ ${0: todo!()}
1173
1147
}
1174
1148
"# ,
1175
1149
)
@@ -1188,8 +1162,8 @@ fn foo() {
1188
1162
" ,
1189
1163
r"
1190
1164
mod bar {
1191
- pub(crate) fn my_fn() ${0:-> ()} {
1192
- todo!()
1165
+ pub(crate) fn my_fn() {
1166
+ ${0: todo!()}
1193
1167
}
1194
1168
}
1195
1169
@@ -1224,8 +1198,8 @@ fn bar() {
1224
1198
baz(foo)
1225
1199
}
1226
1200
1227
- fn baz(foo: foo::Foo) ${0:-> ()} {
1228
- todo!()
1201
+ fn baz(foo: foo::Foo) {
1202
+ ${0: todo!()}
1229
1203
}
1230
1204
"# ,
1231
1205
)
@@ -1248,8 +1222,8 @@ fn foo() {
1248
1222
mod bar {
1249
1223
fn something_else() {}
1250
1224
1251
- pub(crate) fn my_fn() ${0:-> ()} {
1252
- todo!()
1225
+ pub(crate) fn my_fn() {
1226
+ ${0: todo!()}
1253
1227
}
1254
1228
}
1255
1229
@@ -1276,8 +1250,8 @@ fn foo() {
1276
1250
r"
1277
1251
mod bar {
1278
1252
mod baz {
1279
- pub(crate) fn my_fn() ${0:-> ()} {
1280
- todo!()
1253
+ pub(crate) fn my_fn() {
1254
+ ${0: todo!()}
1281
1255
}
1282
1256
}
1283
1257
}
@@ -1305,8 +1279,8 @@ fn main() {
1305
1279
r"
1306
1280
1307
1281
1308
- pub(crate) fn bar() ${0:-> ()} {
1309
- todo!()
1282
+ pub(crate) fn bar() {
1283
+ ${0: todo!()}
1310
1284
}" ,
1311
1285
)
1312
1286
}
0 commit comments