Skip to content

Commit 5fd0e27

Browse files
zthcristianoc
authored andcommitted
make Toption work with both extracted and type expr types
1 parent b542f5f commit 5fd0e27

File tree

7 files changed

+75
-58
lines changed

7 files changed

+75
-58
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,8 @@ and getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
731731
| Some (typ, env) ->
732732
[
733733
Completion.create "dummy" ~env
734-
~kind:(Completion.ExtractedType (Toption (env, typ), `Type));
734+
~kind:
735+
(Completion.ExtractedType (Toption (env, ExtractedType typ), `Type));
735736
])
736737
| CPId (path, completionContext) ->
737738
path
@@ -1207,27 +1208,31 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
12071208
~env ())
12081209
|> filterItems ~prefix
12091210
| Toption (env, t) ->
1210-
let innerType = TypeUtils.unwrapCompletionTypeIfOption t in
1211+
let innerType =
1212+
match t with
1213+
| ExtractedType t -> Some t
1214+
| TypeExpr t -> t |> TypeUtils.extractType ~env ~package:full.package
1215+
in
12111216
let expandedCompletions =
1212-
innerType
1213-
|> completeTypedValue ~full ~prefix ~completionContext ~mode
1214-
|> List.map (fun (c : Completion.t) ->
1215-
{
1216-
c with
1217-
name = "Some(" ^ c.name ^ ")";
1218-
sortText = None;
1219-
insertText =
1220-
(match c.insertText with
1221-
| None -> None
1222-
| Some insertText -> Some ("Some(" ^ insertText ^ ")"));
1223-
})
1217+
match innerType with
1218+
| None -> []
1219+
| Some innerType ->
1220+
innerType
1221+
|> completeTypedValue ~full ~prefix ~completionContext ~mode
1222+
|> List.map (fun (c : Completion.t) ->
1223+
{
1224+
c with
1225+
name = "Some(" ^ c.name ^ ")";
1226+
sortText = None;
1227+
insertText =
1228+
(match c.insertText with
1229+
| None -> None
1230+
| Some insertText -> Some ("Some(" ^ insertText ^ ")"));
1231+
})
12241232
in
12251233
[
1226-
Completion.create "None"
1227-
~kind:(Label (t |> TypeUtils.extractedTypeToString))
1228-
~env;
1229-
Completion.createWithSnippet ~name:"Some(_)"
1230-
~kind:(Label (t |> TypeUtils.extractedTypeToString))
1234+
Completion.create "None" ~kind:(kindFromInnerType t) ~env;
1235+
Completion.createWithSnippet ~name:"Some(_)" ~kind:(kindFromInnerType t)
12311236
~env ~insertText:"Some(${1:_})" ();
12321237
]
12331238
@ expandedCompletions

analysis/src/SharedTypes.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ end
296296

297297
type polyVariantConstructor = {name: string; args: Types.type_expr list}
298298

299-
(** An type that can be used to drive completion *)
300-
type completionType =
299+
type innerType = TypeExpr of Types.type_expr | ExtractedType of completionType
300+
and completionType =
301301
| Tuple of QueryEnv.t * Types.type_expr list * Types.type_expr
302-
| Toption of QueryEnv.t * completionType
303302
| Texn of QueryEnv.t
303+
| Toption of QueryEnv.t * innerType
304304
| Tbool of QueryEnv.t
305305
| Tarray of QueryEnv.t * completionType
306306
| Tstring of QueryEnv.t
@@ -760,6 +760,12 @@ module Completion = struct
760760
| Snippet _ | FollowContextPath _ -> 15
761761
end
762762

