Skip to content

Commit 109f74a

Browse files
committed
pick up all props needed to print types both from type_expr and declarations
1 parent b4facb4 commit 109f74a

File tree

7 files changed

+97
-59
lines changed

7 files changed

+97
-59
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ let detail name (kind : Completion.kind) =
233233
^ ")")
234234
^ "\n\n" ^ s
235235
| Snippet s -> s
236+
| ExtractedType extractedType -> TypeUtils.extractedTypeToString extractedType
236237

237238
let findAllCompletions ~(env : QueryEnv.t) ~prefix ~exact ~namesUsed
238239
~(completionContext : Completable.completionContext) =
@@ -587,6 +588,8 @@ let completionsGetCompletionType = function
587588
| {Completion.kind = ObjLabel typ; env} :: _ -> Some (TypeExpr typ, env)
588589
| {Completion.kind = Field ({typ}, _); env} :: _ -> Some (TypeExpr typ, env)
589590
| {Completion.kind = Type typ; env} :: _ -> Some (ResolvedType typ, env)
591+
| {Completion.kind = ExtractedType typ; env} :: _ ->
592+
Some (ExtractedType typ, env)
590593
| _ -> None
591594

592595
let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
@@ -1083,7 +1086,7 @@ let rec completeTypedValue (t : SharedTypes.completionType) ~env ~full ~prefix
10831086
~insertText:(printConstructorArgs numExprs ~asSnippet:true)
10841087
~kind:(Value typ) ~env ();
10851088
]
1086-
| Some (Trecord {env; fields; typeExpr}) -> (
1089+
| Some (Trecord {env; fields} as extractedType) -> (
10871090
(* As we're completing for a record, we'll need a hint (completionContext)
10881091
here to figure out whether we should complete for a record field, or
10891092
the record body itself. *)
@@ -1094,15 +1097,16 @@ let rec completeTypedValue (t : SharedTypes.completionType) ~env ~full ~prefix
10941097
List.mem field.fname.txt seenFields = false)
10951098
|> List.map (fun (field : field) ->
10961099
Completion.create field.fname.txt
1097-
~kind:(Field (field, typeExpr |> Shared.typeToString))
1100+
~kind:
1101+
(Field (field, TypeUtils.extractedTypeToString extractedType))
10981102
~env)
10991103
|> filterItems ~prefix
11001104
| None ->
11011105
if prefix = "" then
11021106
[
11031107
Completion.createWithSnippet ~name:"{}"
11041108
~insertText:(if !Cfg.supportsSnippets then "{$0}" else "{}")
1105-
~sortText:"A" ~kind:(Value typeExpr) ~env ();
1109+
~sortText:"A" ~kind:(ExtractedType extractedType) ~env ();
11061110
]
11071111
else [])
11081112
| Some (TinlineRecord {env; fields}) -> (

analysis/src/ProcessCmt.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
6464
~item:
6565
{
6666
Type.decl;
67+
name = ident.name;
6768
kind =
6869
(match type_kind with
6970
| Type_abstract -> (
@@ -167,6 +168,7 @@ let forTypeDeclaration ~env ~(exported : Exported.t)
167168
~item:
168169
{
169170
Type.decl = typ_type;
171+
name = name.txt;
170172
kind =
171173
(match typ_kind with
172174
| Ttype_abstract -> (

analysis/src/SharedTypes.ml

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module Type = struct
5757
| Record of field list
5858
| Variant of Constructor.t list
5959

60-
type t = {kind: kind; decl: Types.type_declaration}
60+
type t = {kind: kind; decl: Types.type_declaration; name: string}
6161
end
6262

6363
module Exported = struct
@@ -140,11 +140,6 @@ module Declared = struct
140140
}
141141
end
142142

143-
type completionType =
144-
| TypeExpr of Types.type_expr
145-
| InlineRecord of field list
146-
| ResolvedType of Type.t
147-
148143
module Stamps : sig
149144
type t
150145

@@ -299,6 +294,38 @@ end
299294

300295
type polyVariantConstructor = {name: string; args: Types.type_expr list}
301296

297+
(** An extracted type from a type expr *)
298+
type extractedType =
299+
| Tuple of QueryEnv.t * Types.type_expr list * Types.type_expr
300+
| Toption of QueryEnv.t * Types.type_expr
301+
| Tbool of QueryEnv.t
302+
| Tarray of QueryEnv.t * Types.type_expr
303+
| Tstring of QueryEnv.t
304+
| Tvariant of {
305+
env: QueryEnv.t;
306+
constructors: Constructor.t list;
307+
variantDecl: Types.type_declaration;
308+
variantName: string;
309+
}
310+
| Tpolyvariant of {
311+
env: QueryEnv.t;
312+
constructors: polyVariantConstructor list;
313+
typeExpr: Types.type_expr;
314+
}
315+
| Trecord of {
316+
env: QueryEnv.t;
317+
fields: field list;
318+
name: [`Str of string | `TypeExpr of Types.type_expr];
319+
}
320+
| TinlineRecord of {env: QueryEnv.t; fields: field list}
321+
| Tfunction of {env: QueryEnv.t; args: typedFnArg list; typ: Types.type_expr}
322+
323+
type completionType =
324+
| TypeExpr of Types.type_expr
325+
| InlineRecord of field list
326+
| ResolvedType of Type.t
327+
| ExtractedType of extractedType
328+
302329
module Completion = struct
303330
type kind =
304331
| Module of Module.t
@@ -311,6 +338,7 @@ module Completion = struct
311338
| Field of field * string
312339
| FileModule of string
313340
| Snippet of string
341+
| ExtractedType of extractedType
314342

315343
type t = {
316344
name: string;
@@ -361,7 +389,7 @@ module Completion = struct
361389
| ObjLabel _ -> 4
362390
| Label _ -> 4
363391
| Field (_, _) -> 5
364-
| Type _ -> 22
392+
| Type _ | ExtractedType _ -> 22
365393
| Value _ -> 12
366394
| Snippet _ -> 15
367395
end
@@ -625,36 +653,6 @@ module Completable = struct
625653
}
626654
| CexhaustiveSwitch of {contextPath: contextPath; exprLoc: Location.t}
627655

628-
(** An extracted type from a type expr *)
629-
type extractedType =
630-
| Tuple of QueryEnv.t * Types.type_expr list * Types.type_expr
631-
| Toption of QueryEnv.t * Types.type_expr
632-
| Tbool of QueryEnv.t
633-
| Tarray of QueryEnv.t * Types.type_expr
634-
| Tstring of QueryEnv.t
635-
| Tvariant of {
636-
env: QueryEnv.t;
637-
constructors: Constructor.t list;
638-
variantDecl: Types.type_declaration;
639-
variantName: string;
640-
}
641-
| Tpolyvariant of {
642-
env: QueryEnv.t;
643-
constructors: polyVariantConstructor list;
644-
typeExpr: Types.type_expr;
645-
}
646-
| Trecord of {
647-
env: QueryEnv.t;
648-
fields: field list;
649-
typeExpr: Types.type_expr;
650-
}
651-
| TinlineRecord of {env: QueryEnv.t; fields: field list}
652-
| Tfunction of {
653-
env: QueryEnv.t;
654-
args: typedFnArg list;
655-
typ: Types.type_expr;
656-
}
657-
658656
let toString =
659657
let completionContextToString = function
660658
| Value -> "Value"

analysis/src/TypeUtils.ml

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +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-
Some (Completable.Toption (env, payloadTypeExpr))
115+
Some (Toption (env, payloadTypeExpr))
116116
| Tconstr (Path.Pident {name = "array"}, [payloadTypeExpr], _) ->
117117
Some (Tarray (env, payloadTypeExpr))
118118
| Tconstr (Path.Pident {name = "bool"}, [], _) -> Some (Tbool env)
@@ -126,7 +126,7 @@ let rec extractType ~env ~package (t : Types.type_expr) =
126126
(Tvariant
127127
{env; constructors; variantName = name.txt; variantDecl = decl})
128128
| Some (env, {item = {kind = Record fields}}) ->
129-
Some (Trecord {env; fields; typeExpr = t})
129+
Some (Trecord {env; fields; name = `TypeExpr t})
130130
| _ -> None)
131131
| Ttuple expressions -> Some (Tuple (env, expressions, t))
132132
| Tvariant {row_fields} ->
@@ -244,15 +244,17 @@ let rec resolveTypeForPipeCompletion ~env ~package ~lhsLoc ~full
244244

245245
let extractTypeFromCompletionType (t : completionType) ~env ~full =
246246
match t with
247+
| ExtractedType extractedType -> Some extractedType
247248
| TypeExpr t -> t |> extractType ~env ~package:full.package
248249
| InlineRecord fields -> Some (TinlineRecord {env; fields})
249250
| ResolvedType typ -> (
250251
match typ.kind with
251252
| Tuple items -> Some (Tuple (env, items, Ctype.newty (Ttuple items)))
252-
| Record fields -> Some (TinlineRecord {env; fields})
253+
| Record fields -> Some (Trecord {env; fields; name = `Str typ.name})
253254
| Variant constructors ->
254255
Some
255-
(Tvariant {env; constructors; variantName = ""; variantDecl = typ.decl})
256+
(Tvariant
257+
{env; constructors; variantName = typ.name; variantDecl = typ.decl})
256258
| Abstract _ | Open -> (
257259
match typ.decl.type_manifest with
258260
| None -> None
@@ -279,8 +281,15 @@ let rec resolveNested (typ : completionType) ~env ~full ~nested =
279281
| Some {typ; optional} ->
280282
let typ = if optional then Utils.unwrapIfOption typ else typ in
281283
TypeExpr typ |> resolveNested ~env ~full ~nested)
282-
| NRecordBody {seenFields}, Some (Trecord {env; typeExpr}) ->
284+
| NRecordBody {seenFields}, Some (Trecord {env; name = `TypeExpr typeExpr})
285+
->
283286
Some (TypeExpr typeExpr, env, Some (Completable.RecordField {seenFields}))
287+
| ( NRecordBody {seenFields},
288+
Some (Trecord {env; name = `Str _} as extractedType) ) ->
289+
Some
290+
( ExtractedType extractedType,
291+
env,
292+
Some (Completable.RecordField {seenFields}) )
284293
| NRecordBody {seenFields}, Some (TinlineRecord {env; fields}) ->
285294
Some
286295
(InlineRecord fields, env, Some (Completable.RecordField {seenFields}))
@@ -362,3 +371,28 @@ let contextPathFromCoreType (coreType : Parsetree.core_type) =
362371
| Ptyp_constr (loc, []) ->
363372
Some (Completable.CPId (loc.txt |> Utils.flattenLongIdent, Type))
364373
| _ -> None
374+
375+
let printRecordFromFields ?name (fields : field list) =
376+
(match name with
377+
| None -> ""
378+
| Some name -> "type " ^ name ^ " = ")
379+
^ "{"
380+
^ (fields
381+
|> List.map (fun f -> f.fname.txt ^ ": " ^ Shared.typeToString f.typ)
382+
|> String.concat ", ")
383+
^ "}"
384+
385+
let extractedTypeToString = function
386+
| Tuple (_, _, typ)
387+
| Toption (_, typ)
388+
| Tpolyvariant {typeExpr = typ}
389+
| Tfunction {typ}
390+
| Trecord {name = `TypeExpr typ} ->
391+
Shared.typeToString typ
392+
| Tbool _ -> "bool"
393+
| Tstring _ -> "string"
394+
| Tarray (_, innerTyp) -> "array<" ^ Shared.typeToString innerTyp ^ ">"
395+
| Tvariant {variantDecl; variantName} ->
396+
Shared.declToString variantName variantDecl
397+
| Trecord {name = `Str name; fields} -> printRecordFromFields ~name fields
398+
| TinlineRecord {fields} -> printRecordFromFields fields

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Completable: Cexpression CArgument Value[fnTakingRecord]($0)->recordField(nested
169169
"insertTextFormat": 2
170170
}, {
171171
"label": "Some({})",
172-
"kind": 12,
172+
"kind": 22,
173173
"tags": [],
174174
"detail": "otherRecord",
175175
"documentation": null,
@@ -264,7 +264,7 @@ Pexp_apply ...[56:11->56:25] (...[56:26->56:60])
264264
Completable: Cexpression CArgument Value[fnTakingRecord]($0)->recordField(polyvariant), polyvariantPayload::three($0)
265265
[{
266266
"label": "{}",
267-
"kind": 12,
267+
"kind": 22,
268268
"tags": [],
269269
"detail": "someRecord",
270270
"documentation": null,
@@ -590,7 +590,7 @@ Pexp_apply ...[135:11->135:31] (...[135:32->135:66])
590590
Completable: Cexpression CArgument Value[fnTakingInlineRecord]($0)->variantPayload::WithInlineRecord($0), recordField(nestedRecord)
591591
[{
592592
"label": "{}",
593-
"kind": 12,
593+
"kind": 22,
594594
"tags": [],
595595
"detail": "otherRecord",
596596
"documentation": null,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ XXX Not found!
7676
Completable: Cpattern Value[f]
7777
[{
7878
"label": "{}",
79-
"kind": 12,
79+
"kind": 22,
8080
"tags": [],
8181
"detail": "someRecord",
8282
"documentation": null,
@@ -166,7 +166,7 @@ posCursor:[61:22] posNoWhite:[61:21] Found pattern:[61:16->61:25]
166166
Completable: Cpattern Value[f]->recordField(nest)
167167
[{
168168
"label": "{}",
169-
"kind": 12,
169+
"kind": 22,
170170
"tags": [],
171171
"detail": "nestedRecord",
172172
"documentation": null,

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ XXX Not found!
33
Completable: Cexpression Type[someRecord]
44
[{
55
"label": "{}",
6-
"kind": 4,
6+
"kind": 22,
77
"tags": [],
8-
"detail": "Inline record",
8+
"detail": "type someRecord = {age: int, name: string}",
99
"documentation": null,
1010
"sortText": "A",
1111
"insertText": "{$0}",
@@ -17,15 +17,15 @@ XXX Not found!
1717
Completable: Cexpression Type[someRecord]->recordBody
1818
[{
1919
"label": "age",
20-
"kind": 4,
20+
"kind": 5,
2121
"tags": [],
22-
"detail": "Inline record",
22+
"detail": "age: int\n\ntype someRecord = {age: int, name: string}",
2323
"documentation": null
2424
}, {
2525
"label": "name",
26-
"kind": 4,
26+
"kind": 5,
2727
"tags": [],
28-
"detail": "Inline record",
28+
"detail": "name: string\n\ntype someRecord = {age: int, name: string}",
2929
"documentation": null
3030
}]
3131

@@ -36,15 +36,15 @@ Completable: Cexpression Type[someVariant]
3636
"label": "One",
3737
"kind": 4,
3838
"tags": [],
39-
"detail": "One\n\ntype = One | Two(bool)",
39+
"detail": "One\n\ntype someVariant = One | Two(bool)",
4040
"documentation": null,
4141
"insertText": "One",
4242
"insertTextFormat": 2
4343
}, {
4444
"label": "Two(_)",
4545
"kind": 4,
4646
"tags": [],
47-
"detail": "Two(bool)\n\ntype = One | Two(bool)",
47+
"detail": "Two(bool)\n\ntype someVariant = One | Two(bool)",
4848
"documentation": null,
4949
"insertText": "Two(${1:_})",
5050
"insertTextFormat": 2
@@ -57,7 +57,7 @@ Completable: Cexpression Type[someVariant]=O
5757
"label": "One",
5858
"kind": 4,
5959
"tags": [],
60-
"detail": "One\n\ntype = One | Two(bool)",
60+
"detail": "One\n\ntype someVariant = One | Two(bool)",
6161
"documentation": null,
6262
"sortText": "A One",
6363
"insertText": "One",

0 commit comments

Comments
 (0)