Skip to content

Commit eb4ba05

Browse files
committed
handle piped fn calls properly
1 parent 4438ac7 commit eb4ba05

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

analysis/src/CompletionFrontEnd.ml

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ let isExprHole exp =
120120

121121
let findArgCompletables ~(args : arg list) ~endPos ~posBeforeCursor
122122
~(contextPath : Completable.contextPath) ~posAfterFunExpr ~charBeforeCursor
123-
=
123+
~isPipedExpr =
124124
let fnHasCursor =
125125
posAfterFunExpr <= posBeforeCursor && posBeforeCursor < endPos
126126
in
@@ -132,7 +132,7 @@ let findArgCompletables ~(args : arg list) ~endPos ~posBeforeCursor
132132
| {label = None} -> allLabels)
133133
args []
134134
in
135-
let unlabelledCount = ref 0 in
135+
let unlabelledCount = ref (if isPipedExpr then 1 else 0) in
136136
let rec loop args =
137137
match args with
138138
| {label = Some labelled; exp} :: rest ->
@@ -635,15 +635,36 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
635635
when Loc.end_ opLoc = posCursor ->
636636
(* Case foo-> *)
637637
setPipeResult ~lhs ~id:"" |> ignore
638-
| Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "|."}}, [_; _]) ->
639-
()
640-
| Pexp_apply (funExpr, args)
638+
| Pexp_apply
639+
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
640+
[_; (_, {pexp_desc = Pexp_apply (funExpr, args)})] )
641641
when (* Normally named arg completion fires when the cursor is right after the expression.
642642
E.g in foo(~<---there
643643
But it should not fire in foo(~a)<---there *)
644644
not
645645
(Loc.end_ expr.pexp_loc = posCursor
646646
&& charBeforeCursor = Some ')') ->
647+
(* Complete fn argument values and named args when the fn call is piped. E.g. someVar->someFn(<com>). *)
648+
let args = extractExpApplyArgs ~args in
649+
let argCompletable =
650+
match exprToContextPath funExpr with
651+
| Some contextPath ->
652+
findArgCompletables ~contextPath ~args
653+
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
654+
~posAfterFunExpr:(Loc.end_ funExpr.pexp_loc)
655+
~charBeforeCursor ~isPipedExpr:true
656+
| None -> None
657+
in
658+
659+
setResultOpt argCompletable
660+
| Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "|."}}, [_; _]) ->
661+
(* Ignore any other pipe. *)
662+
()
663+
| Pexp_apply (funExpr, args)
664+
when not
665+
(Loc.end_ expr.pexp_loc = posCursor
666+
&& charBeforeCursor = Some ')') ->
667+
(* Complete fn argument values and named args when the fn call is _not_ piped. E.g. someFn(<com>). *)
647668
let args = extractExpApplyArgs ~args in
648669
if debug then
649670
Printf.printf "Pexp_apply ...%s (%s)\n"
@@ -666,7 +687,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
666687
findArgCompletables ~contextPath ~args
667688
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
668689
~posAfterFunExpr:(Loc.end_ funExpr.pexp_loc)
669-
~charBeforeCursor
690+
~charBeforeCursor ~isPipedExpr:false
670691
| None -> None
671692
in
672693

analysis/tests/src/CompletionFunctionArguments.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ let someFnTakingVariant = (
6767
// let _ = someOtherFn(1, 2, )
6868
// ^com
6969

70+
// let _ = 1->someOtherFn(1, t)
71+
// ^com
72+
7073
// --- BROKEN PARSER CASES ---
7174
// This below demonstrates an issue when what you're completing is the _last_ labelled argument, and there's a unit application after it. The parser wrongly merges the unit argument as the expression of the labelled argument assignment, where is should really let the trailing unit argument be, and set a %rescript.exprhole as the expression of the assignment, just like it normally does.
7275
// let _ = someFn(~isOff=, ())

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,26 @@ Completable: Cargument Value[someOtherFn]($2)
194194
"documentation": null
195195
}]
196196

197-
Complete src/CompletionFunctionArguments.res 71:25
198-
posCursor:[71:25] posNoWhite:[71:24] Found expr:[71:11->71:30]
199-
Pexp_apply ...[71:11->71:17] (~isOff71:19->71:24=...[71:27->71:29])
197+
Complete src/CompletionFunctionArguments.res 69:30
198+
posCursor:[69:30] posNoWhite:[69:29] Found expr:[69:11->69:31]
199+
Completable: Cargument Value[someOtherFn]($2=t)
200+
[{
201+
"label": "true",
202+
"kind": 4,
203+
"tags": [],
204+
"detail": "bool",
205+
"documentation": null
206+
}, {
207+
"label": "tLocalVar",
208+
"kind": 12,
209+
"tags": [],
210+
"detail": "bool",
211+
"documentation": null
212+
}]
213+
214+
Complete src/CompletionFunctionArguments.res 74:25
215+
posCursor:[74:25] posNoWhite:[74:24] Found expr:[74:11->74:30]
216+
Pexp_apply ...[74:11->74:17] (~isOff74:19->74:24=...[74:27->74:29])
200217
Completable: Cargument Value[someFn]($0)
201218
[]
202219

0 commit comments

Comments
 (0)