@@ -113,74 +113,6 @@ let () =
113113 }
114114 });
115115
116- let enumerate_provides = program => {
117- let id_tbl = ref (Ident . empty);
118-
119- let rec pattern_ids = ({pat_desc, pat_loc}: Typedtree . pattern ) => {
120- switch (pat_desc) {
121- | TPatVar (id , _ ) => [ (id, pat_loc)]
122- | TPatAlias (subpat , id , _ ) => [ (id, pat_loc), ... pattern_ids(subpat)]
123- | TPatTuple (pats )
124- | TPatArray (pats )
125- | TPatConstruct (_ , _ , pats ) => List . concat(List . map(pattern_ids, pats))
126- | TPatRecord (elts , _ ) =>
127- List . concat(List . map(((_, _, pat)) => pattern_ids(pat), elts))
128- | _ => []
129- };
130- };
131-
132- module ProvideIterator =
133- TypedtreeIter . MakeIterator ({
134- include TypedtreeIter . DefaultIteratorArgument ;
135-
136- let enter_toplevel_stmt =
137- ({ttop_desc, ttop_attributes}: Typedtree . toplevel_stmt ) => {
138- switch (ttop_desc) {
139- | TTopData (decls ) =>
140- List . iter(
141- ({data_id, data_loc}: Typedtree . data_declaration ) => {
142- id_tbl := Ident . add(data_id, data_loc, id_tbl^ )
143- },
144- decls,
145- )
146- | TTopProvide (decls ) =>
147- List . iter(
148- ({tex_id, tex_loc}: Typedtree . provide_declaration ) => {
149- id_tbl := Ident . add(tex_id, tex_loc, id_tbl^ )
150- },
151- decls,
152- )
153- | TTopForeign ({tvd_id, tvd_loc}) =>
154- id_tbl := Ident . add(tvd_id, tvd_loc, id_tbl^ )
155- | TTopLet (_ , _ , vbinds ) =>
156- List . iter(
157- ({vb_pat}: Typedtree . value_binding ) => {
158- List . iter(
159- ((id, loc)) => {id_tbl := Ident . add(id, loc, id_tbl^ )},
160- pattern_ids(vb_pat),
161- )
162- },
163- vbinds,
164- )
165- | TTopModule ({tmod_id, tmod_loc}) =>
166- id_tbl := Ident . add(tmod_id, tmod_loc, id_tbl^ )
167- | TTopInclude (_ )
168- | TTopException (_ )
169- | TTopExpr (_ ) => ()
170- };
171- };
172- });
173-
174- ProvideIterator . iter_typed_program(program);
175-
176- id_tbl^;
177- };
178-
179- let location_for_ident =
180- (~provides: Ident . tbl (Grain_parsing . Location . t ), ident) => {
181- snd(Ident . find_name(Ident . name(ident), provides));
182- };
183-
184116let title_for_api = (~module_namespace, name) => {
185117 switch (module_namespace) {
186118 | Some (module_namespace ) =>
@@ -284,15 +216,29 @@ let lookup_type_expr = (~idx, type_exprs) => {
284216 Option . bind(type_exprs, te => List . nth_opt(te, idx));
285217};
286218
219+ let get_comments_from_loc = (loc: Grain_parsing . Location . t ) => {
220+ open Compile ;
221+
222+ let comments =
223+ switch (
224+ compile_file(
225+ ~is_root_file= true ,
226+ ~hook= stop_after_parse,
227+ loc. loc_start. pos_fname,
228+ )
229+ ) {
230+ | exception exn => []
231+ | {cstate_desc: Parsed (parsed_program )} => parsed_program. comments
232+ | _ => failwith ("Invalid compilation state" )
233+ };
234+
235+ Comments . to_ordered(comments);
236+ };
237+
287238let for_value_description =
288- (
289- ~comments,
290- ~provides,
291- ~module_namespace,
292- ~ident: Ident . t ,
293- vd: Types . value_description ,
294- ) => {
295- let loc = location_for_ident(~provides, ident);
239+ (~module_namespace, ~ident: Ident . t , vd: Types . value_description ) => {
240+ let loc = vd. val_loc;
241+ let comments = get_comments_from_loc(loc);
296242 let name = Format . asprintf("% a " , Printtyp . ident, ident);
297243 let type_sig = Printtyp . string_of_value_description(~ident, vd);
298244 let comment =
@@ -415,14 +361,9 @@ let for_value_description =
415361};
416362
417363let for_type_declaration =
418- (
419- ~comments,
420- ~provides,
421- ~module_namespace,
422- ~ident: Ident . t ,
423- td: Types . type_declaration ,
424- ) => {
425- let loc = location_for_ident(~provides, ident);
364+ (~module_namespace, ~ident: Ident . t , td: Types . type_declaration ) => {
365+ let loc = td. type_loc;
366+ let comments = get_comments_from_loc(loc);
426367 let name = Format . asprintf("% a " , Printtyp . ident, ident);
427368 let type_sig = Printtyp . string_of_type_declaration(~ident, td);
428369 let comment =
@@ -489,8 +430,7 @@ let for_type_declaration =
489430 });
490431};
491432
492- let rec traverse_signature_items =
493- (~comments, ~provides, ~module_namespace, signature_items) => {
433+ let rec traverse_signature_items = (~module_namespace, signature_items) => {
494434 let {provided_types, provided_values, provided_modules} =
495435 List . fold_left(
496436 (
@@ -499,43 +439,30 @@ let rec traverse_signature_items =
499439 ) => {
500440 switch (sig_item) {
501441 | TSigType (ident , td , _ ) =>
502- let docblock =
503- for_type_declaration(
504- ~comments,
505- ~provides,
506- ~module_namespace,
507- ~ident,
508- td,
509- );
442+ let docblock = for_type_declaration(~module_namespace, ~ident, td);
510443 {
511444 provided_types: [ docblock, ... provided_types] ,
512445 provided_values,
513446 provided_modules,
514447 };
515448 | TSigValue (ident , vd ) =>
516- let docblock =
517- for_value_description(
518- ~comments,
519- ~provides,
520- ~module_namespace,
521- ~ident,
522- vd,
523- );
449+ let docblock = for_value_description(~module_namespace, ~ident, vd);
524450 {
525451 provided_types,
526452 provided_values: [ docblock, ... provided_values] ,
527453 provided_modules,
528454 };
529- | TSigModule (ident , {md_type: TModSignature (signature_items )}, _ ) =>
530- let loc = location_for_ident(~provides, ident);
455+ | TSigModule (
456+ ident ,
457+ {md_type: TModSignature (signature_items ), md_loc},
458+ _ ,
459+ ) =>
531460 let name = Format . asprintf("% a " , Printtyp . ident, ident);
532461 let docblock =
533462 for_signature_items(
534- ~comments,
535- ~provides,
536463 ~module_namespace,
537464 ~name,
538- ~loc,
465+ ~loc= md_loc ,
539466 signature_items,
540467 );
541468 {
@@ -560,13 +487,12 @@ let rec traverse_signature_items =
560487}
561488and for_signature_items =
562489 (
563- ~comments,
564- ~provides,
565490 ~module_namespace,
566491 ~name,
567492 ~loc: Grain_parsing . Location . t ,
568493 signature_items,
569494 ) => {
495+ let comments = get_comments_from_loc(loc);
570496 let comment =
571497 Comments . Doc . ending_on(~lnum= loc. loc_start. pos_lnum - 1 , comments);
572498
@@ -626,8 +552,6 @@ and for_signature_items =
626552 let namespace = title_for_namepace(~module_namespace, name);
627553
628554 traverse_signature_items(
629- ~comments,
630- ~provides,
631555 ~module_namespace= Some (namespace),
632556 signature_items,
633557 );
0 commit comments