@@ -232,6 +232,7 @@ let detail name (kind : Completion.kind) =
232
232
|> String. concat " , " )
233
233
^ " )" )
234
234
^ " \n\n " ^ s
235
+ | Snippet s -> s
235
236
236
237
let findAllCompletions ~(env : QueryEnv.t ) ~prefix ~exact ~namesUsed
237
238
~(completionContext : Completable.completionContext ) =
@@ -552,6 +553,7 @@ let mkItem ~name ~kind ~detail ~deprecated ~docstring =
552
553
sortText = None ;
553
554
insertText = None ;
554
555
insertTextFormat = None ;
556
+ filterText = None ;
555
557
}
556
558
557
559
let completionToItem
@@ -563,14 +565,15 @@ let completionToItem
563
565
sortText;
564
566
insertText;
565
567
insertTextFormat;
568
+ filterText;
566
569
} =
567
570
let item =
568
571
mkItem ~name
569
572
~kind: (Completion. kindToInt kind)
570
573
~deprecated ~detail: (detail name kind) ~docstring
571
574
in
572
575
if ! Cfg. supportsSnippets then
573
- {item with sortText; insertText; insertTextFormat}
576
+ {item with sortText; insertText; insertTextFormat; filterText }
574
577
else item
575
578
576
579
let completionsGetTypeEnv = function
@@ -1304,3 +1307,69 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
1304
1307
in
1305
1308
items @ regularCompletions
1306
1309
| _ -> items)))
1310
+ | CexhaustiveSwitch {contextPath; exprLoc} ->
1311
+ let range = Utils. rangeOfLoc exprLoc in
1312
+ let printFailwithStr num =
1313
+ " ${" ^ string_of_int num ^ " :failwith(\" todo\" )}"
1314
+ in
1315
+ let withExhaustiveItem ~cases ?(startIndex = 0 ) (c : Completion.t ) =
1316
+ (* We don't need to write out `switch` here since we know that's what the
1317
+ user has already written. Just complete for the rest. *)
1318
+ let newText =
1319
+ c.name ^ " {\n "
1320
+ ^ (cases
1321
+ |> List. mapi (fun index caseText ->
1322
+ " | " ^ caseText ^ " => "
1323
+ ^ printFailwithStr (startIndex + index + 1 ))
1324
+ |> String. concat " \n " )
1325
+ ^ " \n }"
1326
+ |> Utils. indent range.start.character
1327
+ in
1328
+ [
1329
+ c;
1330
+ {
1331
+ c with
1332
+ name = c.name ^ " (exhaustive switch)" ;
1333
+ filterText = Some c.name;
1334
+ insertTextFormat = Some Snippet ;
1335
+ insertText = Some newText;
1336
+ kind = Snippet " insert exhaustive switch for value" ;
1337
+ };
1338
+ ]
1339
+ in
1340
+ let completionsForContextPath =
1341
+ contextPath
1342
+ |> getCompletionsForContextPath ~full ~opens ~raw Opens ~all Files ~pos ~env
1343
+ ~exact: forHover ~scope
1344
+ in
1345
+ completionsForContextPath
1346
+ |> List. map (fun (c : Completion.t ) ->
1347
+ match c.kind with
1348
+ | Value typExpr -> (
1349
+ match typExpr |> TypeUtils. extractType ~env: c.env ~package with
1350
+ | Some (Tvariant v ) ->
1351
+ withExhaustiveItem c
1352
+ ~cases:
1353
+ (v.constructors
1354
+ |> List. map (fun (constructor : Constructor.t ) ->
1355
+ constructor.cname.txt
1356
+ ^
1357
+ match constructor.args with
1358
+ | Args [] -> " "
1359
+ | _ -> " (_)" ))
1360
+ | Some (Tpolyvariant v ) ->
1361
+ withExhaustiveItem c
1362
+ ~cases:
1363
+ (v.constructors
1364
+ |> List. map (fun (constructor : polyVariantConstructor ) ->
1365
+ " | #" ^ constructor.name
1366
+ ^
1367
+ match constructor.args with
1368
+ | [] -> " "
1369
+ | _ -> " (_)" ))
1370
+ | Some (Toption (_env , _typ )) ->
1371
+ withExhaustiveItem c ~cases: [" Some($1)" ; " None" ] ~start Index:1
1372
+ | Some (Tbool _ ) -> withExhaustiveItem c ~cases: [" true" ; " false" ]
1373
+ | _ -> [c])
1374
+ | _ -> [c])
1375
+ |> List. flatten
0 commit comments