@@ -280,18 +280,35 @@ module Attribute = {
280280type description = option (string );
281281type attributes = list (Attribute . t );
282282
283- module IntMap = Map . Make (Int );
283+ module type OrderedComments = {
284+ type comment = (Typedtree . comment , description , attributes );
284285
285- type comments = {
286- mutable by_start_lnum:
287- IntMap . t ((Typedtree . comment , description , attributes )),
288- mutable by_end_lnum:
289- IntMap . t ((Typedtree . comment , description , attributes )),
286+ type comments ;
287+
288+ let comments : comments ;
289+
290+ let find_starting_on_lnum : int => option (comment );
291+ let find_ending_on_lnum : int => option (comment );
292+
293+ let iter : ((int , comment ) => unit ) => unit ;
290294};
291295
292- let comments = {by_start_lnum: IntMap . empty, by_end_lnum: IntMap . empty};
296+ module MakeOrderedComments =
297+ (Raw : {let comments : list (Typedtree . comment ); })
298+ : OrderedComments => {
299+ module IntMap = Map . Make (Int );
300+
301+ type comment = (Typedtree . comment , description , attributes );
302+
303+ type comments = {
304+ mutable by_start_lnum:
305+ IntMap . t ((Typedtree . comment , description , attributes )),
306+ mutable by_end_lnum:
307+ IntMap . t ((Typedtree . comment , description , attributes )),
308+ };
309+
310+ let comments = {by_start_lnum: IntMap . empty, by_end_lnum: IntMap . empty};
293311
294- let setup_comments = (raw_comments: list (Typedtree . comment )) => {
295312 List.iter(
296313 (comment: Typedtree.comment) => {
297314 let (start_lnum , end_lnum , data ) =
@@ -310,10 +327,23 @@ let setup_comments = (raw_comments: list(Typedtree.comment)) => {
310327 IntMap.add(start_lnum, data, comments.by_start_lnum);
311328 comments.by_end_lnum = IntMap.add(end_lnum, data, comments.by_end_lnum);
312329 },
313- raw_comments ,
330+ Raw . comments ,
314331 );
332+
333+ let find_starting_on_lnum = lnum =>
334+ IntMap . find_opt(lnum, comments. by_start_lnum);
335+ let find_ending_on_lnum = lnum =>
336+ IntMap . find_opt(lnum, comments. by_end_lnum);
337+
338+ let iter = fn => IntMap . iter(fn, comments. by_start_lnum);
315339};
316340
341+ let to_ordered = (comments): (module OrderedComments ) =>
342+ (module
343+ MakeOrderedComments ({
344+ let comments = comments;
345+ }));
346+
317347let start_line = (comment: Typedtree . comment ) => {
318348 switch (comment) {
319349 | Line ({cmt_loc})
@@ -333,17 +363,17 @@ let end_line = (comment: Typedtree.comment) => {
333363};
334364
335365module Doc = {
336- let starting_on_lnum = lnum => {
337- let data = IntMap . find_opt (lnum, comments . by_start_lnum );
366+ let starting_on = (~ lnum, module C : OrderedComments ) => {
367+ let data = C . find_starting_on_lnum (lnum);
338368 switch (data) {
339369 | Some ((Doc ({cmt_content}), _ , _ )) => data
340370 | _ => None
341371 };
342372 };
343373
344- let ending_on_lnum = lnum => {
374+ let ending_on = (~ lnum, module C : OrderedComments ) => {
345375 let rec ending_on_lnum_help = (lnum, check_prev) => {
346- let data = IntMap . find_opt (lnum, comments . by_end_lnum );
376+ let data = C . find_ending_on_lnum (lnum);
347377 switch (data) {
348378 | Some ((Doc ({cmt_content}), _ , _ )) => data
349379 // Hack to handle code that has an attribute on the line before, such as `@disableGC`
@@ -354,14 +384,12 @@ module Doc = {
354384 ending_on_lnum_help(lnum, true );
355385 };
356386
357- let find_module = () => {
387+ let find_module = (module C : OrderedComments ) => {
358388 let module_comments = ref ([] );
359- IntMap . iter(
360- (_, (_comment, _desc, attrs) as comment) =>
361- if (List . exists(Attribute . is_module, attrs)) {
362- module_comments := [ comment, ... module_comments^ ] ;
363- },
364- comments. by_start_lnum,
389+ C . iter((_, (_comment, _desc, attrs) as comment) =>
390+ if (List . exists(Attribute . is_module, attrs)) {
391+ module_comments := [ comment, ... module_comments^ ] ;
392+ }
365393 );
366394 if (List . length(module_comments^ ) > 1 ) {
367395 failwith ("More than one @module block is not supported" );
@@ -370,14 +398,12 @@ module Doc = {
370398 };
371399 };
372400
373- let find_sections = () => {
401+ let find_sections = (module C : OrderedComments ) => {
374402 let section_comments = ref ([] );
375- IntMap . iter(
376- (_, (_comment, _desc, attrs) as comment) =>
377- if (List . exists(Attribute . is_section, attrs)) {
378- section_comments := [ comment, ... section_comments^ ] ;
379- },
380- comments. by_start_lnum,
403+ C . iter((_, (_comment, _desc, attrs) as comment) =>
404+ if (List . exists(Attribute . is_section, attrs)) {
405+ section_comments := [ comment, ... section_comments^ ] ;
406+ }
381407 );
382408 List . rev(section_comments^ );
383409 };
0 commit comments