763+
let kindFromInnerType (t : innerType) =
764+
match t with
765+
| ExtractedType extractedType ->
766+
Completion.ExtractedType (extractedType, `Value)
767+
| TypeExpr typ -> Value typ
768+
763769
module CursorPosition = struct
764770
type t = NoCursor | HasCursor | EmptyLoc
765771

analysis/src/TypeUtils.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ 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-
payloadTypeExpr |> extractType ~env ~package
116-
|> Option.map (fun payloadTyp -> Toption (env, payloadTyp))
115+
Some (Toption (env, TypeExpr payloadTypeExpr))
117116
| Tconstr (Path.Pident {name = "array"}, [payloadTypeExpr], _) ->
118117
payloadTypeExpr |> extractType ~env ~package
119118
|> Option.map (fun payloadTyp -> Tarray (env, payloadTyp))
@@ -300,9 +299,14 @@ let rec resolveNested (typ : completionType) ~env ~full ~nested =
300299
( TinlineRecord {fields; env},
301300
env,
302301
Some (Completable.RecordField {seenFields}) )
303-
| NVariantPayload {constructorName = "Some"; itemNum = 0}, Toption (env, typ)
304-
->
302+
| ( NVariantPayload {constructorName = "Some"; itemNum = 0},
303+
Toption (env, ExtractedType typ) ) ->
305304
typ |> resolveNested ~env ~full ~nested
305+
| ( NVariantPayload {constructorName = "Some"; itemNum = 0},
306+
Toption (env, TypeExpr typ) ) ->
307+
typ
308+
|> extractType ~env ~package:full.package
309+
|> Utils.Option.flatMap (fun t -> t |> resolveNested ~env ~full ~nested)
306310
| NVariantPayload {constructorName; itemNum}, Tvariant {env; constructors}
307311
-> (
308312
match
@@ -415,7 +419,9 @@ let rec extractedTypeToString ?(inner = false) = function
415419
| Tstring _ -> "string"
416420
| Tarray (_, innerTyp) ->
417421
"array<" ^ extractedTypeToString ~inner:true innerTyp ^ ">"
418-
| Toption (_, innerTyp) ->
422+
| Toption (_, TypeExpr innerTyp) ->
423+
"option<" ^ Shared.typeToString innerTyp ^ ">"
424+
| Toption (_, ExtractedType innerTyp) ->
419425
"option<" ^ extractedTypeToString ~inner:true innerTyp ^ ">"
420426
| Tvariant {variantDecl; variantName} ->
421427
if inner then variantName else Shared.declToString variantName variantDecl
@@ -426,5 +432,5 @@ let rec extractedTypeToString ?(inner = false) = function
426432

427433
let unwrapCompletionTypeIfOption (t : SharedTypes.completionType) =
428434
match t with
429-
| Toption (_, unwrapped) -> unwrapped
435+
| Toption (_, ExtractedType unwrapped) -> unwrapped
430436
| _ -> t

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ Pexp_apply ...[41:11->41:25] (...[41:26->41:47])
155155
Completable: Cexpression CArgument Value[fnTakingRecord]($0)->recordField(nested)
156156
[{
157157
"label": "None",
158-
"kind": 4,
158+
"kind": 12,
159159
"tags": [],
160160
"detail": "otherRecord",
161161
"documentation": null
162162
}, {
163163
"label": "Some(_)",
164-
"kind": 4,
164+
"kind": 12,
165165
"tags": [],
166166
"detail": "otherRecord",
167167
"documentation": null,
@@ -324,13 +324,13 @@ Pexp_apply ...[72:11->72:24] (...[72:25->72:27])
324324
Completable: Cexpression CArgument Value[fnTakingArray]($0)->array
325325
[{
326326
"label": "None",
327-
"kind": 4,
327+
"kind": 12,
328328
"tags": [],
329329
"detail": "bool",
330330
"documentation": null
331331
}, {
332332
"label": "Some(_)",
333-
"kind": 4,
333+
"kind": 12,
334334
"tags": [],
335335
"detail": "bool",
336336
"documentation": null,
@@ -386,13 +386,13 @@ Pexp_apply ...[81:11->81:24] (...[81:25->81:33])
386386
Completable: Cexpression CArgument Value[fnTakingArray]($0)->array
387387
[{
388388
"label": "None",
389-
"kind": 4,
389+
"kind": 12,
390390
"tags": [],
391391
"detail": "bool",
392392
"documentation": null
393393
}, {
394394
"label": "Some(_)",
395-
"kind": 4,
395+
"kind": 12,
396396
"tags": [],
397397
"detail": "bool",
398398
"documentation": null,
@@ -418,13 +418,13 @@ Pexp_apply ...[84:11->84:24] (...[84:25->84:39])
418418
Completable: Cexpression CArgument Value[fnTakingArray]($0)->array
419419
[{
420420
"label": "None",
421-
"kind": 4,
421+
"kind": 12,
422422
"tags": [],
423423
"detail": "bool",
424424
"documentation": null
425425
}, {
426426
"label": "Some(_)",
427-
"kind": 4,
427+
"kind": 12,
428428
"tags": [],
429429
"detail": "bool",
430430
"documentation": null,
@@ -495,15 +495,15 @@ Pexp_apply ...[116:11->116:39] (...[116:40->116:56])
495495
Completable: Cexpression CArgument Value[fnTakingRecordWithOptVariant]($0)->recordField(someVariant)
496496
[{
497497
"label": "None",
498-
"kind": 4,
498+
"kind": 12,
499499
"tags": [],
500-
"detail": "type someVariant = One | Two | Three(int, string)",
500+
"detail": "someVariant",
501501
"documentation": null
502502
}, {
503503
"label": "Some(_)",
504-
"kind": 4,
504+
"kind": 12,
505505
"tags": [],
506-
"detail": "type someVariant = One | Two | Three(int, string)",
506+
"detail": "someVariant",
507507
"documentation": null,
508508
"insertText": "Some(${1:_})",
509509
"insertTextFormat": 2

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ Pexp_apply ...[57:11->57:30] (...[57:31->57:33])
156156
Completable: Cexpression CArgument Value[someFnTakingVariant]($0)=So
157157
[{
158158
"label": "Some(_)",
159-
"kind": 4,
159+
"kind": 12,
160160
"tags": [],
161-
"detail": "type someVariant = One | Two | Three(int, string)",
161+
"detail": "someVariant",
162162
"documentation": null,
163163
"sortText": "A Some(_)",
164164
"insertText": "Some(${1:_})",

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -485,13 +485,13 @@ posCursor:[137:29] posNoWhite:[137:28] Found pattern:[137:20->137:32]
485485
Completable: Cpattern Value[p]->variantPayload::Test($2)
486486
[{
487487
"label": "None",
488-
"kind": 4,
488+
"kind": 12,
489489
"tags": [],
490490
"detail": "bool",
491491
"documentation": null
492492
}, {
493493
"label": "Some(_)",
494-
"kind": 4,
494+
"kind": 12,
495495
"tags": [],
496496
"detail": "bool",
497497
"documentation": null,
@@ -573,13 +573,13 @@ posCursor:[153:30] posNoWhite:[153:29] Found pattern:[153:21->153:32]
573573
Completable: Cpattern Value[v]->polyvariantPayload::test($2)
574574
[{
575575
"label": "None",
576-
"kind": 4,
576+
"kind": 12,
577577
"tags": [],
578578
"detail": "bool",
579579
"documentation": null
580580
}, {
581581
"label": "Some(_)",
582-
"kind": 4,
582+
"kind": 12,
583583
"tags": [],
584584
"detail": "bool",
585585
"documentation": null,
@@ -659,13 +659,13 @@ posCursor:[167:23] posNoWhite:[167:21] Found pattern:[167:16->167:24]
659659
Completable: Cpattern Value[s]->tuple($1)
660660
[{
661661
"label": "None",
662-
"kind": 4,
662+
"kind": 12,
663663
"tags": [],
664664
"detail": "bool",
665665
"documentation": null
666666
}, {
667667
"label": "Some(_)",
668-
"kind": 4,
668+
"kind": 12,
669669
"tags": [],
670670
"detail": "bool",
671671
"documentation": null,
@@ -691,13 +691,13 @@ posCursor:[170:22] posNoWhite:[170:21] Found pattern:[170:16->170:28]
691691
Completable: Cpattern Value[s]->tuple($1)
692692
[{
693693
"label": "None",
694-
"kind": 4,
694+
"kind": 12,
695695
"tags": [],
696696
"detail": "bool",
697697
"documentation": null
698698
}, {
699699
"label": "Some(_)",
700-
"kind": 4,
700+
"kind": 12,
701701
"tags": [],
702702
"detail": "bool",
703703
"documentation": null,
@@ -736,13 +736,13 @@ posCursor:[176:41] posNoWhite:[176:40] Found pattern:[176:35->176:47]
736736
Completable: Cpattern Value[s]->tuple($1)
737737
[{
738738
"label": "None",
739-
"kind": 4,
739+
"kind": 12,
740740
"tags": [],
741741
"detail": "bool",
742742
"documentation": null
743743
}, {
744744
"label": "Some(_)",
745-
"kind": 4,
745+
"kind": 12,
746746
"tags": [],
747747
"detail": "bool",
748748
"documentation": null,
@@ -878,13 +878,13 @@ posCursor:[194:24] posNoWhite:[194:23] Found pattern:[194:23->194:24]
878878
Completable: Cpattern Value[s]->tuple($1)
879879
[{
880880
"label": "None",
881-
"kind": 4,
881+
"kind": 12,
882882
"tags": [],
883883
"detail": "bool",
884884
"documentation": null
885885
}, {
886886
"label": "Some(_)",
887-
"kind": 4,
887+
"kind": 12,
888888
"tags": [],
889889
"detail": "bool",
890890
"documentation": null,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ XXX Not found!
142142
Completable: Cexpression Type[someTuple]->tuple($1)
143143
[{
144144
"label": "None",
145-
"kind": 4,
145+
"kind": 12,
146146
"tags": [],
147147
"detail": "bool",
148148
"documentation": null
149149
}, {
150150
"label": "Some(_)",
151-
"kind": 4,
151+
"kind": 12,
152152
"tags": [],
153153
"detail": "bool",
154154
"documentation": null,
@@ -173,13 +173,13 @@ XXX Not found!
173173
Completable: Cexpression option<Type[someVariant]>
174174
[{
175175
"label": "None",
176-
"kind": 4,
176+
"kind": 12,
177177
"tags": [],
178178
"detail": "type someVariant = One | Two(bool)",
179179
"documentation": null
180180
}, {
181181
"label": "Some(_)",
182-
"kind": 4,
182+
"kind": 12,
183183
"tags": [],
184184
"detail": "type someVariant = One | Two(bool)",
185185
"documentation": null,

0 commit comments

Comments
 (0)