@@ -265,23 +265,10 @@ let get_comments_to_end_of_line =
265265
266266let comment_to_doc = (comment: Parsetree . comment ) => {
267267 let comment_string = Comments . get_comment_source(comment);
268- let newline =
269- switch (comment) {
270- | Line (_ )
271- | Shebang (_ ) => Doc . hardLine
272- | Doc (_ ) => Doc . hardLine
273- | _ => Doc . nil
274- };
275- // this is needed for a couple of reasons. cmt_content doesn't include the comment delimiters (// or /*)
268+ // We use cmt_source over cmt_content, which doesn't include the comment delimiters (// or /*)
276269 // if we use cmt_source, it passes through the newline for line comments, which we don't want here
277270 // we want our own line/hardline formatting blocks
278271
279- Doc . concat([ Doc . text(String . trim(comment_string)), newline] );
280- };
281-
282- let nobreak_comment_to_doc = (comment: Parsetree . comment ) => {
283- let comment_string = Comments . get_comment_source(comment);
284-
285272 Doc . text(String . trim(comment_string));
286273};
287274
@@ -329,6 +316,12 @@ let rec comments_inner =
329316 | [] => [ Doc . nil]
330317 | [ cmt , ... rem ] => [
331318 comment_to_doc(cmt),
319+ switch (cmt) {
320+ | Line (_ )
321+ | Shebang (_ ) => Doc . hardLine
322+ | Doc (_ ) => Doc . hardLine
323+ | _ => Doc . nil
324+ },
332325 ... comments_inner(~prev= cmt, rem),
333326 ]
334327 }
@@ -347,6 +340,12 @@ let rec comments_inner =
347340 | 0 => [
348341 Doc . space,
349342 comment_to_doc(cmt),
343+ switch (cmt) {
344+ | Line (_ )
345+ | Shebang (_ ) => Doc . hardLine
346+ | Doc (_ ) => Doc . hardLine
347+ | _ => Doc . nil
348+ },
350349 ... comments_inner(~prev= cmt, rem),
351350 ]
352351
@@ -356,6 +355,12 @@ let rec comments_inner =
356355 | _ => Doc . hardLine
357356 },
358357 comment_to_doc(cmt),
358+ switch (cmt) {
359+ | Line (_ )
360+ | Shebang (_ ) => Doc . hardLine
361+ | Doc (_ ) => Doc . hardLine
362+ | _ => Doc . nil
363+ },
359364 ... comments_inner(~prev= cmt, rem),
360365 ]
361366 | _ => [
@@ -367,6 +372,12 @@ let rec comments_inner =
367372 },
368373 Doc . hardLine,
369374 comment_to_doc(cmt),
375+ switch (cmt) {
376+ | Line (_ )
377+ | Shebang (_ ) => Doc . hardLine
378+ | Doc (_ ) => Doc . hardLine
379+ | _ => Doc . nil
380+ },
370381 ... comments_inner(~prev= cmt, rem),
371382 ]
372383 };
@@ -418,33 +429,55 @@ let rec trailing_comments_inner =
418429 | None =>
419430 switch (comments) {
420431 | [] => []
421- | [ cmt ] => [ nobreak_comment_to_doc (cmt)]
432+ | [ cmt ] => [ comment_to_doc (cmt)]
422433 | [ cmt , ... rem ] => [
423434 comment_to_doc(cmt),
435+ switch (cmt) {
436+ | Line (_ )
437+ | Shebang (_ ) => Doc . hardLine
438+ | Doc (_ ) => Doc . hardLine
439+ | _ => Doc . nil
440+ },
424441 ... trailing_comments_inner(~prev= cmt, rem),
425442 ]
426443 }
427444 | Some (prev_cmt ) =>
445+ let (_ , prev_line , _ , _ ) =
446+ Locations . get_raw_pos_info(
447+ Locations . get_comment_loc(prev_cmt).loc_end,
448+ );
449+
428450 switch (comments) {
429451 | [] => []
430- | [ cmt , ... rem ] =>
431- let (_ , prev_line , _ , _ ) =
432- Locations . get_raw_pos_info(
433- Locations . get_comment_loc(prev_cmt).loc_end,
434- );
452+ | [ cmt ] =>
435453 let (_ , this_line , _ , _ ) =
436454 Locations . get_raw_pos_info(Locations . get_comment_loc(cmt).loc_start);
437455
438- let comment_printer =
439- switch (rem) {
440- | [] => nobreak_comment_to_doc
441- | _ => comment_to_doc
442- };
456+ switch (this_line - prev_line) {
457+ | 0 => [ Doc . space, comment_to_doc(cmt)]
458+ | 1 => [
459+ switch (prev_cmt) {
460+ | Line (_ ) => Doc . nil
461+ | _ => Doc . hardLine
462+ },
463+ comment_to_doc(cmt),
464+ ]
465+ | _ => [ Doc . hardLine, comment_to_doc(cmt)]
466+ };
467+ | [ cmt , ... rem ] =>
468+ let (_ , this_line , _ , _ ) =
469+ Locations . get_raw_pos_info(Locations . get_comment_loc(cmt).loc_start);
443470
444471 switch (this_line - prev_line) {
445472 | 0 => [
446473 Doc . space,
447- comment_printer(cmt),
474+ comment_to_doc(cmt),
475+ switch (cmt) {
476+ | Line (_ )
477+ | Shebang (_ ) => Doc . hardLine
478+ | Doc (_ ) => Doc . hardLine
479+ | _ => Doc . nil
480+ },
448481 ... trailing_comments_inner(~prev= cmt, rem),
449482 ]
450483
@@ -453,16 +486,28 @@ let rec trailing_comments_inner =
453486 | Line (_ ) => Doc . nil
454487 | _ => Doc . hardLine
455488 },
456- comment_printer(cmt),
489+ comment_to_doc(cmt),
490+ switch (cmt) {
491+ | Line (_ )
492+ | Shebang (_ ) => Doc . hardLine
493+ | Doc (_ ) => Doc . hardLine
494+ | _ => Doc . nil
495+ },
457496 ... trailing_comments_inner(~prev= cmt, rem),
458497 ]
459498 | _ => [
460499 Doc . hardLine,
461- comment_printer(cmt),
500+ comment_to_doc(cmt),
501+ switch (cmt) {
502+ | Line (_ )
503+ | Shebang (_ ) => Doc . hardLine
504+ | Doc (_ ) => Doc . hardLine
505+ | _ => Doc . nil
506+ },
462507 ... trailing_comments_inner(~prev= cmt, rem),
463508 ]
464509 };
465- }
510+ };
466511 };
467512};
468513
@@ -480,7 +525,7 @@ let single_line_of_comments = (comments: list(Parsetree.comment)) =>
480525 Doc . space,
481526 Doc . join(
482527 ~sep= Doc . space,
483- List . map(c => {nobreak_comment_to_doc (c)}, comments),
528+ List . map(c => {comment_to_doc (c)}, comments),
484529 ),
485530 ] )
486531 };
@@ -496,6 +541,12 @@ let rec new_comments_inner =
496541 | [] => []
497542 | [ cmt , ... rem ] => [
498543 comment_to_doc(cmt),
544+ switch (cmt) {
545+ | Line (_ )
546+ | Shebang (_ ) => Doc . hardLine
547+ | Doc (_ ) => Doc . hardLine
548+ | _ => Doc . nil
549+ },
499550 ... new_comments_inner(~prev= cmt, rem),
500551 ]
501552 }
@@ -514,6 +565,12 @@ let rec new_comments_inner =
514565 | 0 => [
515566 Doc . space,
516567 comment_to_doc(cmt),
568+ switch (cmt) {
569+ | Line (_ )
570+ | Shebang (_ ) => Doc . hardLine
571+ | Doc (_ ) => Doc . hardLine
572+ | _ => Doc . nil
573+ },
517574 ... new_comments_inner(~prev= cmt, rem),
518575 ]
519576
@@ -524,6 +581,12 @@ let rec new_comments_inner =
524581 | _ => Doc . hardLine
525582 },
526583 comment_to_doc(cmt),
584+ switch (cmt) {
585+ | Line (_ )
586+ | Shebang (_ ) => Doc . hardLine
587+ | Doc (_ ) => Doc . hardLine
588+ | _ => Doc . nil
589+ },
527590 ... new_comments_inner(~prev= cmt, rem),
528591 ]
529592 | _ => [
@@ -535,6 +598,12 @@ let rec new_comments_inner =
535598 },
536599 Doc . hardLine,
537600 comment_to_doc(cmt),
601+ switch (cmt) {
602+ | Line (_ )
603+ | Shebang (_ ) => Doc . hardLine
604+ | Doc (_ ) => Doc . hardLine
605+ | _ => Doc . nil
606+ },
538607 ... new_comments_inner(~prev= cmt, rem),
539608 ]
540609 };
0 commit comments