Skip to content

Commit a82a072

Browse files
authored
Fix issue with unlabelled arg code swallowing completions (#937)
* tighten check when completing for unlabelled argument * changelog
1 parent 54c7411 commit a82a072

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
#### :bug: Bug Fix
1818

19+
- Fix issue with unlabelled arg code swallowing completions. https://github.com/rescript-lang/rescript-vscode/pull/937
20+
21+
#### :bug: Bug Fix
22+
1923
- Fix issue where completion inside of switch expression would not work in some cases. https://github.com/rescript-lang/rescript-vscode/pull/936
2024
- Fix bug that made empty prop expressions in JSX not complete if in the middle of a JSX element. https://github.com/rescript-lang/rescript-vscode/pull/935
2125

analysis/src/CompletionFrontEnd.ml

+18-10
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ let findArgCompletables ~(args : arg list) ~endPos ~posBeforeCursor
155155
"[findArgCompletables] skipping completion in fn call because \
156156
arg had empty loc";
157157
None
158-
| _ ->
158+
| _
159+
when firstCharBeforeCursorNoWhite = Some '('
160+
|| firstCharBeforeCursorNoWhite = Some ',' ->
161+
(* Checks to ensure that completing for empty unlabelled arg makes
162+
sense by checking what's left of the cursor. *)
159163
if Debug.verbose () then
160164
Printf.printf
161165
"[findArgCompletables] Completing for unlabelled argument value \
@@ -174,7 +178,8 @@ let findArgCompletables ~(args : arg list) ~endPos ~posBeforeCursor
174178
};
175179
prefix = "";
176180
nested = [];
177-
}))
181+
})
182+
| _ -> None)
178183
else None
179184
in
180185
match args with
@@ -549,15 +554,17 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
549554
let inJsxContext = ref false in
550555
(* Identifies expressions where we can do typed pattern or expr completion. *)
551556
let typedCompletionExpr (exp : Parsetree.expression) =
557+
let debugTypedCompletionExpr = false in
552558
if exp.pexp_loc |> CursorPosition.locHasCursor ~pos:posBeforeCursor then (
553-
if Debug.verbose () then print_endline "[typedCompletionExpr] Has cursor";
559+
if Debug.verbose () && debugTypedCompletionExpr then
560+
print_endline "[typedCompletionExpr] Has cursor";
554561
match exp.pexp_desc with
555562
(* No cases means there's no `|` yet in the switch *)
556563
| Pexp_match (({pexp_desc = Pexp_ident _} as expr), []) ->
557-
if Debug.verbose () then
564+
if Debug.verbose () && debugTypedCompletionExpr then
558565
print_endline "[typedCompletionExpr] No cases, with ident";
559566
if locHasCursor expr.pexp_loc then (
560-
if Debug.verbose () then
567+
if Debug.verbose () && debugTypedCompletionExpr then
561568
print_endline "[typedCompletionExpr] No cases - has cursor";
562569
(* We can do exhaustive switch completion if this is an ident we can
563570
complete from. *)
@@ -566,7 +573,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
566573
| Some contextPath ->
567574
setResult (CexhaustiveSwitch {contextPath; exprLoc = exp.pexp_loc}))
568575
| Pexp_match (_expr, []) ->
569-
if Debug.verbose () then
576+
if Debug.verbose () && debugTypedCompletionExpr then
570577
print_endline "[typedCompletionExpr] No cases, rest";
571578
()
572579
| Pexp_match
@@ -594,15 +601,16 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
594601
patternMode = Default;
595602
}))
596603
| Pexp_match (exp, cases) -> (
597-
if Debug.verbose () then print_endline "[typedCompletionExpr] Has cases";
604+
if Debug.verbose () && debugTypedCompletionExpr then
605+
print_endline "[typedCompletionExpr] Has cases";
598606
(* If there's more than one case, or the case isn't a pattern hole, figure out if we're completing another
599607
broken parser case (`switch x { | true => () | <com> }` for example). *)
600608
match exp |> exprToContextPath with
601609
| None ->
602-
if Debug.verbose () then
610+
if Debug.verbose () && debugTypedCompletionExpr then
603611
print_endline "[typedCompletionExpr] Has cases - no ctx path"
604612
| Some ctxPath -> (
605-
if Debug.verbose () then
613+
if Debug.verbose () && debugTypedCompletionExpr then
606614
print_endline "[typedCompletionExpr] Has cases - has ctx path";
607615
let hasCaseWithCursor =
608616
cases
@@ -616,7 +624,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
616624
locIsEmpty case.Parsetree.pc_lhs.ppat_loc)
617625
|> Option.is_some
618626
in
619-
if Debug.verbose () then
627+
if Debug.verbose () && debugTypedCompletionExpr then
620628
Printf.printf
621629
"[typedCompletionExpr] Has cases - has ctx path - \
622630
hasCaseWithEmptyLoc: %b, hasCaseWithCursor: %b\n"

analysis/tests/src/CompletionFunctionArguments.res

+8
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,11 @@ let _ =
113113
// ^com
114114
}}
115115
/>
116+
117+
let fineModuleVal = {
118+
FineModule.online: true,
119+
somethingElse: "",
120+
}
121+
122+
// makeItem(~changefreq=Monthly, ~lastmod=fineModuleVal->, ~priority=Low)
123+
// ^com

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

+23
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,26 @@ Path ReactEvent.Mouse.a
418418
"documentation": null
419419
}]
420420

421+
Complete src/CompletionFunctionArguments.res 121:57
422+
posCursor:[121:57] posNoWhite:[121:56] Found expr:[121:3->121:73]
423+
Pexp_apply ...[121:3->121:11] (~changefreq121:13->121:23=...[121:24->121:31], ~lastmod121:34->121:41=...[121:42->0:-1], ~priority121:60->121:68=...[121:69->121:72])
424+
posCursor:[121:57] posNoWhite:[121:56] Found expr:[121:42->0:-1]
425+
posCursor:[121:57] posNoWhite:[121:56] Found expr:[121:42->0:-1]
426+
Completable: Cpath Value[fineModuleVal]->
427+
Package opens Pervasives.JsxModules.place holder
428+
Resolved opens 1 pervasives
429+
ContextPath Value[fineModuleVal]->
430+
ContextPath Value[fineModuleVal]
431+
Path fineModuleVal
432+
CPPipe env:CompletionFunctionArguments
433+
CPPipe type path:FineModule.t
434+
CPPipe pathFromEnv:FineModule found:true
435+
Path FineModule.
436+
[{
437+
"label": "FineModule.setToFalse",
438+
"kind": 12,
439+
"tags": [],
440+
"detail": "t => t",
441+
"documentation": null
442+
}]
443+

0 commit comments

Comments
 (0)