@@ -157,6 +157,46 @@ let rec exprToContextPath (e : Parsetree.expression) =
157
157
| Some contexPath -> Some (CPApply (contexPath, args |> List. map fst)))
158
158
| _ -> None
159
159
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
+
160
200
let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
161
201
let offsetNoWhite = skipWhite text (offset - 1 ) in
162
202
let posNoWhite =
@@ -392,11 +432,16 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
392
432
(Loc. toString expr.pexp_loc)
393
433
in
394
434
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 )
396
442
| Some pipe ->
397
443
setResult (Cpath (CPPipe (pipe, id)));
398
444
true
399
- | None -> false
400
445
in
401
446
match expr.pexp_desc with
402
447
| Pexp_apply
0 commit comments