Skip to content

Commit b4807d8

Browse files
committed
one step forward (and one backward) in resolving aliases in pipe completion
1 parent b403ca6 commit b4807d8

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
723723
|> completionsGetTypeEnv
724724
with
725725
| Some (typ, envFromCompletionItem) -> (
726+
let env, typ =
727+
typ |> TypeUtils.resolveTypeForPipeCompletion ~env ~package
728+
in
729+
726730
(* If the type we're completing on is a type parameter, we won't be able to do
727731
completion unless we know what that type parameter is compiled as. This
728732
attempts to look up the compiled type for that type parameter by looking

analysis/src/TypeUtils.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ let rec extractType ~env ~package (t : Types.type_expr) =
148148
Some (Tpolyvariant {env; constructors; typeExpr = t})
149149
| _ -> None
150150

151+
let rec resolveTypeForPipeCompletion ~env ~package (t : Types.type_expr) =
152+
match t.desc with
153+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) ->
154+
resolveTypeForPipeCompletion ~env ~package t1
155+
(* Don't descend into types named "t". Type t is a convention in the ReScript ecosystem. *)
156+
| Tconstr (path, _, _) when path |> Path.last = "t" -> (env, t)
157+
| Tconstr (path, _, _) -> (
158+
match References.digConstructor ~env ~package path with
159+
| Some (env, {item = {decl = {type_manifest = Some typ}}}) ->
160+
resolveTypeForPipeCompletion ~env ~package typ
161+
| _ -> (env, t))
162+
| _ -> (env, t)
163+
151164
(** This moves through a nested path via a set of instructions, trying to resolve the type at the end of the path. *)
152165
let rec resolveNested (typ : completionType) ~env ~package ~nested =
153166
match nested with

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,17 +1745,5 @@ posCursor:[425:8] posNoWhite:[425:7] Found expr:[425:3->425:8]
17451745
Completable: Cpath Value[ok]->g
17461746
Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder
17471747
Resolved opens 2 Completion.res Completion.res
1748-
[{
1749-
"label": "Belt.Result.getExn",
1750-
"kind": 12,
1751-
"tags": [],
1752-
"detail": "t<'a, 'b> => 'a",
1753-
"documentation": {"kind": "markdown", "value": "\n `getExn(res)`: when `res` is `Ok(n)`, returns `n` when `res` is `Error(m)`, raise an exception\n\n ```res example\n Belt.Result.getExn(Belt.Result.Ok(42)) == 42\n\n Belt.Result.getExn(Belt.Result.Error(\"Invalid data\")) /* raises exception */\n ```\n"}
1754-
}, {
1755-
"label": "Belt.Result.getWithDefault",
1756-
"kind": 12,
1757-
"tags": [],
1758-
"detail": "(t<'a, 'b>, 'a) => 'a",
1759-
"documentation": {"kind": "markdown", "value": "\n `getWithDefault(res, defaultValue)`: If `res` is `Ok(n)`, returns `n`,\n otherwise `default`\n\n ```res example\n Belt.Result.getWithDefault(Ok(42), 0) == 42\n\n Belt.Result.getWithDefault(Error(\"Invalid Data\"), 0) == 0\n ```\n"}
1760-
}]
1748+
[]
17611749

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,25 @@ Completable: Cpath Value[Belt, Array, reduce](Nolabel, Nolabel, Nolabel)->t
259259
Complete src/CompletionPipeChain.res 70:12
260260
posCursor:[70:12] posNoWhite:[70:11] Found expr:[70:3->0:-1]
261261
Completable: Cpath Value[aliased]->
262-
[]
262+
[{
263+
"label": "CompletionSupport.Test.add",
264+
"kind": 12,
265+
"tags": [],
266+
"detail": "t => int",
267+
"documentation": null
268+
}, {
269+
"label": "CompletionSupport.Test.addSelf",
270+
"kind": 12,
271+
"tags": [],
272+
"detail": "t => t",
273+
"documentation": null
274+
}, {
275+
"label": "CompletionSupport.Test.make",
276+
"kind": 12,
277+
"tags": [],
278+
"detail": "int => t",
279+
"documentation": null
280+
}]
263281

264282
Complete src/CompletionPipeChain.res 73:15
265283
posCursor:[73:15] posNoWhite:[73:14] Found expr:[73:3->0:-1]

0 commit comments

Comments
 (0)