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 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
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

<p align="center">The Official VSCode plugin for ReScript</p>

<p align="center">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about these changes?

<a href="https://marketplace.visualstudio.com/items?itemName=chenglou92.rescript-vscode">
<img src="https://vsmarketplacebadge.apphb.com/version/chenglou92.rescript-vscode.svg"/>
<img src="https://vsmarketplacebadge.apphb.com/installs/chenglou92.rescript-vscode.svg"/>
</a>
</p>

<p align="center">
<img src="https://user-images.githubusercontent.com/1909539/101266821-790b1400-3707-11eb-8e9f-fb7e36e660e6.gif"/>
</p>
Expand Down
6 changes: 3 additions & 3 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ let getCompletions ~debug ~path ~pos ~currentFile ~forHover =
(* Only perform expensive ast operations if there are completables *)
match Cmt.loadFullCmtFromPath ~path with
| None -> []
| Some {file; package} ->
let env = SharedTypes.QueryEnv.fromFile file in
| Some full ->
let env = SharedTypes.QueryEnv.fromFile full.file in
completable
|> CompletionBackEnd.processCompletable ~debug ~package ~pos ~scope ~env
|> CompletionBackEnd.processCompletable ~debug ~full ~pos ~scope ~env
~forHover))

let completion ~debug ~path ~pos ~currentFile =
Expand Down
54 changes: 39 additions & 15 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,17 @@ let completionsGetTypeEnv = function
| {Completion.kind = Field ({typ}, _); env} :: _ -> Some (typ, env)
| _ -> None

let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
~env ~exact ~scope (contextPath : Completable.contextPath) =
let findReturnTypeOfFunctionAtLoc loc ~(env : QueryEnv.t) ~full ~debug =
match References.getLocItem ~full ~pos:(loc |> Loc.end_) ~debug with
| Some {locType = Typed (_, typExpr, _)} -> (
match extractFunctionType ~env ~package:full.package typExpr with
| args, tRet when args <> [] -> Some tRet
| _ -> None)
| _ -> None

let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
~exact ~scope (contextPath : Completable.contextPath) =
let package = full.package in
match contextPath with
| CPString ->
[
Expand All @@ -1181,8 +1190,8 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
| CPApply (cp, labels) -> (
match
cp
|> getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
~env ~exact:true ~scope
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
~exact:true ~scope
|> completionsGetTypeEnv
with
| Some (typ, env) -> (
Expand Down Expand Up @@ -1227,8 +1236,8 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
| CPField (cp, fieldName) -> (
match
cp
|> getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
~env ~exact:true ~scope
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
~exact:true ~scope
|> completionsGetTypeEnv
with
| Some (typ, env) -> (
Expand All @@ -1250,8 +1259,8 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
| CPObj (cp, label) -> (
match
cp
|> getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
~env ~exact:true ~scope
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
~exact:true ~scope
|> completionsGetTypeEnv
with
| Some (typ, env) -> (
Expand All @@ -1275,14 +1284,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
|> getCompletionsForContextPath ~full ~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 ~full ~debug:false
with
| None -> typ
| Some typFromLoc -> typFromLoc)
| _ -> typ
in
let {
arrayModulePath;
optionModulePath;
Expand Down Expand Up @@ -1418,8 +1441,9 @@ let getOpens ~debug ~rawOpens ~package ~env =
(* Last open takes priority *)
List.rev resolvedOpens

let processCompletable ~debug ~package ~scope ~env ~pos ~forHover
let processCompletable ~debug ~full ~scope ~env ~pos ~forHover
(completable : Completable.t) =
let package = full.package in
let rawOpens = Scope.getRawOpens scope in
let opens = getOpens ~debug ~rawOpens ~package ~env in
let allFiles = FileSet.union package.projectFiles package.dependenciesFiles in
Expand All @@ -1433,8 +1457,8 @@ let processCompletable ~debug ~package ~scope ~env ~pos ~forHover
| Cnone -> []
| Cpath contextPath ->
contextPath
|> getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
~env ~exact:forHover ~scope
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
~exact:forHover ~scope
| Cjsx ([id], prefix, identsSeen) when String.uncapitalize_ascii id = id ->
let mkLabel (name, typString) =
Completion.create ~name ~kind:(Label typString) ~env
Expand Down Expand Up @@ -1783,7 +1807,7 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
let labels =
match
cp
|> getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos
~env ~exact:true ~scope
|> completionsGetTypeEnv
with
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
7 changes: 4 additions & 3 deletions analysis/src/Hover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
(* Only perform expensive ast operations if there are completables *)
match Cmt.loadFullCmtFromPath ~path with
| None -> None
| Some {file; package} -> (
| Some full -> (
let {file; package} = full in
let env = SharedTypes.QueryEnv.fromFile file in
let completions =
completable
|> CompletionBackEnd.processCompletable ~debug ~package ~pos ~scope
~env ~forHover
|> CompletionBackEnd.processCompletable ~debug ~full ~pos ~scope ~env
~forHover
in
match completions with
| {kind = Label typString; docstring} :: _ ->
Expand Down
9 changes: 7 additions & 2 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,12 @@ 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: Location.t;
(** The loc item for the left hand side of the pipe. *)
}

type t =
| Cdecorator of string (** e.g. @module *)
Expand Down Expand Up @@ -513,7 +518,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
7 changes: 4 additions & 3 deletions analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ let findFunctionType ~currentFile ~debug ~path ~pos =
| Some (completable, scope) -> (
match Cmt.loadFullCmtFromPath ~path with
| None -> None
| Some {file; package} ->
| Some full ->
let {file; package} = full in
let env = QueryEnv.fromFile file in
Some
( completable
|> CompletionBackEnd.processCompletable ~debug ~package ~pos
~scope ~env ~forHover:true,
|> CompletionBackEnd.processCompletable ~debug ~full ~pos ~scope
~env ~forHover:true,
env,
package,
file )))
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)
// ->t
// ^com
17 changes: 17 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,20 @@ 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:6
posCursor:[62:6] posNoWhite:[62:5] Found expr:[61:8->62:6]
Completable: Cpath Value[Belt, Array, reduce](Nolabel, Nolabel, Nolabel)->t
[{
"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"}
}]