@@ -35,6 +35,54 @@ let map_record_field {Types.ld_id; ld_type; ld_attributes; ld_optional} =
3535 deprecated = Process_attributes. find_deprecated_attribute ld_attributes;
3636 }
3737
38+ (* The constructors of [type t += ...] are registered as constructors of an
39+ open type named [t], so that completion can offer them for values of
40+ type [t]. They are not part of any type declaration, hence the stand-in
41+ declaration that carries the kind and, as its manifest, the extended
42+ type, which completion compares with the type it completes. *)
43+ let add_extension_constructor ~(env : Env.t ) ~id ~name
44+ (ext : Types.extension_constructor ) =
45+ let type_decl : Types.type_declaration =
46+ {
47+ type_params = [] ;
48+ type_arity = 0 ;
49+ type_kind = Type_open ;
50+ type_private = Public ;
51+ type_manifest = Some (Ctype. newconstr ext.ext_type_path [] );
52+ type_variance = [] ;
53+ type_newtype_level = None ;
54+ type_loc = Location. none;
55+ type_attributes = [] ;
56+ type_immediate = false ;
57+ type_representation = Boxed ;
58+ type_shape = Unstated ;
59+ type_inlined_types = [] ;
60+ }
61+ in
62+ let stamp = Ident. binding_time id in
63+ let item =
64+ {
65+ Constructor. stamp;
66+ cname = name;
67+ args =
68+ (match ext.ext_args with
69+ | Cstr_tuple args ->
70+ Args (args |> List. map (fun t -> (t, Location. none)))
71+ | Cstr_record fields ->
72+ InlineRecord (fields |> List. map map_record_field));
73+ res = ext.ext_ret_type;
74+ type_decl = (Path. last ext.ext_type_path, type_decl);
75+ docstring = attrs_to_docstring ext.ext_attributes;
76+ deprecated =
77+ Process_attributes. find_deprecated_attribute ext.ext_attributes;
78+ }
79+ in
80+ let declared =
81+ Process_attributes. new_declared ~item ~extent: ext.ext_loc ~name ~stamp
82+ ~module_path: env.module_path true ext.ext_attributes
83+ in
84+ Stamps. add_constructor env.stamps stamp declared
85+
3886let rec for_type_signature_item ~(env : Shared_types.Env.t )
3987 ~(exported : Exported.t ) (item : Types.signature_item ) =
4088 match item with
@@ -165,6 +213,11 @@ let rec for_type_signature_item ~(env : Shared_types.Env.t)
165213 loc = declared.extent_loc;
166214 };
167215 ]
216+ | Sig_typext (id , ext , _ ) ->
217+ add_extension_constructor ~env ~id
218+ ~name: (Location. mkloc (Ident. name id) ext.ext_loc)
219+ ext;
220+ []
168221 | _ -> []
169222
170223and for_type_signature ~name ~env signature =
@@ -328,60 +381,11 @@ let for_type_declaration ~env ~(exported : Exported.t)
328381 loc = declared.extent_loc;
329382 }
330383
331- (* The constructors of [type t += ...] are registered as constructors of an
332- open type named [t], so that completion can offer them for values of
333- type [t]. They are not part of any type declaration, hence the stand-in
334- declaration that carries the kind and, as its manifest, the extended
335- type, which completion compares with the type it completes. *)
336- let add_extension_constructors ~(env : Env.t )
337- {Typedtree. tyext_path; tyext_constructors} =
338- let type_name = Path. last tyext_path in
339- let type_decl : Types.type_declaration =
340- {
341- type_params = [] ;
342- type_arity = 0 ;
343- type_kind = Type_open ;
344- type_private = Public ;
345- type_manifest = Some (Ctype. newconstr tyext_path [] );
346- type_variance = [] ;
347- type_newtype_level = None ;
348- type_loc = Location. none;
349- type_attributes = [] ;
350- type_immediate = false ;
351- type_representation = Boxed ;
352- type_shape = Unstated ;
353- type_inlined_types = [] ;
354- }
355- in
384+ let add_extension_constructors ~(env : Env.t ) {Typedtree. tyext_constructors} =
356385 tyext_constructors
357386 |> List. iter
358- (fun
359- ({ext_id; ext_name; ext_type; ext_loc; ext_attributes} :
360- Typedtree.extension_constructor )
361- ->
362- let stamp = Ident. binding_time ext_id in
363- let item =
364- {
365- Constructor. stamp;
366- cname = ext_name;
367- args =
368- (match ext_type.ext_args with
369- | Cstr_tuple args ->
370- Args (args |> List. map (fun t -> (t, Location. none)))
371- | Cstr_record fields ->
372- InlineRecord (fields |> List. map map_record_field));
373- res = ext_type.ext_ret_type;
374- type_decl = (type_name, type_decl);
375- docstring = attrs_to_docstring ext_attributes;
376- deprecated =
377- Process_attributes. find_deprecated_attribute ext_attributes;
378- }
379- in
380- let declared =
381- Process_attributes. new_declared ~item ~extent: ext_loc ~name: ext_name
382- ~stamp ~module_path: env.module_path true ext_attributes
383- in
384- Stamps. add_constructor env.stamps stamp declared)
387+ (fun ({ext_id; ext_name; ext_type} : Typedtree.extension_constructor ) ->
388+ add_extension_constructor ~env ~id: ext_id ~name: ext_name ext_type)
385389
386390let rec for_signature_item ~env ~(exported : Exported.t )
387391 (item : Typedtree.signature_item ) =
0 commit comments