diff --git a/src/lib/chunk_ast.ml b/src/lib/chunk_ast.ml index 3f91e9fb6..6064dfe0b 100644 --- a/src/lib/chunk_ast.ml +++ b/src/lib/chunk_ast.ml @@ -433,6 +433,18 @@ let rec pop_comments ?(spacer = true) comments chunks l = | _ -> () end +let rec pop_comments_until_loc_end comments chunks l = + match Stack.top_opt comments with + | None -> () + | Some (Lexer.Comment (comment_type, comment_s, comment_e, contents)) -> begin + match Reporting.simp_loc l with + | Some (_, e) when comment_s.pos_cnum <= e.pos_cnum -> + let _ = Stack.pop comments in + Queue.add (Comment (comment_type, 0, comment_s.pos_cnum - comment_s.pos_bol, contents)) chunks; + pop_comments_until_loc_end comments chunks l + | _ -> () + end + let rec discard_comments comments (pos : Lexing.position) = match Stack.top_opt comments with | None -> () @@ -873,7 +885,13 @@ let rec chunk_exp comments chunks (E_aux (aux, l)) = let next_line_num = Option.bind next (fun bexp -> block_exp_locs bexp |> fst |> starting_line_num) in if have_linebreak (ending_line_num e_l) next_line_num || Option.is_none next then ignore (pop_trailing_comment comments chunks (ending_line_num e_l)); - + begin + match next with + | Some next -> + let next_s_l, _ = block_exp_locs block_exp in + pop_comments comments chunks next_s_l + | _ -> pop_comments_until_loc_end comments chunks l + end; (chunks, have_blank_linebreak (ending_line_num e_l) next_line_num) ) false block_exps @@ -1323,4 +1341,14 @@ let chunk_defs source comments defs = let chunks = Queue.create () in chunk_header_comments comments chunks defs; let _ = List.fold_left (fun last_span def -> chunk_def source last_span comments chunks def) (None, Some 0) defs in + + (* pop remaining comments *) + if not (Stack.is_empty comments) then Queue.add (Spacer (true, 1)) chunks; + Stack.iter + (fun c -> + match c with + | Lexer.Comment (comment_type, comment_s, e, contents) -> + Queue.add (Comment (comment_type, 0, comment_s.pos_cnum - comment_s.pos_bol, contents)) chunks + ) + comments; chunks diff --git a/test/format/comments.sail b/test/format/comments.sail new file mode 100644 index 000000000..e1f107d9b --- /dev/null +++ b/test/format/comments.sail @@ -0,0 +1,29 @@ +function a () -> int = { + *R = baz; // 000 + + 1//aaa + //bbb + //ccc + + /* dd + d */ + + } + +// eee + +function b () -> int = { + let a = { + 1 + // fff + }; + 1 +} + +let c = 1 // ggg0 + +// ggg1 +// ggg2 + +// ggg3 + diff --git a/test/format/default/comments.expect b/test/format/default/comments.expect new file mode 100644 index 000000000..6408f1b07 --- /dev/null +++ b/test/format/default/comments.expect @@ -0,0 +1,23 @@ +function a () -> int = { + *R = baz; // 000 + + 1 //aaa + //bbb + //ccc + /* dd + d */ +} + +// eee +function b () -> int = { + let a = { + 1 // fff + }; + 1 +} + +let c = + 1 // ggg0 +// ggg1 +// ggg2 +// ggg3