Skip to content

Instantiate variant types as needed #814

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

Merged
merged 2 commits into from
Aug 20, 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 @@ -32,6 +32,7 @@
- Fix JS syntax highlighting in single-line FFI extension points. https://github.com/rescript-lang/rescript-vscode/pull/807
- Fix signature help in uncurried mode. https://github.com/rescript-lang/rescript-vscode/pull/809
- Fix various issues in uncurried mode. https://github.com/rescript-lang/rescript-vscode/pull/810
- Fixes a bug in pattern completion where for example `result` wouldn't complete, due to type variables getting lost/not being instantiated. https://github.com/rescript-lang/rescript-vscode/pull/814

## 1.18.0

Expand Down
2 changes: 2 additions & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ and completionType =
constructors: Constructor.t list;
variantDecl: Types.type_declaration;
variantName: string;
typeArgs: Types.type_expr list;
typeParams: Types.type_expr list;
}
| Tpolyvariant of {
env: QueryEnv.t;
Expand Down
33 changes: 25 additions & 8 deletions analysis/src/TypeUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,23 @@ let rec extractType ~env ~package (t : Types.type_expr) =
| args, _tRet when args <> [] ->
Some (Tfunction {env; args; typ = t; uncurried = true})
| _args, _tRet -> None)
| Tconstr (path, _, _) -> (
| Tconstr (path, typeArgs, _) -> (
match References.digConstructor ~env ~package path with
| Some (env, {item = {decl = {type_manifest = Some t1}}}) ->
extractType ~env ~package t1
| Some (env, {name; item = {decl; kind = Type.Variant constructors}}) ->
| Some (_env, {item = {decl = {type_manifest = Some t1; type_params}}}) ->
t1
|> instantiateType ~typeParams:type_params ~typeArgs
|> extractType ~env ~package
| Some (_env, {name; item = {decl; kind = Type.Variant constructors}}) ->
Some
(Tvariant
{env; constructors; variantName = name.txt; variantDecl = decl})
{
env;
constructors;
variantName = name.txt;
variantDecl = decl;
typeArgs;
typeParams = decl.type_params;
})
| Some (env, {item = {kind = Record fields}}) ->
Some (Trecord {env; fields; definition = `TypeExpr t})
| _ -> None)
Expand Down Expand Up @@ -280,7 +289,14 @@ let extractTypeFromResolvedType (typ : Type.t) ~env ~full =
| Variant constructors ->
Some
(Tvariant
{env; constructors; variantName = typ.name; variantDecl = typ.decl})
{
env;
constructors;
variantName = typ.name;
variantDecl = typ.decl;
typeParams = typ.decl.type_params;
typeArgs = [];
})
| Abstract _ | Open -> (
match typ.decl.type_manifest with
| None -> None
Expand Down Expand Up @@ -345,8 +361,8 @@ let rec resolveNested ~env ~full ~nested ?ctx (typ : completionType) =
typ
|> extractType ~env ~package:full.package
|> Utils.Option.flatMap (fun t -> t |> resolveNested ~env ~full ~nested)
| NVariantPayload {constructorName; itemNum}, Tvariant {env; constructors}
-> (
| ( NVariantPayload {constructorName; itemNum},
Tvariant {env; constructors; typeParams; typeArgs} ) -> (
match
constructors
|> List.find_opt (fun (c : Constructor.t) ->
Expand All @@ -357,6 +373,7 @@ let rec resolveNested ~env ~full ~nested ?ctx (typ : completionType) =
| None -> None
| Some (typ, _) ->
typ
|> instantiateType ~typeParams ~typeArgs
|> extractType ~env ~package:full.package
|> Utils.Option.flatMap (fun typ ->
typ |> resolveNested ~env ~full ~nested))
Expand Down
5 changes: 5 additions & 0 deletions analysis/tests/src/CompletionPattern.res
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,8 @@ let getThing = async () => One

// switch await getThing() { | }
// ^com

let res: result<someVariant, somePolyVariant> = Ok(One)

// switch res { | Ok() }
// ^com
36 changes: 36 additions & 0 deletions analysis/tests/src/expected/CompletionPattern.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1105,3 +1105,39 @@ Path getThing
"insertTextFormat": 2
}]

Complete src/CompletionPattern.res 216:21
posCursor:[216:21] posNoWhite:[216:20] Found pattern:[216:18->216:22]
Ppat_construct Ok:[216:18->216:20]
posCursor:[216:21] posNoWhite:[216:20] Found pattern:[216:20->216:22]
Ppat_construct ():[216:20->216:22]
Completable: Cpattern Value[res]->variantPayload::Ok($0)
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath Value[res]
Path res
[{
"label": "One",
"kind": 4,
"tags": [],
"detail": "One\n\ntype someVariant = One | Two(bool) | Three(someRecord, bool)",
"documentation": null,
"insertText": "One",
"insertTextFormat": 2
}, {
"label": "Two(_)",
"kind": 4,
"tags": [],
"detail": "Two(bool)\n\ntype someVariant = One | Two(bool) | Three(someRecord, bool)",
"documentation": null,
"insertText": "Two(${1:_})",
"insertTextFormat": 2
}, {
"label": "Three(_, _)",
"kind": 4,
"tags": [],
"detail": "Three(someRecord, bool)\n\ntype someVariant = One | Two(bool) | Three(someRecord, bool)",
"documentation": null,
"insertText": "Three(${1:_}, ${2:_})",
"insertTextFormat": 2
}]