Skip to content

Commit 2ae02fb

Browse files
author
Guillaume Petiot
authored
Display comments in the loc_tree for debugging (ocaml-ppx#1665)
1 parent be14af3 commit 2ae02fb

File tree

6 files changed

+161
-79
lines changed

6 files changed

+161
-79
lines changed

lib/Cmts.ml

+22-44
Original file line numberDiff line numberDiff line change
@@ -158,34 +158,10 @@ end = struct
158158
| _ -> (empty, cmts)
159159
end
160160

161-
let position_to_string = function
162-
| `Before -> "before"
163-
| `After -> "after"
164-
| `Within -> "within"
165-
166-
let add_cmts t ?prev ?next position loc cmts =
167-
if not (CmtSet.is_empty cmts) then (
161+
let add_cmts t position loc cmts =
162+
if not (CmtSet.is_empty cmts) then
168163
let cmtl = CmtSet.to_list cmts in
169-
if t.debug then
170-
List.iter cmtl ~f:(fun {Cmt.txt= cmt_txt; loc= cmt_loc} ->
171-
let string_between (l1 : Location.t) (l2 : Location.t) =
172-
match Source.string_between t.source l1.loc_end l2.loc_start with
173-
| None -> "swapped"
174-
| Some s -> s
175-
in
176-
let btw_prev =
177-
Option.value_map prev ~default:"no prev"
178-
~f:(Fn.flip string_between cmt_loc)
179-
in
180-
let btw_next =
181-
Option.value_map next ~default:"no next"
182-
~f:(string_between cmt_loc)
183-
in
184-
Caml.Format.eprintf "add %s %a: %a \"%s\" %s \"%s\"@\n%!"
185-
(position_to_string position)
186-
Location.fmt loc Location.fmt cmt_loc (String.escaped btw_prev)
187-
cmt_txt (String.escaped btw_next) ) ;
188-
update_cmts t position ~f:(Map.add_exn ~key:loc ~data:cmtl) )
164+
update_cmts t position ~f:(Map.add_exn ~key:loc ~data:cmtl)
189165

190166
(** Traverse the location tree from locs, find the deepest location that
191167
contains each comment, intersperse comments between that location's
@@ -205,19 +181,17 @@ let rec place t loc_tree ?prev_loc locs cmts =
205181
let after_prev, before_curr =
206182
CmtSet.partition t.source ~prev:prev_loc ~next:curr_loc before
207183
in
208-
add_cmts t `After ~prev:prev_loc ~next:curr_loc prev_loc
209-
after_prev ;
184+
add_cmts t `After prev_loc after_prev ;
210185
before_curr
211186
in
212-
add_cmts t `Before ?prev:prev_loc ~next:curr_loc curr_loc before_curr ;
187+
add_cmts t `Before curr_loc before_curr ;
213188
( match Loc_tree.children loc_tree curr_loc with
214-
| [] ->
215-
add_cmts t `Within ?prev:prev_loc ~next:curr_loc curr_loc within
189+
| [] -> add_cmts t `Within curr_loc within
216190
| children -> place t loc_tree children within ) ;
217191
place t loc_tree ~prev_loc:curr_loc next_locs after
218192
| [] -> (
219193
match prev_loc with
220-
| Some prev_loc -> add_cmts t `After ~prev:prev_loc prev_loc cmts
194+
| Some prev_loc -> add_cmts t `After prev_loc cmts
221195
| None ->
222196
if t.debug then
223197
List.iter (CmtSet.to_list cmts) ~f:(fun {Cmt.txt; _} ->
@@ -311,26 +285,30 @@ let init fragment ~debug source asts comments_n_docstrings =
311285
; remove= true }
312286
in
313287
let comments = Normalize.dedup_cmts fragment asts comments_n_docstrings in
314-
if debug then (
315-
Format.eprintf "\nComments:\n%!" ;
316-
List.iter comments ~f:(fun {Cmt.txt; loc} ->
317-
Caml.Format.eprintf "%a %s %s@\n%!" Location.fmt loc txt
318-
(if Source.ends_line source loc then "eol" else "") ) ) ;
319288
if not (List.is_empty comments) then (
320289
let loc_tree, locs = Loc_tree.of_ast fragment asts source in
321290
if debug then
322291
List.iter locs ~f:(fun loc ->
323292
if not (Location.compare loc Location.none = 0) then
324293
update_remaining t ~f:(fun s -> Set.add s loc) ) ;
325-
if debug then (
326-
let dump fs lt = Fmt.eval fs (Loc_tree.dump lt) in
327-
Format.eprintf "\nLoc_tree:\n%!" ;
328-
Format.eprintf "@\n%a@\n@\n%!" dump loc_tree ) ;
329294
let locs = Loc_tree.roots loc_tree in
330295
let cmts = CmtSet.of_list comments in
331-
match locs with
332-
| [] -> add_cmts t `After ~prev:Location.none Location.none cmts
296+
( match locs with
297+
| [] -> add_cmts t `After Location.none cmts
333298
| _ -> place t loc_tree locs cmts ) ;
299+
if debug then (
300+
let dump fs lt =
301+
let get_cmts pos loc =
302+
let cmts = find_at_position t loc pos in
303+
Option.map cmts ~f:(fun cmts -> List.map cmts ~f:Cmt.txt)
304+
in
305+
let cmts_before = get_cmts `Before in
306+
let cmts_within = get_cmts `Within in
307+
let cmts_after = get_cmts `After in
308+
Fmt.eval fs (Loc_tree.dump ~cmts_before ~cmts_within ~cmts_after lt)
309+
in
310+
Format.eprintf "\nLoc_tree:\n%!" ;
311+
Format.eprintf "@\n%a@\n@\n%!" dump loc_tree ) ) ;
334312
t
335313

