Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 4c5dcca

Browse files
authored
Improve debug logging (#554)
* Improve Res_comments_table.log * Improve Res_comment.toString
1 parent 326fb49 commit 4c5dcca

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed

src/res_comment.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ let isSingleLineComment t = match t.style with
2525
| MultiLine -> false
2626

2727
let toString t =
28+
let {Location.loc_start; loc_end} = t.loc in
2829
Format.sprintf
29-
"(txt: %s\nstyle: %s\nlines: %d-%d)"
30+
"(txt: %s\nstyle: %s\nlocation: %d,%d-%d,%d)"
3031
t.txt
3132
(styleToString t.style)
32-
t.loc.loc_start.pos_lnum
33-
t.loc.loc_end.pos_lnum
33+
loc_start.pos_lnum
34+
(loc_start.pos_cnum - loc_start.pos_bol)
35+
loc_end.pos_lnum
36+
(loc_end.pos_cnum - loc_end.pos_bol)
3437

3538
let makeSingleLineComment ~loc txt = {
3639
txt;

src/res_comments_table.ml

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,9 @@ let copy tbl = {
2121

2222
let empty = make ()
2323

24-
let log t =
24+
let printEntries tbl =
2525
let open Location in
26-
let leadingStuff = Hashtbl.fold (fun (k : Location.t) (v : Comment.t list) acc ->
27-
let loc = Doc.concat [
28-
Doc.lbracket;
29-
Doc.text (string_of_int k.loc_start.pos_lnum);
30-
Doc.text ":";
31-
Doc.text (string_of_int (k.loc_start.pos_cnum - k.loc_start.pos_bol));
32-
Doc.text "-";
33-
Doc.text (string_of_int k.loc_end.pos_lnum);
34-
Doc.text ":";
35-
Doc.text (string_of_int (k.loc_end.pos_cnum - k.loc_end.pos_bol));
36-
Doc.rbracket;
37-
] in
38-
let doc = Doc.breakableGroup ~forceBreak:true (
39-
Doc.concat [
40-
loc;
41-
Doc.indent (
42-
Doc.concat [
43-
Doc.line;
44-
Doc.join ~sep:Doc.comma (List.map (fun c -> Doc.text (Comment.txt c)) v)
45-
]
46-
);
47-
Doc.line;
48-
]
49-
) in
50-
doc::acc
51-
) t.leading []
52-
in
53-
let trailingStuff = Hashtbl.fold (fun (k : Location.t) (v : Comment.t list) acc ->
26+
Hashtbl.fold (fun (k : Location.t) (v : Comment.t list) acc ->
5427
let loc = Doc.concat [
5528
Doc.lbracket;
5629
Doc.text (string_of_int k.loc_start.pos_lnum);
@@ -75,18 +48,23 @@ let log t =
7548
]
7649
) in
7750
doc::acc
78-
) t.trailing []
79-
in
51+
) tbl []
52+
53+
let log t =
54+
let open Location in
55+
let leadingStuff = printEntries t.leading in
56+
let trailingStuff = printEntries t.trailing in
57+
let stuffInside = printEntries t.inside in
8058
Doc.breakableGroup ~forceBreak:true (
8159
Doc.concat [
8260
Doc.text "leading comments:";
61+
Doc.indent (Doc.concat [Doc.line; Doc.concat leadingStuff]);
8362
Doc.line;
84-
Doc.indent (Doc.concat leadingStuff);
85-
Doc.line;
63+
Doc.text "comments inside:";
64+
Doc.indent (Doc.concat [Doc.line; Doc.concat stuffInside]);
8665
Doc.line;
8766
Doc.text "trailing comments:";
88-
Doc.indent (Doc.concat trailingStuff);
89-
Doc.line;
67+
Doc.indent (Doc.concat [Doc.line; Doc.concat trailingStuff]);
9068
Doc.line;
9169
]
9270
) |> Doc.toString ~width:80 |> print_endline

0 commit comments

Comments
 (0)