Skip to content

Commit

Permalink
pipe complete only for functions that take the expected type as the f…
Browse files Browse the repository at this point in the history
…irst argument
  • Loading branch information
zth committed Nov 16, 2024
1 parent 88a20bc commit a881b26
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
5 changes: 1 addition & 4 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
TypeUtils.getExtraModuleToCompleteFromForType typ
~env:envFromCompletionItem ~full
in
let tPath = TypeUtils.pathFromTypeExpr typ in
let env, typ =
typ
|> TypeUtils.resolveTypeForPipeCompletion ~env ~package ~full ~lhsLoc
Expand All @@ -1123,10 +1124,6 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
(QueryEnv.toString env)
(QueryEnv.toString envFromCompletionItem)
else Printf.printf "CPPipe env:%s\n" (QueryEnv.toString env);
let tPath =
match typ with
| Builtin (_, t) | TypExpr t -> TypeUtils.pathFromTypeExpr t
in
let completionPath =
match typ with
| Builtin (builtin, _) ->
Expand Down
11 changes: 10 additions & 1 deletion analysis/src/TypeUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,16 @@ let rec fnTakesTypeAsFirstArg ~env ~full ~path t =
let p = pathFromTypeExpr t in
match p with
| None -> false
| Some p -> Path.same p path || Path.name p = "t")
| Some p ->
(*
Rules:
- The path p of the current type in the module we're looking at is relative to the current module.
- The path we're comparing against, `path`, is assumed to belong to this current module, because we're completing from it.
Therefore, we can safely pluck out just the last part of the `path`, but need to use the entire name of the current type
we're comparing with.
*)
Path.name p = Path.last path || Path.name p = "t")
| _ -> false)
| _ -> false

Expand Down
9 changes: 9 additions & 0 deletions analysis/tests/src/CompletionPipeChain.res
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,12 @@ let r = %re("/t/g")

// r->la
// ^com

module Xyz = {
type xx = One
let do = (_: xx) => ""
}

let xx = Xyz.One
// xx->
// ^com
8 changes: 3 additions & 5 deletions analysis/tests/src/CompletionPipeSubmodules.res
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module A = {
module B1 = {
type b1 = B1
type t = b1 // TODO(pipe-filter) Should be allowed without needing type t
let xx = B1
let d = (_: t) => ""
let d = (_: b1) => ""
}
module B2 = {
let yy = 20
}
type t = {v: B1.t} // TODO(pipe-filter) Should be allowed without needing type t
type t2 = {v: B1.b1}
let x = {v: B1.B1}
}

Expand All @@ -28,8 +27,7 @@ module C = {
module D = {
module C2 = {
type t2 = C2
type t = t2 // TODO(pipe-filter) Should be allowed without needing type t
let do = (_: t) => ""
let do = (_: t2) => ""
}

type d = {v: C.t, v2: C2.t2}
Expand Down
20 changes: 20 additions & 0 deletions analysis/tests/src/expected/CompletionPipeChain.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,23 @@ Path Js.Re.la
"documentation": {"kind": "markdown", "value": "\nReturns the index where the next match will start its search. This property\nwill be modified when the RegExp object is used, if the global (\"g\") flag is\nset.\n\n## Examples\n\n```rescript\nlet re = %re(\"/ab*TODO/g\")\nlet str = \"abbcdefabh\"\n\nlet break = ref(false)\nwhile !break.contents {\n switch Js.Re.exec_(re, str) {\n | Some(result) => Js.Nullable.iter(Js.Re.captures(result)[0], (. match_) => {\n let next = Belt.Int.toString(Js.Re.lastIndex(re))\n Js.log(\"Found \" ++ (match_ ++ (\". Next match starts at \" ++ next)))\n })\n | None => break := true\n }\n}\n```\n\nSee\n[`RegExp: lastIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex)\non MDN.\n"}
}]

Complete src/CompletionPipeChain.res 112:7
posCursor:[112:7] posNoWhite:[112:6] Found expr:[112:3->0:-1]
Completable: Cpath Value[xx]->
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath Value[xx]->
ContextPath Value[xx]
Path xx
CPPipe env:CompletionPipeChain
CPPipe type path:Xyz.xx
CPPipe pathFromEnv:Xyz found:true
Path Xyz.
[{
"label": "Xyz.do",
"kind": 12,
"tags": [],
"detail": "xx => string",
"documentation": null
}]

25 changes: 12 additions & 13 deletions analysis/tests/src/expected/CompletionPipeSubmodules.res.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Complete src/CompletionPipeSubmodules.res 14:20
posCursor:[14:20] posNoWhite:[14:19] Found expr:[14:11->22:8]
Complete src/CompletionPipeSubmodules.res 13:20
posCursor:[13:20] posNoWhite:[13:19] Found expr:[13:11->21:8]
Completable: Cpath Value[A, B1, xx]->
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
Expand All @@ -14,34 +14,33 @@ Path A.B1.
"label": "A.B1.d",
"kind": 12,
"tags": [],
"detail": "t => string",
"detail": "b1 => string",
"documentation": null
}]

Complete src/CompletionPipeSubmodules.res 18:18
posCursor:[18:18] posNoWhite:[18:17] Found expr:[18:11->22:8]
Complete src/CompletionPipeSubmodules.res 17:18
posCursor:[17:18] posNoWhite:[17:17] Found expr:[17:11->21:8]
Completable: Cpath Value[A, x].v->
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath Value[A, x].v->
ContextPath Value[A, x].v
ContextPath Value[A, x]
Path A.x
Path v
CPPipe env:CompletionPipeSubmodules envFromCompletionItem:CompletionPipeSubmodules.A
CPPipe type path:B1.t
CPPipe type path:B1.b1
CPPipe pathFromEnv:A.B1 found:true
Path A.B1.
[{
"label": "A.B1.d",
"kind": 12,
"tags": [],
"detail": "t => string",
"detail": "b1 => string",
"documentation": null
}]

Complete src/CompletionPipeSubmodules.res 43:20
posCursor:[43:20] posNoWhite:[43:19] Found expr:[43:11->0:-1]
Complete src/CompletionPipeSubmodules.res 41:20
posCursor:[41:20] posNoWhite:[41:19] Found expr:[41:11->0:-1]
Completable: Cpath Value[E, e].v.v->
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
Expand All @@ -62,8 +61,8 @@ Path C.
"documentation": null
}]

Complete src/CompletionPipeSubmodules.res 47:21
posCursor:[47:21] posNoWhite:[47:20] Found expr:[47:11->0:-1]
Complete src/CompletionPipeSubmodules.res 45:21
posCursor:[45:21] posNoWhite:[45:20] Found expr:[45:11->0:-1]
Completable: Cpath Value[E, e].v.v2->
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
Expand All @@ -80,7 +79,7 @@ Path D.C2.
"label": "D.C2.do",
"kind": 12,
"tags": [],
"detail": "t => string",
"detail": "t2 => string",
"documentation": null
}]

0 comments on commit a881b26

Please sign in to comment.