Skip to content

Commit

Permalink
FIx formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg committed May 17, 2024
1 parent cb0a396 commit c0cd419
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/lib/chunk_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> ()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions test/format/comments.sail
Original file line number Diff line number Diff line change
@@ -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

23 changes: 23 additions & 0 deletions test/format/default/comments.expect
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c0cd419

Please sign in to comment.