Skip to content

Commit d01a08c

Browse files
authored
Fix issue where completion inside of switch expr would not work (#936)
* fix issue where completion inside of switch expr would not work * changelog * fix
1 parent 45f5dbe commit d01a08c

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
## master
1414

15+
#### :bug: Bug Fix
16+
17+
- Fix issue where completion inside of switch expression would not work in some cases. https://github.com/rescript-lang/rescript-vscode/pull/936
18+
1519
## 1.40.0
1620

1721
#### :nail_care: Polish

analysis/src/CompletionFrontEnd.ml

+28-7
Original file line numberDiff line numberDiff line change
@@ -549,18 +549,26 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
549549
let inJsxContext = ref false in
550550
(* Identifies expressions where we can do typed pattern or expr completion. *)
551551
let typedCompletionExpr (exp : Parsetree.expression) =
552-
if exp.pexp_loc |> CursorPosition.locHasCursor ~pos:posBeforeCursor then
552+
if exp.pexp_loc |> CursorPosition.locHasCursor ~pos:posBeforeCursor then (
553+
if Debug.verbose () then print_endline "[typedCompletionExpr] Has cursor";
553554
match exp.pexp_desc with
554555
(* No cases means there's no `|` yet in the switch *)
555-
| Pexp_match (({pexp_desc = Pexp_ident _} as expr), []) -> (
556-
if locHasCursor expr.pexp_loc then
556+
| Pexp_match (({pexp_desc = Pexp_ident _} as expr), []) ->
557+
if Debug.verbose () then
558+
print_endline "[typedCompletionExpr] No cases, with ident";
559+
if locHasCursor expr.pexp_loc then (
560+
if Debug.verbose () then
561+
print_endline "[typedCompletionExpr] No cases - has cursor";
557562
(* We can do exhaustive switch completion if this is an ident we can
558563
complete from. *)
559564
match exprToContextPath expr with
560565
| None -> ()
561566
| Some contextPath ->
562567
setResult (CexhaustiveSwitch {contextPath; exprLoc = exp.pexp_loc}))
563-
| Pexp_match (_expr, []) -> ()
568+
| Pexp_match (_expr, []) ->
569+
if Debug.verbose () then
570+
print_endline "[typedCompletionExpr] No cases, rest";
571+
()
564572
| Pexp_match
565573
( exp,
566574
[
@@ -586,11 +594,16 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
586594
patternMode = Default;
587595
}))
588596
| Pexp_match (exp, cases) -> (
597+
if Debug.verbose () then print_endline "[typedCompletionExpr] Has cases";
589598
(* If there's more than one case, or the case isn't a pattern hole, figure out if we're completing another
590599
broken parser case (`switch x { | true => () | <com> }` for example). *)
591600
match exp |> exprToContextPath with
592-
| None -> ()
601+
| None ->
602+
if Debug.verbose () then
603+
print_endline "[typedCompletionExpr] Has cases - no ctx path"
593604
| Some ctxPath -> (
605+
if Debug.verbose () then
606+
print_endline "[typedCompletionExpr] Has cases - has ctx path";
594607
let hasCaseWithCursor =
595608
cases
596609
|> List.find_opt (fun case ->
@@ -603,6 +616,11 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
603616
locIsEmpty case.Parsetree.pc_lhs.ppat_loc)
604617
|> Option.is_some
605618
in
619+
if Debug.verbose () then
620+
Printf.printf
621+
"[typedCompletionExpr] Has cases - has ctx path - \
622+
hasCaseWithEmptyLoc: %b, hasCaseWithCursor: %b\n"
623+
hasCaseWithEmptyLoc hasCaseWithCursor;
606624
match (hasCaseWithEmptyLoc, hasCaseWithCursor) with
607625
| _, true ->
608626
(* Always continue if there's a case with the cursor *)
@@ -619,7 +637,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
619637
patternMode = Default;
620638
})
621639
| false, false -> ()))
622-
| _ -> ()
640+
| _ -> ())
623641
in
624642
let structure (iterator : Ast_iterator.iterator)
625643
(structure : Parsetree.structure) =
@@ -977,7 +995,10 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
977995
in
978996
typedCompletionExpr expr;
979997
match expr.pexp_desc with
980-
| Pexp_match (expr, cases) when cases <> [] ->
998+
| Pexp_match (expr, cases)
999+
when cases <> [] && locHasCursor expr.pexp_loc = false ->
1000+
if Debug.verbose () then
1001+
print_endline "[completionFrontend] Checking each case";
9811002
let ctxPath = exprToContextPath expr in
9821003
let oldCtxPath = !currentCtxPath in
9831004
cases

analysis/tests/src/CompletionExpressions.res

+7-2
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,17 @@ module Money: {
348348
let plus = (m1, _) => m1
349349
}
350350

351-
let tArgCompletionTestFn = (tVal: Money.t) => ()
351+
let tArgCompletionTestFn = (_tVal: Money.t) => ()
352352

353353
// tArgCompletionTestFn()
354354
// ^com
355355

356-
let labeledTArgCompletionTestFn = (~tVal: Money.t) => ()
356+
let labeledTArgCompletionTestFn = (~tVal as _: Money.t) => ()
357357

358358
// labeledTArgCompletionTestFn(~tVal=)
359359
// ^com
360+
361+
let someTyp: someTyp = {test: true}
362+
363+
// switch someTyp. { | _ => () }
364+
// ^com

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

+19
Original file line numberDiff line numberDiff line change
@@ -1404,3 +1404,22 @@ Path labeledTArgCompletionTestFn
14041404
"insertTextFormat": 2
14051405
}]
14061406

1407+
Complete src/CompletionExpressions.res 362:18
1408+
posCursor:[362:18] posNoWhite:[362:17] Found expr:[362:3->362:32]
1409+
posCursor:[362:18] posNoWhite:[362:17] Found expr:[362:10->362:18]
1410+
Pexp_field [362:10->362:17] _:[362:19->362:18]
1411+
Completable: Cpath Value[someTyp].""
1412+
Raw opens: 1 CompletionSupport.place holder
1413+
Package opens Pervasives.JsxModules.place holder
1414+
Resolved opens 2 pervasives CompletionSupport.res
1415+
ContextPath Value[someTyp].""
1416+
ContextPath Value[someTyp]
1417+
Path someTyp
1418+
[{
1419+
"label": "test",
1420+
"kind": 5,
1421+
"tags": [],
1422+
"detail": "test: bool\n\ntype someTyp = {test: bool}",
1423+
"documentation": null
1424+
}]
1425+

0 commit comments

Comments
 (0)