336314
let preserve fmt_x t =

lib/Non_overlapping_interval_tree.ml

+19-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ module type S = sig
3030

3131
val children : t -> itv -> itv list
3232

33-
val dump : t -> Fmt.t
33+
val dump :
34+
?cmts_before:(itv -> string list option)
35+
-> ?cmts_within:(itv -> string list option)
36+
-> ?cmts_after:(itv -> string list option)
37+
-> t
38+
-> Fmt.t
3439
(** Debug: dump debug representation of tree. *)
3540
end
3641

@@ -95,14 +100,25 @@ module Make (Itv : IN) = struct
95100

96101
let children {map; _} elt = Option.value ~default:[] (Map.find map elt)
97102

98-
let dump tree =
103+
let dump ?cmts_before ?cmts_within ?cmts_after tree =
99104
let open Fmt in
105+
let dump_cmts lbl cmts loc =
106+
opt cmts (fun find ->
107+
opt (find loc) (fun cmts ->
108+
break 1 8
109+
$ list cmts "@;<1 8>" (fun k ->
110+
str lbl $ str ": " $ wrap "(*" "*)" (str k) ) ) )
111+
in
100112
let rec dump_ tree roots =
101113
vbox 0
102114
(list roots "@," (fun root ->
103115
let children = children tree root in
104116
vbox 1
105-
( str (Sexp.to_string_hum (Itv.comparator.sexp_of_t root))
117+
( vbox 0
118+
( str (Sexp.to_string_hum (Itv.comparator.sexp_of_t root))
119+
$ dump_cmts "before" cmts_before root
120+
$ dump_cmts "within" cmts_within root
121+
$ dump_cmts "after" cmts_after root )
106122
$ wrap_if
107123
(not (List.is_empty children))
108124
"@,{" " }" (dump_ tree children) ) ) )

lib/Non_overlapping_interval_tree.mli

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ module type S = sig
3434

3535
val children : t -> itv -> itv list
3636

37-
val dump : t -> Fmt.t
37+
val dump :
38+
?cmts_before:(itv -> string list option)
39+
-> ?cmts_within:(itv -> string list option)
40+
-> ?cmts_after:(itv -> string list option)
41+
-> t
42+
-> Fmt.t
3843
(** Debug: dump debug representation of tree. *)
3944
end
4045

lib/Source.ml

-13
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ let create ~text ~tokens =
2222
in
2323
{text; tokens= Array.of_list tokens}
2424

25-
let string_between (t : t) (p1 : Lexing.position) (p2 : Lexing.position) =
26-
let pos = p1.pos_cnum in
27-
let len = Position.distance p1 p2 in
28-
if
29-
len < 0 || pos < 0
30-
(* can happen e.g. if comment is within a parenthesized expression *)
31-
then None
32-
else if
33-
String.length t.text < pos + len
34-
(* can happen e.g. if source is not available *)
35-
then None
36-
else Some (String.sub t.text ~pos ~len)
37-
3825
let string_at t (l : Location.t) =
3926
let pos = l.loc_start.Lexing.pos_cnum
4027
and len = Position.distance l.loc_start l.loc_end in

lib/Source.mli

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ val empty_line_before : t -> Location.t -> bool
2525

2626
val empty_line_after : t -> Location.t -> bool
2727

28-
val string_between : t -> Lexing.position -> Lexing.position -> string option
29-
3028
val tokens_between :
3129
t
3230
-> filter:(Parser.token -> bool)

test/cli/debug.t

+114-16
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,150 @@
77

88
$ ocamlformat --debug a.ml
99

10-
Comments:
11-
([1,0+0]..[1,0+33]) Intentionally not formatted eol
12-
1310
Loc_tree:
1411
"([2,34+0]..[4,59+7])"
12+
before: (* Intentionally not formatted *)
1513
{"([2,34+4]..[2,34+6])"
1614
"([3,43+2]..[4,59+7])"
1715
{"([3,43+2]..[3,43+15])"
1816
"([4,59+4]..[4,59+7])" } }
1917

20-
add before ([2,34+0]..[4,59+7]): ([1,0+0]..[1,0+33]) "no prev" Intentionally not formatted "\n"
21-
22-
Comments:
23-
([1,0+0]..[1,0+33]) Intentionally not formatted eol
2418

2519
Loc_tree:
2620
"([2,34+0]..[4,59+7])"
21+
before: (* Intentionally not formatted *)
2722
{"([2,34+4]..[2,34+6])"
2823
"([3,43+2]..[4,59+7])"
2924
{"([3,43+2]..[3,43+15])"
3025
"([4,59+4]..[4,59+7])" } }
3126

32-
add before ([2,34+0]..[4,59+7]): ([1,0+0]..[1,0+33]) "no prev" Intentionally not formatted "\n"
33-
34-
Comments:
35-
([1,0+0]..[1,0+33]) Intentionally not formatted eol
3627

3728
Loc_tree:
3829
"([2,34+0]..[2,34+26])"
30+
before: (* Intentionally not formatted *)
3931
{"([2,34+4]..[2,34+6])"
4032
"([2,34+9]..[2,34+26])"
4133
{"([2,34+9]..[2,34+22])"
4234
"([2,34+23]..[2,34+26])" } }
4335

44-
add before ([2,34+0]..[2,34+26]): ([1,0+0]..[1,0+33]) "no prev" Intentionally not formatted "\n"
45-
46-
Comments:
47-
([1,0+0]..[1,0+33]) Intentionally not formatted eol
4836

4937
Loc_tree:
5038
"([2,34+0]..[2,34+26])"
39+
before: (* Intentionally not formatted *)
5140
{"([2,34+4]..[2,34+6])"
5241
"([2,34+9]..[2,34+26])"
5342
{"([2,34+9]..[2,34+22])"
5443
"([2,34+23]..[2,34+26])" } }
5544

56-
add before ([2,34+0]..[2,34+26]): ([1,0+0]..[1,0+33]) "no prev" Intentionally not formatted "\n"
5745
(* Intentionally not formatted *)
5846
let () = print_endline A.x
47+
48+
$ cat > a.ml << EOF
49+
> (* before let-binding *)
50+
> let () =
51+
> (* before x binding #1 *)
52+
> (* before x binding #2 *)
53+
> let (* before x *) x (* after x #1 *) (* after x #2 *) = (* before unit *) ( (* within unit #1 *) (* within unit #2 *) ) (* after unit *) in
54+
> x
55+
> (* after let-binding *)
56+
> EOF
57+
58+
$ ocamlformat --debug a.ml
59+
60+
Loc_tree:
61+
"([2,25+0]..[6,233+3])"
62+
before: (* before let-binding *)
63+
after: (* after let-binding *)
64+
{"([2,25+4]..[2,25+6])"
65+
"([5,90+2]..[6,233+3])"
66+
before: (* before x binding #1 *)
67+
before: (* before x binding #2 *)
68+
{"([5,90+2]..[5,90+122])"
69+
after: (* after unit *)
70+
{"([5,90+21]..[5,90+22])"
71+
before: (* before x *)
72+
after: (* after x #1 *)
73+
after: (* after x #2 *)
74+
"([5,90+77]..[5,90+122])"
75+
before: (* before unit *)
76+
within: (* within unit #1 *)
77+
within: (* within unit #2 *) }
78+
"([6,233+2]..[6,233+3])" } }
79+
80+
81+
Loc_tree:
82+
"([2,25+0]..[6,233+3])"
83+
before: (* before let-binding *)
84+
after: (* after let-binding *)
85+
{"([2,25+4]..[2,25+6])"
86+
"([5,90+2]..[6,233+3])"
87+
before: (* before x binding #1 *)
88+
before: (* before x binding #2 *)
89+
{"([5,90+2]..[5,90+122])"
90+
after: (* after unit *)
91+
{"([5,90+21]..[5,90+22])"
92+
before: (* before x *)
93+
after: (* after x #1 *)
94+
after: (* after x #2 *)
95+
"([5,90+77]..[5,90+122])"
96+
before: (* before unit *)
97+
within: (* within unit #1 *)
98+
within: (* within unit #2 *) }
99+
"([6,233+2]..[6,233+3])" } }
100+
101+
102+
Loc_tree:
103+
"([2,25+0]..[13,265+3])"
104+
before: (* before let-binding *)
105+
after: (* after let-binding *)
106+
{"([2,25+4]..[2,25+6])"
107+
"([5,90+2]..[13,265+3])"
108+
before: (* before x binding #1 *)
109+
before: (* before x binding #2 *)
110+
{"([5,90+2]..[10,210+28])"
111+
after: (* after unit *)
112+
{"([5,90+21]..[5,90+22])"
113+
before: (* before x *)
114+
after: (* after x #1 *)
115+
after: (* after x #2 *)
116+
"([9,183+4]..[10,210+28])"
117+
before: (* before unit *)
118+
within: (* within unit #1 *)
119+
within: (* within unit #2 *) }
120+
"([13,265+2]..[13,265+3])" } }
121+
122+
123+
Loc_tree:
124+
"([2,25+0]..[13,265+3])"
125+
before: (* before let-binding *)
126+
after: (* after let-binding *)
127+
{"([2,25+4]..[2,25+6])"
128+
"([5,90+2]..[13,265+3])"
129+
before: (* before x binding #1 *)
130+
before: (* before x binding #2 *)
131+
{"([5,90+2]..[10,210+28])"
132+
after: (* after unit *)
133+
{"([5,90+21]..[5,90+22])"
134+
before: (* before x *)
135+
after: (* after x #1 *)
136+
after: (* after x #2 *)
137+
"([9,183+4]..[10,210+28])"
138+
before: (* before unit *)
139+
within: (* within unit #1 *)
140+
within: (* within unit #2 *) }
141+
"([13,265+2]..[13,265+3])" } }
142+
143+
(* before let-binding *)
144+
let () =
145+
(* before x binding #1 *)
146+
(* before x binding #2 *)
147+
let (* before x *) x
148+
(* after x #1 *)
149+
(* after x #2 *) =
150+
(* before unit *)
151+
( (* within unit #1 *)
152+
(* within unit #2 *) )
153+
(* after unit *)
154+
in
155+
x
156+
(* after let-binding *)

0 commit comments

Comments
 (0)