Skip to content

Commit 1932703

Browse files
committed
complete pipe chains
1 parent 2f836d0 commit 1932703

File tree

3 files changed

+2036
-2
lines changed

3 files changed

+2036
-2
lines changed

analysis/src/CompletionFrontEnd.ml

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,46 @@ let rec exprToContextPath (e : Parsetree.expression) =
157157
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
158158
| _ -> None
159159

160+
let completePipeChain ~(lhs : Parsetree.expression) =
161+
(* Complete the end of pipe chains by reconstructing the pipe chain as a single pipe,
162+
so it can be completed.
163+
Example:
164+
someArray->Js.Array2.filter(v => v > 10)->Js.Array2.map(v => v + 2)->
165+
will complete as:
166+
Js.Array2.map(someArray->Js.Array2.filter(v => v > 10), v => v + 2)->
167+
*)
168+
match lhs.pexp_desc with
169+
(* When the left side of the pipe we're completing is a function application.
170+
Example: someArray->Js.Array2.map(v => v + 2)-> *)
171+
| Pexp_apply
172+
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
173+
[
174+
(_, lhs);
175+
(_, {pexp_desc = Pexp_apply (d, args); pexp_loc; pexp_attributes});
176+
] ) ->
177+
exprToContextPath
178+
{
179+
pexp_desc = Pexp_apply (d, (Nolabel, lhs) :: args);
180+
pexp_loc;
181+
pexp_attributes;
182+
}
183+
(* When the left side of the pipe we're completing is an identifier application.
184+
Example: someArray->filterAllTheGoodStuff-> *)
185+
| Pexp_apply
186+
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
187+
[(_, lhs); (_, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes})]
188+
) ->
189+
exprToContextPath
190+
{
191+
pexp_desc =
192+
Pexp_apply
193+
( {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes},
194+
[(Nolabel, lhs)] );
195+
pexp_loc;
196+
pexp_attributes;
197+
}
198+
| _ -> None
199+
160200
let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
161201
let offsetNoWhite = skipWhite text (offset - 1) in
162202
let posNoWhite =
@@ -392,11 +432,16 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
392432
(Loc.toString expr.pexp_loc)
393433
in
394434
let setPipeResult ~(lhs : Parsetree.expression) ~id =
395-
match exprToContextPath lhs with
435+
match completePipeChain ~lhs with
436+
| None -> (
437+
match exprToContextPath lhs with
438+
| Some pipe ->
439+
setResult (Cpath (CPPipe (pipe, id)));
440+
true
441+
| None -> false)
396442
| Some pipe ->
397443
setResult (Cpath (CPPipe (pipe, id)));
398444
true
399-
| None -> false
400445
in
401446
match expr.pexp_desc with
402447
| Pexp_apply
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
let mapArr = arr => arr->Js.Array2.map(v => v + 2)
2+
let arr = [1, 2, 3]
3+
// let _ = arr->
4+
// ^com
5+
6+
// let _ = arr->Js.Array2.map(v => v)->
7+
// ^com
8+
9+
// let _ = arr->Js.Array2.filter(v => v)->Js.Array2.filter(v => v)->
10+
// ^com
11+
12+
// let _ = arr->Js.Array2.filter(v => v)->
13+
// ^com
14+
15+
// let _ = arr->Js.Array2-map(v => v)->Belt.List.fromArray->
16+
// ^com
17+
18+
// let _ = arr->Js.Array2-map(v => v)->Belt.List.fromArray->s
19+
// ^com

0 commit comments

Comments
 (0)