Skip to content

Pipe completion unknown/type parameter return types #662

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 15 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
27 changes: 26 additions & 1 deletion analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,17 @@ let completionsGetTypeEnv = function
| {Completion.kind = Field ({typ}, _); env} :: _ -> Some (typ, env)
| _ -> None

let findReturnTypeOfFunctionAtLoc loc ~(env : QueryEnv.t) ~package ~debug =
match Cmt.loadFullCmtFromPath ~path:(env.file.uri |> Uri.toPath) with
| None -> None
| Some full -> (
match References.getLocItem ~full ~pos:(loc |> Loc.end_) ~debug with
| Some {locType = Typed (_, typExpr, _)} -> (
match extractFunctionType ~env ~package typExpr with
| args, tRet when args <> [] -> Some tRet
| _ -> None)
| _ -> None)

let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
~env ~exact ~scope (contextPath : Completable.contextPath) =
match contextPath with
Expand Down Expand Up @@ -1275,14 +1286,28 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
else None)
| None -> [])
| None -> [])
| CPPipe (cp, funNamePrefix) -> (
| CPPipe {contextPath = cp; id = funNamePrefix; lhsLoc} -> (
match
cp
|> getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
~env ~exact:true ~scope
|> completionsGetTypeEnv
with
| Some (typ, envFromCompletionItem) -> (
(* If the type we're completing on is a type parameter, we won't be able to do
completion unless we know what that type parameter is compiled as. This
attempts to look up the compiled type for that type parameter by looking
for compiled information at the loc of that expression. *)
let typ =
match typ with
| {Types.desc = Tvar _} -> (
match
findReturnTypeOfFunctionAtLoc lhsLoc ~env ~package ~debug:false
with
| None -> typ
| Some typFromLoc -> typFromLoc)
| _ -> typ
in
let {
arrayModulePath;
optionModulePath;
Expand Down
9 changes: 6 additions & 3 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ let completePipeChain ~(lhs : Parsetree.expression) =
pexp_loc;
pexp_attributes;
}
|> Option.map (fun ctxPath -> (ctxPath, d.pexp_loc))
(* When the left side of the pipe we're completing is an identifier application.
Example: someArray->filterAllTheGoodStuff-> *)
| Pexp_apply
Expand All @@ -195,6 +196,7 @@ let completePipeChain ~(lhs : Parsetree.expression) =
pexp_loc;
pexp_attributes;
}
|> Option.map (fun ctxPath -> (ctxPath, pexp_loc))
| _ -> None

let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
Expand Down Expand Up @@ -436,11 +438,12 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
| None -> (
match exprToContextPath lhs with
| Some pipe ->
setResult (Cpath (CPPipe (pipe, id)));
setResult
(Cpath (CPPipe {contextPath = pipe; id; lhsLoc = lhs.pexp_loc}));
true
| None -> false)
| Some pipe ->
setResult (Cpath (CPPipe (pipe, id)));
| Some (pipe, lhsLoc) ->
setResult (Cpath (CPPipe {contextPath = pipe; id; lhsLoc}));
true
in
match expr.pexp_desc with
Expand Down
4 changes: 2 additions & 2 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ module Completable = struct
| CPId of string list * completionContext
| CPField of contextPath * string
| CPObj of contextPath * string
| CPPipe of contextPath * string
| CPPipe of {contextPath: contextPath; id: string; lhsLoc: Warnings.loc}

type t =
| Cdecorator of string (** e.g. @module *)
Expand Down Expand Up @@ -513,7 +513,7 @@ module Completable = struct
completionContextToString completionContext ^ list sl
| CPField (cp, s) -> contextPathToString cp ^ "." ^ str s
| CPObj (cp, s) -> contextPathToString cp ^ "[\"" ^ s ^ "\"]"
| CPPipe (cp, s) -> contextPathToString cp ^ "->" ^ s
| CPPipe {contextPath; id} -> contextPathToString contextPath ^ "->" ^ id
in
function
| Cpath cp -> "Cpath " ^ contextPathToString cp
Expand Down
4 changes: 4 additions & 0 deletions analysis/tests/src/CompletionPipeChain.res
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ let f = int->Integer.increment(2)
let _ = [123]->Js.Array2.forEach(v => Js.log(v))
// ->
// ^com

let _ = [123]->Belt.Array.reduce(0, (acc, curr) => acc + curr)
// ->
// ^com
53 changes: 53 additions & 0 deletions analysis/tests/src/expected/CompletionPipeChain.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,56 @@ posCursor:[58:5] posNoWhite:[58:4] Found expr:[57:8->0:-1]
Completable: Cpath Value[Js, Array2, forEach](Nolabel, Nolabel)->
[]

Complete src/CompletionPipeChain.res 62:5
posCursor:[62:5] posNoWhite:[62:4] Found expr:[61:8->0:-1]
Completable: Cpath Value[Belt, Array, reduce](Nolabel, Nolabel, Nolabel)->
[{
"label": "Belt.Int.fromString",
"kind": 12,
"tags": [],
"detail": "string => option<int>",
"documentation": {"kind": "markdown", "value": "\n Converts a given `string` to an `int`. Returns `Some(int)` when the input is a number, `None` otherwise.\n\n ```res example\n Js.log(Belt.Int.fromString(\"1\") === Some(1)) /* true */\n ```\n"}
}, {
"label": "Belt.Int.*",
"kind": 12,
"tags": [],
"detail": "(int, int) => int",
"documentation": {"kind": "markdown", "value": "\n Multiplication of two `int` values. Same as the multiplication from `Pervasives`.\n\n ```res example\n open Belt.Int\n Js.log(2 * 2 === 4) /* true */\n ```\n"}
}, {
"label": "Belt.Int./",
"kind": 12,
"tags": [],
"detail": "(int, int) => int",
"documentation": {"kind": "markdown", "value": "\n Division of two `int` values. Same as the division from `Pervasives`.\n\n ```res example\n open Belt.Int\n Js.log(4 / 2 === 2); /* true */\n ```\n"}
}, {
"label": "Belt.Int.toString",
"kind": 12,
"tags": [],
"detail": "int => string",
"documentation": {"kind": "markdown", "value": "\n Converts a given `int` to a `string`. Uses the JavaScript `String` constructor under the hood.\n\n ```res example\n Js.log(Belt.Int.toString(1) === \"1\") /* true */\n ```\n"}
}, {
"label": "Belt.Int.toFloat",
"kind": 12,
"tags": [],
"detail": "int => float",
"documentation": {"kind": "markdown", "value": "\n Converts a given `int` to a `float`.\n\n ```res example\n Js.log(Belt.Int.toFloat(1) === 1.0) /* true */\n ```\n"}
}, {
"label": "Belt.Int.fromFloat",
"kind": 12,
"tags": [],
"detail": "float => int",
"documentation": {"kind": "markdown", "value": "\n Converts a given `float` to an `int`.\n\n ```res example\n Js.log(Belt.Int.fromFloat(1.0) === 1) /* true */\n ```\n"}
}, {
"label": "Belt.Int.-",
"kind": 12,
"tags": [],
"detail": "(int, int) => int",
"documentation": {"kind": "markdown", "value": "\n Subtraction of two `int` values. Same as the subtraction from `Pervasives`.\n\n ```res example\n open Belt.Int\n Js.log(2 - 1 === 1) /* true */\n ```\n"}
}, {
"label": "Belt.Int.+",
"kind": 12,
"tags": [],
"detail": "(int, int) => int",
"documentation": {"kind": "markdown", "value": "\n Addition of two `int` values. Same as the addition from `Pervasives`.\n\n ```res example\n open Belt.Int\n Js.log(2 + 2 === 4) /* true */\n ```\n"}
}]