Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make function template async when fn returns a promise #816

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Add completion to top level decorators. https://github.com/rescript-lang/rescript-vscode/pull/799
- Add code action for wrapping patterns where option is expected with `Some`. https://github.com/rescript-lang/rescript-vscode/pull/806
- Better completion from identifiers with inferred types. https://github.com/rescript-lang/rescript-vscode/pull/808
- Make suggested template functions async when the target function returns a promise. https://github.com/rescript-lang/rescript-vscode/pull/816

#### :nail_care: Polish

Expand Down
15 changes: 11 additions & 4 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1382,8 +1382,8 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
~env ();
]
else []
| Tfunction {env; typ; args; uncurried} when prefix = "" && mode = Expression
->
| Tfunction {env; typ; args; uncurried; returnType}
when prefix = "" && mode = Expression ->
let shouldPrintAsUncurried = uncurried && !Config.uncurried <> Uncurried in
let mkFnArgs ~asSnippet =
match args with
Expand Down Expand Up @@ -1419,11 +1419,18 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
in
"(" ^ if shouldPrintAsUncurried then ". " else "" ^ argsText ^ ")"
in
let isAsync =
match TypeUtils.extractType ~env ~package:full.package returnType with
| Some (Tpromise _) -> true
| _ -> false
in
let asyncPrefix = if isAsync then "async " else "" in
[
Completion.createWithSnippet
~name:(mkFnArgs ~asSnippet:false ^ " => {}")
~name:(asyncPrefix ^ mkFnArgs ~asSnippet:false ^ " => {}")
~insertText:
(mkFnArgs ~asSnippet:!Cfg.supportsSnippets
(asyncPrefix
^ mkFnArgs ~asSnippet:!Cfg.supportsSnippets
^ " => "
^ if !Cfg.supportsSnippets then "{$0}" else "{}")
~sortText:"A" ~kind:(Value typ) ~env ();
Expand Down
1 change: 1 addition & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ and completionType =
args: typedFnArg list;
typ: Types.type_expr;
uncurried: bool;
returnType: Types.type_expr;
}

module Env = struct
Expand Down
9 changes: 5 additions & 4 deletions analysis/src/TypeUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ let rec extractType ~env ~package (t : Types.type_expr) =
| Tconstr (Pident {name = "function$"}, [t; _], _) -> (
(* Uncurried functions. *)
match extractFunctionType t ~env ~package with
| args, _tRet when args <> [] ->
Some (Tfunction {env; args; typ = t; uncurried = true})
| args, tRet when args <> [] ->
Some (Tfunction {env; args; typ = t; uncurried = true; returnType = tRet})
| _args, _tRet -> None)
| Tconstr (path, typeArgs, _) -> (
match References.digConstructor ~env ~package path with
Expand Down Expand Up @@ -168,8 +168,9 @@ let rec extractType ~env ~package (t : Types.type_expr) =
Some (Tpolyvariant {env; constructors; typeExpr = t})
| Tarrow _ -> (
match extractFunctionType t ~env ~package with
| args, _tRet when args <> [] ->
Some (Tfunction {env; args; typ = t; uncurried = false})
| args, tRet when args <> [] ->
Some
(Tfunction {env; args; typ = t; uncurried = false; returnType = tRet})
| _args, _tRet -> None)
| _ -> None

Expand Down
7 changes: 7 additions & 0 deletions analysis/tests/src/CompletionExpressions.res
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,10 @@ external commitLocalUpdate: (~updater: RecordSourceSelectorProxy.t => unit) => u

// commitLocalUpdate(~updater=)
// ^com

let fnTakingAsyncCallback = (cb: unit => promise<unit>) => {
let _ = cb
}

// fnTakingAsyncCallback()
// ^com
20 changes: 20 additions & 0 deletions analysis/tests/src/expected/CompletionExpressions.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1074,3 +1074,23 @@ Path commitLocalUpdate
"insertTextFormat": 2
}]

Complete src/CompletionExpressions.res 257:25
posCursor:[257:25] posNoWhite:[257:24] Found expr:[257:3->257:26]
Pexp_apply ...[257:3->257:24] (...[257:25->257:26])
Completable: Cexpression CArgument Value[fnTakingAsyncCallback]($0)
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath CArgument Value[fnTakingAsyncCallback]($0)
ContextPath Value[fnTakingAsyncCallback]
Path fnTakingAsyncCallback
[{
"label": "async () => {}",
"kind": 12,
"tags": [],
"detail": "unit => promise<unit>",
"documentation": null,
"sortText": "A",
"insertText": "async () => {$0}",
"insertTextFormat": 2
}]