Skip to content

Commit 78d58c8

Browse files
committed
move Toption to use completion type as well
1 parent 47f9650 commit 78d58c8

File tree

6 files changed

+37
-38
lines changed

6 files changed

+37
-38
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,35 +1059,30 @@ let rec completeTypedValue (t : SharedTypes.completionType) ~full ~prefix
10591059
~env ())
10601060
|> filterItems ~prefix
10611061
| Toption (env, t) ->
1062-
let innerType =
1063-
Utils.unwrapIfOption t |> TypeUtils.extractType ~env ~package:full.package
1064-
in
1062+
let innerType = TypeUtils.unwrapCompletionTypeIfOption t in
10651063
let expandedCompletions =
10661064
innerType
1067-
|> Option.map (fun innerType ->
1068-
innerType
1069-
|> completeTypedValue ~full ~prefix ~completionContext ~mode
1070-
|> List.map (fun (c : Completion.t) ->
1071-
{
1072-
c with
1073-
name = "Some(" ^ c.name ^ ")";
1074-
sortText = None;
1075-
insertText =
1076-
(match c.insertText with
1077-
| None -> None
1078-
| Some insertText -> Some ("Some(" ^ insertText ^ ")"));
1079-
}))
1065+
|> completeTypedValue ~full ~prefix ~completionContext ~mode
1066+
|> List.map (fun (c : Completion.t) ->
1067+
{
1068+
c with
1069+
name = "Some(" ^ c.name ^ ")";
1070+
sortText = None;
1071+
insertText =
1072+
(match c.insertText with
1073+
| None -> None
1074+
| Some insertText -> Some ("Some(" ^ insertText ^ ")"));
1075+
})
10801076
in
1081-
([
1082-
Completion.create "None" ~kind:(Label (t |> Shared.typeToString)) ~env;
1083-
Completion.createWithSnippet ~name:"Some(_)"
1084-
~kind:(Label (t |> Shared.typeToString))
1085-
~env ~insertText:"Some(${1:_})" ();
1086-
]
1087-
@
1088-
match expandedCompletions with
1089-
| None -> []
1090-
| Some expandedCompletions -> expandedCompletions)
1077+
[
1078+
Completion.create "None"
1079+
~kind:(Label (t |> TypeUtils.extractedTypeToString))
1080+
~env;
1081+
Completion.createWithSnippet ~name:"Some(_)"
1082+
~kind:(Label (t |> TypeUtils.extractedTypeToString))
1083+
~env ~insertText:"Some(${1:_})" ();
1084+
]
1085+
@ expandedCompletions
10911086
|> filterItems ~prefix
10921087
| Tuple (env, exprs, typ) ->
10931088
let numExprs = List.length exprs in

analysis/src/SharedTypes.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ type polyVariantConstructor = {name: string; args: Types.type_expr list}
297297
(** An type that can be used to drive completion *)
298298
type completionType =
299299
| Tuple of QueryEnv.t * Types.type_expr list * Types.type_expr
300-
| Toption of QueryEnv.t * Types.type_expr
300+
| Toption of QueryEnv.t * completionType
301301
| Tbool of QueryEnv.t
302302
| Tarray of QueryEnv.t * Types.type_expr
303303
| Tstring of QueryEnv.t

analysis/src/TypeUtils.ml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ let rec extractType ~env ~package (t : Types.type_expr) =
112112
match t.desc with
113113
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> extractType ~env ~package t1
114114
| Tconstr (Path.Pident {name = "option"}, [payloadTypeExpr], _) ->
115-
Some (Toption (env, payloadTypeExpr))
115+
payloadTypeExpr |> extractType ~env ~package
116+
|> Option.map (fun payloadTyp -> Toption (env, payloadTyp))
116117
| Tconstr (Path.Pident {name = "array"}, [payloadTypeExpr], _) ->
117118
Some (Tarray (env, payloadTypeExpr))
118119
| Tconstr (Path.Pident {name = "bool"}, [], _) -> Some (Tbool env)
@@ -299,10 +300,7 @@ let rec resolveNested (typ : completionType) ~env ~full ~nested =
299300
Some (Completable.RecordField {seenFields}) )
300301
| NVariantPayload {constructorName = "Some"; itemNum = 0}, Toption (env, typ)
301302
->
302-
typ
303-
|> extractType ~env ~package:full.package
304-
|> Utils.Option.flatMap (fun typ ->
305-
typ |> resolveNested ~env ~full ~nested)
303+
typ |> resolveNested ~env ~full ~nested
306304
| NVariantPayload {constructorName; itemNum}, Tvariant {env; constructors}
307305
-> (
308306
match
@@ -400,18 +398,23 @@ let printRecordFromFields ?name (fields : field list) =
400398
|> String.concat ", ")
401399
^ "}"
402400

403-
let extractedTypeToString = function
401+
let rec extractedTypeToString = function
404402
| Tuple (_, _, typ)
405-
| Toption (_, typ)
406403
| Tpolyvariant {typeExpr = typ}
407404
| Tfunction {typ}
408405
| Trecord {definition = `TypeExpr typ} ->
409406
Shared.typeToString typ
410407
| Tbool _ -> "bool"
411408
| Tstring _ -> "string"
412409
| Tarray (_, innerTyp) -> "array<" ^ Shared.typeToString innerTyp ^ ">"
410+
| Toption (_, typ) -> "option<" ^ extractedTypeToString typ ^ ">"
413411
| Tvariant {variantDecl; variantName} ->
414412
Shared.declToString variantName variantDecl
415413
| Trecord {definition = `NameOnly name; fields} ->
416414
printRecordFromFields ~name fields
417-
| TinlineRecord {fields} -> printRecordFromFields fields
415+
| TinlineRecord {fields} -> printRecordFromFields fields
416+
417+
let unwrapCompletionTypeIfOption (t : SharedTypes.completionType) =
418+
match t with
419+
| Toption (_, unwrapped) -> unwrapped
420+
| _ -> t

analysis/src/Utils.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ let rec unwrapIfOption (t : Types.type_expr) =
168168
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> unwrapIfOption t1
169169
| Tconstr (Path.Pident {name = "option"}, [unwrappedType], _) -> unwrappedType
170170
| _ -> t
171+
171172
let isReactComponent (vb : Parsetree.value_binding) =
172173
vb.pvb_attributes
173174
|> List.exists (function

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,13 @@ Completable: Cexpression CArgument Value[fnTakingRecordWithOptVariant]($0)->reco
497497
"label": "None",
498498
"kind": 4,
499499
"tags": [],
500-
"detail": "someVariant",
500+
"detail": "type someVariant = One | Two | Three(int, string)",
501501
"documentation": null
502502
}, {
503503
"label": "Some(_)",
504504
"kind": 4,
505505
"tags": [],
506-
"detail": "someVariant",
506+
"detail": "type someVariant = One | Two | Three(int, string)",
507507
"documentation": null,
508508
"insertText": "Some(${1:_})",
509509
"insertTextFormat": 2

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Completable: Cexpression CArgument Value[someFnTakingVariant]($0)=So
158158
"label": "Some(_)",
159159
"kind": 4,
160160
"tags": [],
161-
"detail": "someVariant",
161+
"detail": "type someVariant = One | Two | Three(int, string)",
162162
"documentation": null,
163163
"sortText": "A Some(_)",
164164
"insertText": "Some(${1:_})",

0 commit comments

Comments
 (0)