Skip to content

Commit 97c7ce4

Browse files
authored
feat(graindoc)!: Only use original doc blocks when re-providing (#1687)
* feat(graindoc)!: Only use original doc blocks when re-providing * chore(stdlib): Move doc blocks to original definition * chore(stdlib): Regenerate markdown docs
1 parent 3b6b5ca commit 97c7ce4

File tree

16 files changed

+1067
-476
lines changed

16 files changed

+1067
-476
lines changed

compiler/graindoc/docblock.re

Lines changed: 35 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
184116
let 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+
287238
let 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

417363
let 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
}
561488
and 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
);

compiler/graindoc/graindoc.re

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ let compile_typed = (input: Fp.t(Fp.absolute)) => {
8080

8181
let generate_docs =
8282
(~current_version, ~output=?, program: Typedtree.typed_program) => {
83-
let comments = Comments.to_ordered(program.comments);
84-
85-
let provides = Docblock.enumerate_provides(program);
86-
8783
let signature_items = program.signature.cmi_sign;
8884

8985
let buf = Buffer.create(0);
@@ -93,8 +89,6 @@ let generate_docs =
9389

9490
let docblock =
9591
Docblock.for_signature_items(
96-
~comments,
97-
~provides,
9892
~module_namespace=None,
9993
~name=module_name,
10094
~loc=program.module_name.loc,

stdlib/bigint.gr

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,7 @@ from Numbers use {
1919
coerceBigIntToNumber as toNumber,
2020
}
2121

22-
/**
23-
* Converts a Number to a BigInt.
24-
*
25-
* @param number: The value to convert
26-
* @returns The Number represented as a BigInt
27-
*
28-
* @since v0.5.0
29-
*/
30-
provide { fromNumber }
31-
/**
32-
* Converts a BigInt to a Number.
33-
*
34-
* @param num: The value to convert
35-
* @returns The BigInt represented as a Number
36-
*
37-
* @since v0.5.0
38-
*/
39-
provide { toNumber }
22+
provide { fromNumber, toNumber }
4023

4124
/**
4225
* Increments the value by one.

stdlib/float32.gr

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,7 @@ provide let tau = 6.2831853f
6666
*/
6767
provide let e = 2.7182817f
6868

69-
/**
70-
* Converts a Number to a Float32.
71-
*
72-
* @param number: The value to convert
73-
* @returns The Number represented as a Float32
74-
*
75-
* @since v0.2.0
76-
*/
77-
provide { fromNumber }
78-
79-
/**
80-
* Converts a Float32 to a Number.
81-
*
82-
* @param float: The value to convert
83-
* @returns The Float32 represented as a Number
84-
*
85-
* @since v0.2.0
86-
*/
87-
provide { toNumber }
69+
provide { fromNumber, toNumber }
8870

8971
/**
9072
* Computes the sum of its operands.

stdlib/float64.gr

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,7 @@ provide let tau = 6.283185307179586d
7070
*/
7171
provide let e = 2.718281828459045d
7272

73-
/**
74-
* Converts a Number to a Float64.
75-
*
76-
* @param number: The value to convert
77-
* @returns The Number represented as a Float64
78-
*
79-
* @since v0.2.0
80-
*/
81-
provide { fromNumber }
82-
83-
/**
84-
* Converts a Float64 to a Number.
85-
*
86-
* @param float: The value to convert
87-
* @returns The Float64 represented as a Number
88-
*
89-
* @since v0.2.0
90-
*/
91-
provide { toNumber }
73+
provide { fromNumber, toNumber }
9274

9375
/**
9476
* Computes the sum of its operands.

stdlib/int32.gr

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,7 @@ from Numbers use {
2020
coerceInt32ToNumber as toNumber,
2121
}
2222

23-
/**
24-
* Converts a Number to an Int32.
25-
*
26-
* @param number: The value to convert
27-
* @returns The Number represented as an Int32
28-
*
29-
* @since v0.2.0
30-
*/
31-
provide { fromNumber }
32-
33-
/**
34-
* Converts an Int32 to a Number.
35-
*
36-
* @param value: The value to convert
37-
* @returns The Int32 represented as a Number
38-
*
39-
* @since v0.2.0
40-
*/
41-
provide { toNumber }
23+
provide { fromNumber, toNumber }
4224

4325
/**
4426
* Converts a Uint32 to an Int32.

stdlib/int64.gr

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,7 @@ from Numbers use {
2121
coerceInt64ToNumber as toNumber,
2222
}
2323

24-
/**
25-
* Converts a Number to an Int64.
26-
*
27-
* @param number: The value to convert
28-
* @returns The Number represented as an Int64
29-
*
30-
* @since v0.2.0
31-
*/
32-
provide { fromNumber }
33-
34-
/**
35-
* Converts an Int64 to a Number.
36-
*
37-
* @param value: The value to convert
38-
* @returns The Int64 represented as a Number
39-
*
40-
* @since v0.2.0
41-
*/
42-
provide { toNumber }
24+
provide { fromNumber, toNumber }
4325

4426
/**
4527
* Converts a Uint64 to an Int64.

0 commit comments

Comments
 (0)