Skip to content

Commit bd4cfc5

Browse files
committed
Named arg completion should not fire on closing bracket.
1 parent d2b8a9b commit bd4cfc5

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

analysis/src/CompletionFrontEnd.ml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ type labelled = {
132132
type label = labelled option
133133
type arg = {label : label; exp : Parsetree.expression}
134134

135-
let findExpApplyCompletable ~(args : arg list) ~endPos ~posBeforeCursor
135+
let findNamedArgCompletable ~(args : arg list) ~endPos ~posBeforeCursor
136136
~(contextPath : Completable.contextPath) ~posAfterFunExpr =
137137
let allNames =
138138
List.fold_right
@@ -211,7 +211,7 @@ let rec exprToContextPath (e : Parsetree.expression) =
211211
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
212212
| _ -> None
213213

214-
let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
214+
let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
215215
let offset =
216216
match positionToOffset text posCursor with
217217
| Some offset -> offset
@@ -223,17 +223,18 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
223223
(line, max 0 col - offset + offsetNoWhite)
224224
in
225225
let posBeforeCursor = (fst posCursor, max 0 (snd posCursor - 1)) in
226-
let blankAfterCursor =
226+
let charBeforeCursor, blankAfterCursor =
227227
match positionToOffset text posCursor with
228228
| Some offset when offset > 0 -> (
229229
let charBeforeCursor = text.[offset - 1] in
230230
let charAtCursor =
231231
if offset < String.length text then text.[offset] else '\n'
232232
in
233233
match charAtCursor with
234-
| ' ' | '\t' | '\r' | '\n' -> Some charBeforeCursor
235-
| _ -> None)
236-
| _ -> None
234+
| ' ' | '\t' | '\r' | '\n' ->
235+
(Some charBeforeCursor, Some charBeforeCursor)
236+
| _ -> (Some charBeforeCursor, None))
237+
| _ -> (None, None)
237238
in
238239
let flattenLidCheckDot ?(jsx = true) (lid : Longident.t Location.loc) =
239240
(* Flatten an identifier keeping track of whether the current cursor
@@ -559,7 +560,13 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
559560
setPipeResult ~lhs ~id:"" |> ignore
560561
| Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "|."}}, [_; _]) ->
561562
()
562-
| Pexp_apply (funExpr, args) ->
563+
| Pexp_apply (funExpr, args)
564+
when not
565+
(* Normally named arg completion fires when the cursor is right after the expression.
566+
E.g in foo(~<---there
567+
But it should not fire in foo(~a)<---there *)
568+
(Loc.end_ expr.pexp_loc = posCursor
569+
&& charBeforeCursor = Some ')') ->
563570
let args = extractExpApplyArgs ~args in
564571
if debug then
565572
Printf.printf "Pexp_apply ...%s (%s)\n"
@@ -578,7 +585,7 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
578585
let expApplyCompletable =
579586
match exprToContextPath funExpr with
580587
| Some contextPath ->
581-
findExpApplyCompletable ~contextPath ~args
588+
findNamedArgCompletable ~contextPath ~args
582589
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
583590
~posAfterFunExpr:(Loc.end_ funExpr.pexp_loc)
584591
| None -> None

analysis/tests/src/Completion.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,6 @@ let _ = <div name="" />
352352

353353
// let _ = ff(~opt1=3)
354354
// ^hov
355+
356+
// (let _ = ff(~opt1=3))
357+
// ^com

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,3 +1376,8 @@ Found type for function (
13761376
) => int
13771377
{"contents": "```rescript\noption<int>\n```"}
13781378

1379+
Complete tests/src/Completion.res 355:23
1380+
posCursor:[355:23] posNoWhite:[355:22] Found expr:[0:-1->355:23]
1381+
posCursor:[355:23] posNoWhite:[355:22] Found expr:[355:12->355:23]
1382+
[]
1383+

0 commit comments

Comments
 (0)