Skip to content

Commit

Permalink
Make function template async when fn returns a promise (#816)
Browse files Browse the repository at this point in the history
* make function template async when fn returns a promise

* changelog
  • Loading branch information
zth authored Sep 11, 2023
1 parent d06ce9f commit f549132
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
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
}]

0 comments on commit f549132

Please sign in to comment.