Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg committed Jul 5, 2024
1 parent 6dbac8b commit 2289361
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/lib/format_sail.ml
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ module PPrintWrapper = struct
let rec loop min_indent lines =
match lines with
| line :: rest_of_lines ->
(* ignore empty line *)
(* Ignore empty line *)
if line = "" then loop min_indent rest_of_lines
else (
let indent = count_indent line in
Expand All @@ -249,22 +249,23 @@ module PPrintWrapper = struct

let patch_comment_lines_indent col lines =
let min_indent = count_lines_min_indent lines in
Printf.printf "min_ident: %d, col: %d, " min_indent col;
let right_indent_count = col - min_indent in
Printf.printf "right_indent_count: %d\n" right_indent_count;
let lines =
List.mapi
(fun i l ->
(* The first line remains unchanged *)
if i == 0 then l else if right_indent_count > 0 then String.make (abs right_indent_count) ' ' ^ l else l
(* The first_line or empty_line remains unchanged *)
if i == 0 || l = "" then l
else if right_indent_count > 0 then String.make (abs right_indent_count) ' ' ^ l
else l
)
lines
in
lines

let block_comment_lines col s =
let lines = Util.split_on_char '\n' s in
let lines = List.mapi (fun i l -> if i + 1 == List.length lines then l else rtrim l) lines in
(* Last line (before */) shouldn't be rtrimed *)
let lines = List.mapi (fun i l -> if i + 1 = List.length lines then l else rtrim l) lines in
let lines = patch_comment_lines_indent col lines in
List.mapi
(fun n line ->
Expand Down
20 changes: 20 additions & 0 deletions test/format/comments.sail
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*comment*/
/* comment */
/* first line comment
*/
/* first line comment

*/
/* first line comment

last line comment */

/* first line comment
indent many
Expand All @@ -11,6 +21,16 @@
indent many
last line comment*/
function a () -> int = {
/*comment*/
/* comment */
/* first line comment
*/
/* first line comment

*/
/* first line comment

last line comment */

/* first line comment
indent many
Expand Down
20 changes: 20 additions & 0 deletions test/format/default/comments.expect
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
/*comment*/
/* comment */
/* first line comment
*/

/* first line comment

*/
/* first line comment

last line comment */
/* first line comment
indent many
last line comment*/
Expand All @@ -9,6 +19,16 @@ indent many
indent many
last line comment*/
function a () -> int = {
/*comment*/
/* comment */
/* first line comment
*/
/* first line comment

*/
/* first line comment

last line comment */
/* first line comment
indent many
last line comment*/
Expand Down

0 comments on commit 2289361

Please sign in to comment.