Skip to content

Commit e8b5231

Browse files
zthcristianoc
authored andcommitted
apply pipe transform to ctx path extraction so pipes can be tracked as context paths
1 parent fec7662 commit e8b5231

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

analysis/src/CompletionFrontEnd.ml

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,33 @@ let rec exprToContextPath (e : Parsetree.expression) =
161161
match exprToContextPath e1 with
162162
| None -> None
163163
| Some contexPath -> Some (CPObj (contexPath, txt)))
164+
| Pexp_apply
165+
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
166+
[
167+
(_, lhs);
168+
(_, {pexp_desc = Pexp_apply (d, args); pexp_loc; pexp_attributes});
169+
] ) ->
170+
(* Transform away pipe with apply call *)
171+
exprToContextPath
172+
{
173+
pexp_desc = Pexp_apply (d, (Nolabel, lhs) :: args);
174+
pexp_loc;
175+
pexp_attributes;
176+
}
177+
| Pexp_apply
178+
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
179+
[(_, lhs); (_, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes})]
180+
) ->
181+
(* Transform away pipe with identifier *)
182+
exprToContextPath
183+
{
184+
pexp_desc =
185+
Pexp_apply
186+
( {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes},
187+
[(Nolabel, lhs)] );
188+
pexp_loc;
189+
pexp_attributes;
190+
}
164191
| Pexp_apply (e1, args) -> (
165192
match exprToContextPath e1 with
166193
| None -> None
@@ -185,33 +212,14 @@ let completePipeChain (exp : Parsetree.expression) =
185212
Example: someArray->Js.Array2.map(v => v + 2)-> *)
186213
| Pexp_apply
187214
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
188-
[
189-
(_, lhs);
190-
(_, {pexp_desc = Pexp_apply (d, args); pexp_loc; pexp_attributes});
191-
] ) ->
192-
exprToContextPath
193-
{
194-
pexp_desc = Pexp_apply (d, (Nolabel, lhs) :: args);
195-
pexp_loc;
196-
pexp_attributes;
197-
}
198-
|> Option.map (fun ctxPath -> (ctxPath, d.pexp_loc))
215+
[_; (_, {pexp_desc = Pexp_apply (d, _)})] ) ->
216+
exprToContextPath exp |> Option.map (fun ctxPath -> (ctxPath, d.pexp_loc))
199217
(* When the left side of the pipe we're completing is an identifier application.
200218
Example: someArray->filterAllTheGoodStuff-> *)
201219
| Pexp_apply
202220
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
203-
[(_, lhs); (_, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes})]
204-
) ->
205-
exprToContextPath
206-
{
207-
pexp_desc =
208-
Pexp_apply
209-
( {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes},
210-
[(Nolabel, lhs)] );
211-
pexp_loc;
212-
pexp_attributes;
213-
}
214-
|> Option.map (fun ctxPath -> (ctxPath, pexp_loc))
221+
[_; (_, {pexp_desc = Pexp_ident _; pexp_loc})] ) ->
222+
exprToContextPath exp |> Option.map (fun ctxPath -> (ctxPath, pexp_loc))
215223
| _ -> None
216224

217225
let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =

analysis/tests/src/CompletionInferValues.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,11 @@ let fn3 = (~cb: sameFileRecord => unit) => {
147147

148148
// fn3(~cb=({root}) => {root-> })
149149
// ^com
150+
151+
// Handles pipe chains as input for switch
152+
// let x = 123; switch x->Belt.Int.toString { | }
153+
// ^com
154+
155+
// Handles pipe chains as input for switch
156+
// let x = 123; switch x->Belt.Int.toString->Js.String2.split("/") { | }
157+
// ^com

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,31 @@ Completable: Cpath Value[root]->
468468
"documentation": null
469469
}]
470470

471+
Complete src/CompletionInferValues.res 151:47
472+
XXX Not found!
473+
Completable: Cpattern Value[Belt, Int, toString](Nolabel)
474+
[{
475+
"label": "\"\"",
476+
"kind": 12,
477+
"tags": [],
478+
"detail": "string",
479+
"documentation": null,
480+
"sortText": "A",
481+
"insertText": "\"$0\"",
482+
"insertTextFormat": 2
483+
}]
484+
485+
Complete src/CompletionInferValues.res 155:70
486+
XXX Not found!
487+
Completable: Cpattern Value[Js, String2, split](Nolabel, Nolabel)
488+
[{
489+
"label": "[]",
490+
"kind": 12,
491+
"tags": [],
492+
"detail": "t",
493+
"documentation": null,
494+
"sortText": "A",
495+
"insertText": "[$0]",
496+
"insertTextFormat": 2
497+
}]
498+

0 commit comments

Comments
 (0)