Skip to content

Commit

Permalink
complete arrays in patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Jan 5, 2023
1 parent c8ece7c commit bed266e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,8 @@ let rec extractType ~env ~package (t : Types.type_expr) =
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> extractType ~env ~package t1
| Tconstr (Path.Pident {name = "option"}, [payloadTypeExpr], _) ->
Some (Completable.Toption (env, payloadTypeExpr))
| Tconstr (Path.Pident {name = "array"}, [payloadTypeExpr], _) ->
Some (Tarray (env, payloadTypeExpr))
| Tconstr (Path.Pident {name = "bool"}, [], _) -> Some (Tbool env)
| Tconstr (path, _, _) -> (
match References.digConstructor ~env ~package path with
Expand Down Expand Up @@ -1665,6 +1667,12 @@ let completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
~insertText:(if !Cfg.supportsSnippets then "{$0}" else "{}")
~sortText:"a" ~kind:(Value typeExpr) ~env ();
])
| Some (Tarray (env, typeExpr)) ->
[
Completion.createWithSnippet ~name:"[]"
~insertText:(if !Cfg.supportsSnippets then "[$0]" else "[]")
~sortText:"a" ~kind:(Value typeExpr) ~env ();
]
| _ -> []
in
(* Include all values and modules in completion if there's a prefix, not otherwise *)
Expand Down Expand Up @@ -1796,6 +1804,8 @@ let rec resolveNestedPattern typ ~env ~package ~nested =
match List.nth_opt constructor.args payloadNum with
| None -> None
| Some typ -> typ |> resolveNestedPattern ~env ~package ~nested))
| PArray, Some (Tarray (env, typ)) ->
typ |> resolveNestedPattern ~env ~package ~nested
| _ -> None)

let processCompletable ~debug ~full ~scope ~env ~pos ~forHover
Expand Down
3 changes: 3 additions & 0 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
| Ppat_construct ({txt = Lident prefix}, None) ->
commitFoundPat ~prefix ()
| Ppat_variant (prefix, None) -> commitFoundPat ~prefix:("#" ^ prefix) ()
| Ppat_array arrayPatterns ->
appendNestedPat Completable.PArray;
if List.length arrayPatterns = 0 then commitFoundPat ~prefix:"" ()
| Ppat_tuple patterns -> (
match patterns |> findPatTupleItemWithCursor ~pos:posBeforeCursor with
| Some itemNum -> appendNestedPat (Completable.PTupleItem {itemNum})
Expand Down
3 changes: 3 additions & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ module Completable = struct
| PRecordBody of {seenFields: string list}
| PVariantPayload of {constructorName: string; payloadNum: int}
| PPolyvariantPayload of {constructorName: string; payloadNum: int}
| PArray

let patternPathToString p =
match p with
Expand All @@ -571,6 +572,7 @@ module Completable = struct
| PPolyvariantPayload {constructorName; payloadNum} ->
"polyvariantPayload::" ^ constructorName ^ "($" ^ string_of_int payloadNum
^ ")"
| PArray -> "array"

type t =
| Cdecorator of string (** e.g. @module *)
Expand Down Expand Up @@ -602,6 +604,7 @@ module Completable = struct
| Tuple of QueryEnv.t * Types.type_expr list * Types.type_expr
| Toption of QueryEnv.t * Types.type_expr
| Tbool of QueryEnv.t
| Tarray of QueryEnv.t * Types.type_expr
| Tvariant of {
env: QueryEnv.t;
constructors: Constructor.t list;
Expand Down
9 changes: 9 additions & 0 deletions analysis/tests/src/CompletionPattern.res
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,12 @@ ignore(b)

// switch b { | #three({})}
// ^com

let c: array<bool> = []
ignore(c)

// switch c { | }
// ^com

// switch c { | [] }
// ^com
33 changes: 33 additions & 0 deletions analysis/tests/src/expected/CompletionPattern.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,36 @@ Completable: Cpattern Value[b]->polyvariantPayload::three($0), recordBody
"documentation": null
}]

Complete src/CompletionPattern.res 111:15
XXX Not found!
Completable: Cpattern Value[c]
[{
"label": "[]",
"kind": 12,
"tags": [],
"detail": "bool",
"documentation": null,
"sortText": "a",
"insertText": "[$0]",
"insertTextFormat": 2
}]

Complete src/CompletionPattern.res 114:17
looking for: Cpath Value[c]
posCursor:[114:17] posNoWhite:[114:16] Found expr:[114:3->114:20]
posCursor:[114:17] posNoWhite:[114:16] Found pattern:[114:16->114:18]
Completable: Cpattern Value[c]->array
[{
"label": "true",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "false",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]

0 comments on commit bed266e

Please sign in to comment.