From 228936183931990944441b25ab26811acec7868a Mon Sep 17 00:00:00 2001 From: trdthg Date: Fri, 5 Jul 2024 06:03:06 +0000 Subject: [PATCH] add test case --- src/lib/format_sail.ml | 13 +++++++------ test/format/comments.sail | 20 ++++++++++++++++++++ test/format/default/comments.expect | 20 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/lib/format_sail.ml b/src/lib/format_sail.ml index 0882c60a8..178e78a50 100644 --- a/src/lib/format_sail.ml +++ b/src/lib/format_sail.ml @@ -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 @@ -249,14 +249,14 @@ 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 @@ -264,7 +264,8 @@ module PPrintWrapper = struct 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 -> diff --git a/test/format/comments.sail b/test/format/comments.sail index 7a0ac0a29..a7d0993eb 100644 --- a/test/format/comments.sail +++ b/test/format/comments.sail @@ -1,3 +1,13 @@ +/*comment*/ +/* comment */ +/* first line comment + */ +/* first line comment + + */ +/* first line comment + + last line comment */ /* first line comment indent many @@ -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 diff --git a/test/format/default/comments.expect b/test/format/default/comments.expect index ebc9490ac..b139155ee 100644 --- a/test/format/default/comments.expect +++ b/test/format/default/comments.expect @@ -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*/ @@ -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*/