Skip to content

Commit a33c4f0

Browse files
committed
handle completing root switch case when theres more than one case
1 parent f22cc81 commit a33c4f0

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

analysis/src/CompletionFrontEnd.ml

+25-3
Original file line numberDiff line numberDiff line change
@@ -705,13 +705,35 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
705705
| Some ctxPath ->
706706
setResult
707707
(Completable.Cpattern {typ = ctxPath; nested = []; prefix = ""}))
708-
| Pexp_match (exp, _cases) -> (
709-
(* If there's more than one case, or the case isn't a pattern hole, set that we're looking for this path currently. *)
708+
| Pexp_match (exp, cases) -> (
709+
(* If there's more than one case, or the case isn't a pattern hole, figure out if we're completing another
710+
broken parser case (`switch x { | true => () | <com> }` for example). *)
710711
match exp |> exprToContextPath with
711712
| None -> ()
712-
| Some ctxPath -> setLookingForPat ctxPath)
713+
| Some ctxPath -> (
714+
let caseWithCursor =
715+
cases
716+
|> List.find_opt (fun case ->
717+
case.Parsetree.pc_lhs.ppat_loc
718+
|> CursorPosition.classifyLoc ~pos:posBeforeCursor
719+
= HasCursor)
720+
in
721+
let caseWithPatHole =
722+
cases
723+
|> List.find_opt (fun case -> isPatternHole case.Parsetree.pc_lhs)
724+
in
725+
match (caseWithPatHole, caseWithCursor) with
726+
| _, Some _ ->
727+
(* Always continue if there's a case with the cursor *)
728+
setLookingForPat ctxPath
729+
| Some _, None ->
730+
(* If there's no case with the cursor, but a broken parser case, complete for the top level. *)
731+
setResult
732+
(Completable.Cpattern {typ = ctxPath; nested = []; prefix = ""})
733+
| None, None -> ()))
713734
| _ -> unsetLookingForPat ()
714735
in
736+
715737
let case (iterator : Ast_iterator.iterator) (case : Parsetree.case) =
716738
let oldScope = !scope in
717739
scopePattern case.pc_lhs;

analysis/tests/src/CompletionPattern.res

+6
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,9 @@ let s = (true, Some(true), [false])
170170

171171
// switch s { | (true, , []) }
172172
// ^com
173+
174+
// switch s { | (true, []) => () | }
175+
// ^com
176+
177+
// switch s { | (true, []) => () | (true, , []) }
178+
// ^com

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

-1
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,6 @@ Resolved opens 2 Completion.res Completion.res
965965
[]
966966

967967
Complete src/Completion.res 243:8
968-
looking for: Cpath Value[someR]
969968
posCursor:[243:8] posNoWhite:[243:7] Found expr:[241:8->246:1]
970969
posCursor:[243:8] posNoWhite:[243:7] Found expr:[242:14->243:8]
971970
Pexp_apply ...[243:3->243:4] (...[242:14->242:15], ...[243:5->243:8])

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

+34-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ Completable: Cpattern Value[f]->recordField(nest), recordBody
198198
}]
199199

200200
Complete src/CompletionPattern.res 70:22
201-
looking for: Cpath Value[f]
202201
posCursor:[70:22] posNoWhite:[70:21] Found expr:[67:8->74:1]
203202
posCursor:[70:22] posNoWhite:[70:21] Found expr:[69:2->72:13]
204203
posCursor:[70:22] posNoWhite:[70:21] Found expr:[70:5->72:13]
@@ -690,3 +689,37 @@ Completable: Cpattern Value[s]->tuple($1)
690689
"insertTextFormat": 2
691690
}]
692691

692+
Complete src/CompletionPattern.res 173:35
693+
XXX Not found!
694+
Completable: Cpattern Value[s]
695+
[{
696+
"label": "(_, _, _)",
697+
"kind": 12,
698+
"tags": [],
699+
"detail": "(bool, option<bool>, array<bool>)",
700+
"documentation": null,
701+
"insertText": "(${1:_}, ${2:_}, ${3:_})",
702+
"insertTextFormat": 2
703+
}]
704+
705+
Complete src/CompletionPattern.res 176:41
706+
looking for: Cpath Value[s]
707+
posCursor:[176:41] posNoWhite:[176:40] Found expr:[176:3->176:50]
708+
posCursor:[176:41] posNoWhite:[176:40] Found pattern:[176:35->176:47]
709+
Completable: Cpattern Value[s]->tuple($1)
710+
[{
711+
"label": "None",
712+
"kind": 4,
713+
"tags": [],
714+
"detail": "bool",
715+
"documentation": null
716+
}, {
717+
"label": "Some(_)",
718+
"kind": 4,
719+
"tags": [],
720+
"detail": "bool",
721+
"documentation": null,
722+
"insertText": "Some(${1:_})",
723+
"insertTextFormat": 2
724+
}]
725+

0 commit comments

Comments
 (0)