Skip to content

Commit 5ad0319

Browse files
committed
pipe complete only for functions that take the expected type as the first argument
1 parent ce80c6c commit 5ad0319

File tree

6 files changed

+55
-23
lines changed

6 files changed

+55
-23
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
11131113
TypeUtils.getExtraModuleToCompleteFromForType typ
11141114
~env:envFromCompletionItem ~full
11151115
in
1116+
let tPath = TypeUtils.pathFromTypeExpr typ in
11161117
let env, typ =
11171118
typ
11181119
|> TypeUtils.resolveTypeForPipeCompletion ~env ~package ~full ~lhsLoc
@@ -1123,10 +1124,6 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
11231124
(QueryEnv.toString env)
11241125
(QueryEnv.toString envFromCompletionItem)
11251126
else Printf.printf "CPPipe env:%s\n" (QueryEnv.toString env);
1126-
let tPath =
1127-
match typ with
1128-
| Builtin (_, t) | TypExpr t -> TypeUtils.pathFromTypeExpr t
1129-
in
11301127
let completionPath =
11311128
match typ with
11321129
| Builtin (builtin, _) ->

analysis/src/TypeUtils.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,16 @@ let rec fnTakesTypeAsFirstArg ~env ~full ~path t =
11721172
let p = pathFromTypeExpr t in
11731173
match p with
11741174
| None -> false
1175-
| Some p -> Path.same p path || Path.name p = "t")
1175+
| Some p ->
1176+
(*
1177+
Rules:
1178+
- The path p of the current type in the module we're looking at is relative to the current module.
1179+
- The path we're comparing against, `path`, is assumed to belong to this current module, because we're completing from it.
1180+
1181+
Therefore, we can safely pluck out just the last part of the `path`, but need to use the entire name of the current type
1182+
we're comparing with.
1183+
*)
1184+
Path.name p = Path.last path || Path.name p = "t")
11761185
| _ -> false)
11771186
| _ -> false
11781187

analysis/tests/src/CompletionPipeChain.res

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,12 @@ let r = %re("/t/g")
103103

104104
// r->la
105105
// ^com
106+
107+
module Xyz = {
108+
type xx = One
109+
let do = (_: xx) => ""
110+
}
111+
112+
let xx = Xyz.One
113+
// xx->
114+
// ^com

analysis/tests/src/CompletionPipeSubmodules.res

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
module A = {
22
module B1 = {
33
type b1 = B1
4-
type t = b1 // TODO(pipe-filter) Should be allowed without needing type t
54
let xx = B1
6-
let d = (_: t) => ""
5+
let d = (_: b1) => ""
76
}
87
module B2 = {
98
let yy = 20
109
}
11-
type t = {v: B1.t} // TODO(pipe-filter) Should be allowed without needing type t
10+
type t2 = {v: B1.b1}
1211
let x = {v: B1.B1}
1312
}
1413

@@ -28,8 +27,7 @@ module C = {
2827
module D = {
2928
module C2 = {
3029
type t2 = C2
31-
type t = t2 // TODO(pipe-filter) Should be allowed without needing type t
32-
let do = (_: t) => ""
30+
let do = (_: t2) => ""
3331
}
3432

3533
type d = {v: C.t, v2: C2.t2}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,3 +501,23 @@ Path Js.Re.la
501501
"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"}
502502
}]
503503

504+
Complete src/CompletionPipeChain.res 112:7
505+
posCursor:[112:7] posNoWhite:[112:6] Found expr:[112:3->0:-1]
506+
Completable: Cpath Value[xx]->
507+
Package opens Pervasives.JsxModules.place holder
508+
Resolved opens 1 pervasives
509+
ContextPath Value[xx]->
510+
ContextPath Value[xx]
511+
Path xx
512+
CPPipe env:CompletionPipeChain
513+
CPPipe type path:Xyz.xx
514+
CPPipe pathFromEnv:Xyz found:true
515+
Path Xyz.
516+
[{
517+
"label": "Xyz.do",
518+
"kind": 12,
519+
"tags": [],
520+
"detail": "xx => string",
521+
"documentation": null
522+
}]
523+

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Complete src/CompletionPipeSubmodules.res 14:20
2-
posCursor:[14:20] posNoWhite:[14:19] Found expr:[14:11->22:8]
1+
Complete src/CompletionPipeSubmodules.res 13:20
2+
posCursor:[13:20] posNoWhite:[13:19] Found expr:[13:11->21:8]
33
Completable: Cpath Value[A, B1, xx]->
44
Package opens Pervasives.JsxModules.place holder
55
Resolved opens 1 pervasives
@@ -14,34 +14,33 @@ Path A.B1.
1414
"label": "A.B1.d",
1515
"kind": 12,
1616
"tags": [],
17-
"detail": "t => string",
17+
"detail": "b1 => string",
1818
"documentation": null
1919
}]
2020

21-
Complete src/CompletionPipeSubmodules.res 18:18
22-
posCursor:[18:18] posNoWhite:[18:17] Found expr:[18:11->22:8]
21+
Complete src/CompletionPipeSubmodules.res 17:18
22+
posCursor:[17:18] posNoWhite:[17:17] Found expr:[17:11->21:8]
2323
Completable: Cpath Value[A, x].v->
2424
Package opens Pervasives.JsxModules.place holder
2525
Resolved opens 1 pervasives
2626
ContextPath Value[A, x].v->
2727
ContextPath Value[A, x].v
2828
ContextPath Value[A, x]
2929
Path A.x
30-
Path v
3130
CPPipe env:CompletionPipeSubmodules envFromCompletionItem:CompletionPipeSubmodules.A
32-
CPPipe type path:B1.t
31+
CPPipe type path:B1.b1
3332
CPPipe pathFromEnv:A.B1 found:true
3433
Path A.B1.
3534
[{
3635
"label": "A.B1.d",
3736
"kind": 12,
3837
"tags": [],
39-
"detail": "t => string",
38+
"detail": "b1 => string",
4039
"documentation": null
4140
}]
4241

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

65-
Complete src/CompletionPipeSubmodules.res 47:21
66-
posCursor:[47:21] posNoWhite:[47:20] Found expr:[47:11->0:-1]
64+
Complete src/CompletionPipeSubmodules.res 45:21
65+
posCursor:[45:21] posNoWhite:[45:20] Found expr:[45:11->0:-1]
6766
Completable: Cpath Value[E, e].v.v2->
6867
Package opens Pervasives.JsxModules.place holder
6968
Resolved opens 1 pervasives
@@ -80,7 +79,7 @@ Path D.C2.
8079
"label": "D.C2.do",
8180
"kind": 12,
8281
"tags": [],
83-
"detail": "t => string",
82+
"detail": "t2 => string",
8483
"documentation": null
8584
}]
8685

0 commit comments

Comments
 (0)