Skip to content

Commit f2b4644

Browse files
authored
Make completion engine understand await (#813)
* make completion engine understand await * changelog
1 parent 09fd729 commit f2b4644

File tree

7 files changed

+68
-1
lines changed

7 files changed

+68
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#### :nail_care: Polish
2323

2424
- Revamp "Insert missing cases" code action to make it apply in more cases and be much more robust. https://github.com/rescript-lang/rescript-vscode/pull/804
25+
- Make the completion engine understand async/await. https://github.com/rescript-lang/rescript-vscode/pull/813
2526

2627
#### :bug: Bug Fix
2728

analysis/src/CompletionBackEnd.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,16 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
735735
~kind:
736736
(Completion.ExtractedType (Toption (env, ExtractedType typ), `Type));
737737
])
738+
| CPAwait cp -> (
739+
match
740+
cp
741+
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
742+
~exact:true ~scope
743+
|> completionsGetCompletionType ~full
744+
with
745+
| Some (Tpromise (env, typ), _env) ->
746+
[Completion.create "dummy" ~env ~kind:(Completion.Value typ)]
747+
| _ -> [])
738748
| CPId (path, completionContext) ->
739749
path
740750
|> getCompletionsForPath ~debug ~package ~opens ~full ~pos ~exact
@@ -1433,6 +1443,7 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
14331443
]
14341444
~env;
14351445
]
1446+
| Tpromise _ -> []
14361447

14371448
let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
14381449
if debug then

analysis/src/CompletionFrontEnd.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ let findArgCompletables ~(args : arg list) ~endPos ~posBeforeCursor
136136
})
137137
| _ -> loop args
138138

139-
let rec exprToContextPath (e : Parsetree.expression) =
139+
let rec exprToContextPathInner (e : Parsetree.expression) =
140140
match e.pexp_desc with
141141
| Pexp_constant (Pconst_string _) -> Some Completable.CPString
142142
| Pexp_constant (Pconst_integer _) -> Some CPInt
@@ -199,6 +199,15 @@ let rec exprToContextPath (e : Parsetree.expression) =
199199
else None
200200
| _ -> None
201201

202+
and exprToContextPath (e : Parsetree.expression) =
203+
match
204+
( Res_parsetree_viewer.hasAwaitAttribute e.pexp_attributes,
205+
exprToContextPathInner e )
206+
with
207+
| true, Some ctxPath -> Some (CPAwait ctxPath)
208+
| false, Some ctxPath -> Some ctxPath
209+
| _, None -> None
210+
202211
let completePipeChain (exp : Parsetree.expression) =
203212
(* Complete the end of pipe chains by reconstructing the pipe chain as a single pipe,
204213
so it can be completed.

analysis/src/SharedTypes.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ type innerType = TypeExpr of Types.type_expr | ExtractedType of completionType
310310
and completionType =
311311
| Tuple of QueryEnv.t * Types.type_expr list * Types.type_expr
312312
| Texn of QueryEnv.t
313+
| Tpromise of QueryEnv.t * Types.type_expr
313314
| Toption of QueryEnv.t * innerType
314315
| Tbool of QueryEnv.t
315316
| Tarray of QueryEnv.t * innerType
@@ -578,6 +579,7 @@ module Completable = struct
578579
| CPId of string list * completionContext
579580
| CPField of contextPath * string
580581
| CPObj of contextPath * string
582+
| CPAwait of contextPath
581583
| CPPipe of {
582584
contextPath: contextPath;
583585
id: string;
@@ -631,6 +633,7 @@ module Completable = struct
631633
| CPInt -> "int"
632634
| CPFloat -> "float"
633635
| CPBool -> "bool"
636+
| CPAwait ctxPath -> "await " ^ contextPathToString ctxPath
634637
| CPOption ctxPath -> "option<" ^ contextPathToString ctxPath ^ ">"
635638
| CPApply (cp, labels) ->
636639
contextPathToString cp ^ "("

analysis/src/TypeUtils.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ let rec extractType ~env ~package (t : Types.type_expr) =
115115
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> extractType ~env ~package t1
116116
| Tconstr (Path.Pident {name = "option"}, [payloadTypeExpr], _) ->
117117
Some (Toption (env, TypeExpr payloadTypeExpr))
118+
| Tconstr (Path.Pident {name = "promise"}, [payloadTypeExpr], _) ->
119+
Some (Tpromise (env, payloadTypeExpr))
118120
| Tconstr (Path.Pident {name = "array"}, [payloadTypeExpr], _) ->
119121
Some (Tarray (env, TypeExpr payloadTypeExpr))
120122
| Tconstr (Path.Pident {name = "bool"}, [], _) -> Some (Tbool env)
@@ -595,6 +597,7 @@ let rec extractedTypeToString ?(inner = false) = function
595597
"option<" ^ Shared.typeToString innerTyp ^ ">"
596598
| Toption (_, ExtractedType innerTyp) ->
597599
"option<" ^ extractedTypeToString ~inner:true innerTyp ^ ">"
600+
| Tpromise (_, innerTyp) -> "promise<" ^ Shared.typeToString innerTyp ^ ">"
598601
| Tvariant {variantDecl; variantName} ->
599602
if inner then variantName else Shared.declToString variantName variantDecl
600603
| Trecord {definition = `NameOnly name; fields} ->

analysis/tests/src/CompletionPattern.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,8 @@ let xn: exn = Obj.magic()
206206

207207
// switch xn { | }
208208
// ^com
209+
210+
let getThing = async () => One
211+
212+
// switch await getThing() { | }
213+
// ^com

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,3 +1070,38 @@ Path xn
10701070
"documentation": {"kind": "markdown", "value": "Matches on a JavaScript error. Read more in the [documentation on catching JS exceptions](https://rescript-lang.org/docs/manual/latest/exception#catching-js-exceptions)."}
10711071
}]
10721072

1073+
Complete src/CompletionPattern.res 211:30
1074+
XXX Not found!
1075+
Completable: Cpattern await Value[getThing](Nolabel)
1076+
Package opens Pervasives.JsxModules.place holder
1077+
Resolved opens 1 pervasives
1078+
ContextPath await Value[getThing](Nolabel)
1079+
ContextPath Value[getThing](Nolabel)
1080+
ContextPath Value[getThing]
1081+
Path getThing
1082+
[{
1083+
"label": "One",
1084+
"kind": 4,
1085+
"tags": [],
1086+
"detail": "One\n\ntype someVariant = One | Two(bool) | Three(someRecord, bool)",
1087+
"documentation": null,
1088+
"insertText": "One",
1089+
"insertTextFormat": 2
1090+
}, {
1091+
"label": "Two(_)",
1092+
"kind": 4,
1093+
"tags": [],
1094+
"detail": "Two(bool)\n\ntype someVariant = One | Two(bool) | Three(someRecord, bool)",
1095+
"documentation": null,
1096+
"insertText": "Two(${1:_})",
1097+
"insertTextFormat": 2
1098+
}, {
1099+
"label": "Three(_, _)",
1100+
"kind": 4,
1101+
"tags": [],
1102+
"detail": "Three(someRecord, bool)\n\ntype someVariant = One | Two(bool) | Three(someRecord, bool)",
1103+
"documentation": null,
1104+
"insertText": "Three(${1:_}, ${2:_})",
1105+
"insertTextFormat": 2
1106+
}]
1107+

0 commit comments

Comments
 (0)