Skip to content

Commit f549132

Browse files
authored
Make function template async when fn returns a promise (rescript-lang#816)
* make function template async when fn returns a promise * changelog
1 parent d06ce9f commit f549132

6 files changed

+45
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Add completion to top level decorators. https://github.com/rescript-lang/rescript-vscode/pull/799
1919
- Add code action for wrapping patterns where option is expected with `Some`. https://github.com/rescript-lang/rescript-vscode/pull/806
2020
- Better completion from identifiers with inferred types. https://github.com/rescript-lang/rescript-vscode/pull/808
21+
- Make suggested template functions async when the target function returns a promise. https://github.com/rescript-lang/rescript-vscode/pull/816
2122

2223
#### :nail_care: Polish
2324

analysis/src/CompletionBackEnd.ml

+11-4
Original file line numberDiff line numberDiff line change
@@ -1382,8 +1382,8 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
13821382
~env ();
13831383
]
13841384
else []
1385-
| Tfunction {env; typ; args; uncurried} when prefix = "" && mode = Expression
1386-
->
1385+
| Tfunction {env; typ; args; uncurried; returnType}
1386+
when prefix = "" && mode = Expression ->
13871387
let shouldPrintAsUncurried = uncurried && !Config.uncurried <> Uncurried in
13881388
let mkFnArgs ~asSnippet =
13891389
match args with
@@ -1419,11 +1419,18 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
14191419
in
14201420
"(" ^ if shouldPrintAsUncurried then ". " else "" ^ argsText ^ ")"
14211421
in
1422+
let isAsync =
1423+
match TypeUtils.extractType ~env ~package:full.package returnType with
1424+
| Some (Tpromise _) -> true
1425+
| _ -> false
1426+
in
1427+
let asyncPrefix = if isAsync then "async " else "" in
14221428
[
14231429
Completion.createWithSnippet
1424-
~name:(mkFnArgs ~asSnippet:false ^ " => {}")
1430+
~name:(asyncPrefix ^ mkFnArgs ~asSnippet:false ^ " => {}")
14251431
~insertText:
1426-
(mkFnArgs ~asSnippet:!Cfg.supportsSnippets
1432+
(asyncPrefix
1433+
^ mkFnArgs ~asSnippet:!Cfg.supportsSnippets
14271434
^ " => "
14281435
^ if !Cfg.supportsSnippets then "{$0}" else "{}")
14291436
~sortText:"A" ~kind:(Value typ) ~env ();

analysis/src/SharedTypes.ml

+1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ and completionType =
343343
args: typedFnArg list;
344344
typ: Types.type_expr;
345345
uncurried: bool;
346+
returnType: Types.type_expr;
346347
}
347348

348349
module Env = struct

analysis/src/TypeUtils.ml

+5-4
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ let rec extractType ~env ~package (t : Types.type_expr) =
125125
| Tconstr (Pident {name = "function$"}, [t; _], _) -> (
126126
(* Uncurried functions. *)
127127
match extractFunctionType t ~env ~package with
128-
| args, _tRet when args <> [] ->
129-
Some (Tfunction {env; args; typ = t; uncurried = true})
128+
| args, tRet when args <> [] ->
129+
Some (Tfunction {env; args; typ = t; uncurried = true; returnType = tRet})
130130
| _args, _tRet -> None)
131131
| Tconstr (path, typeArgs, _) -> (
132132
match References.digConstructor ~env ~package path with
@@ -168,8 +168,9 @@ let rec extractType ~env ~package (t : Types.type_expr) =
168168
Some (Tpolyvariant {env; constructors; typeExpr = t})
169169
| Tarrow _ -> (
170170
match extractFunctionType t ~env ~package with
171-
| args, _tRet when args <> [] ->
172-
Some (Tfunction {env; args; typ = t; uncurried = false})
171+
| args, tRet when args <> [] ->
172+
Some
173+
(Tfunction {env; args; typ = t; uncurried = false; returnType = tRet})
173174
| _args, _tRet -> None)
174175
| _ -> None
175176

analysis/tests/src/CompletionExpressions.res

+7
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,10 @@ external commitLocalUpdate: (~updater: RecordSourceSelectorProxy.t => unit) => u
250250

251251
// commitLocalUpdate(~updater=)
252252
// ^com
253+
254+
let fnTakingAsyncCallback = (cb: unit => promise<unit>) => {
255+
let _ = cb
256+
}
257+
258+
// fnTakingAsyncCallback()
259+
// ^com

analysis/tests/src/expected/CompletionExpressions.res.txt

+20
Original file line numberDiff line numberDiff line change
@@ -1074,3 +1074,23 @@ Path commitLocalUpdate
10741074
"insertTextFormat": 2
10751075
}]
10761076

1077+
Complete src/CompletionExpressions.res 257:25
1078+
posCursor:[257:25] posNoWhite:[257:24] Found expr:[257:3->257:26]
1079+
Pexp_apply ...[257:3->257:24] (...[257:25->257:26])
1080+
Completable: Cexpression CArgument Value[fnTakingAsyncCallback]($0)
1081+
Package opens Pervasives.JsxModules.place holder
1082+
Resolved opens 1 pervasives
1083+
ContextPath CArgument Value[fnTakingAsyncCallback]($0)
1084+
ContextPath Value[fnTakingAsyncCallback]
1085+
Path fnTakingAsyncCallback
1086+
[{
1087+
"label": "async () => {}",
1088+
"kind": 12,
1089+
"tags": [],
1090+
"detail": "unit => promise<unit>",
1091+
"documentation": null,
1092+
"sortText": "A",
1093+
"insertText": "async () => {$0}",
1094+
"insertTextFormat": 2
1095+
}]
1096+

0 commit comments

Comments
 (0)