Skip to content

Commit bed266e

Browse files
committed
complete arrays in patterns
1 parent c8ece7c commit bed266e

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

analysis/src/CompletionBackEnd.ml

+10
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,8 @@ let rec extractType ~env ~package (t : Types.type_expr) =
15211521
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> extractType ~env ~package t1
15221522
| Tconstr (Path.Pident {name = "option"}, [payloadTypeExpr], _) ->
15231523
Some (Completable.Toption (env, payloadTypeExpr))
1524+
| Tconstr (Path.Pident {name = "array"}, [payloadTypeExpr], _) ->
1525+
Some (Tarray (env, payloadTypeExpr))
15241526
| Tconstr (Path.Pident {name = "bool"}, [], _) -> Some (Tbool env)
15251527
| Tconstr (path, _, _) -> (
15261528
match References.digConstructor ~env ~package path with
@@ -1665,6 +1667,12 @@ let completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
16651667
~insertText:(if !Cfg.supportsSnippets then "{$0}" else "{}")
16661668
~sortText:"a" ~kind:(Value typeExpr) ~env ();
16671669
])
1670+
| Some (Tarray (env, typeExpr)) ->
1671+
[
1672+
Completion.createWithSnippet ~name:"[]"
1673+
~insertText:(if !Cfg.supportsSnippets then "[$0]" else "[]")
1674+
~sortText:"a" ~kind:(Value typeExpr) ~env ();
1675+
]
16681676
| _ -> []
16691677
in
16701678
(* Include all values and modules in completion if there's a prefix, not otherwise *)
@@ -1796,6 +1804,8 @@ let rec resolveNestedPattern typ ~env ~package ~nested =
17961804
match List.nth_opt constructor.args payloadNum with
17971805
| None -> None
17981806
| Some typ -> typ |> resolveNestedPattern ~env ~package ~nested))
1807+
| PArray, Some (Tarray (env, typ)) ->
1808+
typ |> resolveNestedPattern ~env ~package ~nested
17991809
| _ -> None)
18001810

18011811
let processCompletable ~debug ~full ~scope ~env ~pos ~forHover

analysis/src/CompletionFrontEnd.ml

+3
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
510510
| Ppat_construct ({txt = Lident prefix}, None) ->
511511
commitFoundPat ~prefix ()
512512
| Ppat_variant (prefix, None) -> commitFoundPat ~prefix:("#" ^ prefix) ()
513+
| Ppat_array arrayPatterns ->
514+
appendNestedPat Completable.PArray;
515+
if List.length arrayPatterns = 0 then commitFoundPat ~prefix:"" ()
513516
| Ppat_tuple patterns -> (
514517
match patterns |> findPatTupleItemWithCursor ~pos:posBeforeCursor with
515518
| Some itemNum -> appendNestedPat (Completable.PTupleItem {itemNum})

analysis/src/SharedTypes.ml

+3
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ module Completable = struct
559559
| PRecordBody of {seenFields: string list}
560560
| PVariantPayload of {constructorName: string; payloadNum: int}
561561
| PPolyvariantPayload of {constructorName: string; payloadNum: int}
562+
| PArray
562563

563564
let patternPathToString p =
564565
match p with
@@ -571,6 +572,7 @@ module Completable = struct
571572
| PPolyvariantPayload {constructorName; payloadNum} ->
572573
"polyvariantPayload::" ^ constructorName ^ "($" ^ string_of_int payloadNum
573574
^ ")"
575+
| PArray -> "array"
574576

575577
type t =
576578
| Cdecorator of string (** e.g. @module *)
@@ -602,6 +604,7 @@ module Completable = struct
602604
| Tuple of QueryEnv.t * Types.type_expr list * Types.type_expr
603605
| Toption of QueryEnv.t * Types.type_expr
604606
| Tbool of QueryEnv.t
607+
| Tarray of QueryEnv.t * Types.type_expr
605608
| Tvariant of {
606609
env: QueryEnv.t;
607610
constructors: Constructor.t list;

analysis/tests/src/CompletionPattern.res

+9
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,12 @@ ignore(b)
105105

106106
// switch b { | #three({})}
107107
// ^com
108+
109+
let c: array<bool> = []
110+
ignore(c)
111+
112+
// switch c { | }
113+
// ^com
114+
115+
// switch c { | [] }
116+
// ^com

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

+33
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,36 @@ Completable: Cpattern Value[b]->polyvariantPayload::three($0), recordBody
390390
"documentation": null
391391
}]
392392

393+
Complete src/CompletionPattern.res 111:15
394+
XXX Not found!
395+
Completable: Cpattern Value[c]
396+
[{
397+
"label": "[]",
398+
"kind": 12,
399+
"tags": [],
400+
"detail": "bool",
401+
"documentation": null,
402+
"sortText": "a",
403+
"insertText": "[$0]",
404+
"insertTextFormat": 2
405+
}]
406+
407+
Complete src/CompletionPattern.res 114:17
408+
looking for: Cpath Value[c]
409+
posCursor:[114:17] posNoWhite:[114:16] Found expr:[114:3->114:20]
410+
posCursor:[114:17] posNoWhite:[114:16] Found pattern:[114:16->114:18]
411+
Completable: Cpattern Value[c]->array
412+
[{
413+
"label": "true",
414+
"kind": 4,
415+
"tags": [],
416+
"detail": "bool",
417+
"documentation": null
418+
}, {
419+
"label": "false",
420+
"kind": 4,
421+
"tags": [],
422+
"detail": "bool",
423+
"documentation": null
424+
}]
425+

0 commit comments

Comments
 (0)