@@ -21,8 +21,16 @@ type t =
21
21
; mutable cmts_after : Cmt .t Multimap .M (Location ).t
22
22
; mutable cmts_within : Cmt .t Multimap .M (Location ).t
23
23
; source : Source .t
24
- ; mutable remaining : Set .M (Location ).t
25
- ; remove : bool }
24
+ ; mutable remaining : Set .M (Location ).t }
25
+
26
+ let copy {debug; cmts_before; cmts_after; cmts_within; source; remaining} =
27
+ {debug; cmts_before; cmts_after; cmts_within; source; remaining}
28
+
29
+ let restore src ~into =
30
+ into.cmts_before < - src.cmts_before ;
31
+ into.cmts_after < - src.cmts_after ;
32
+ into.cmts_within < - src.cmts_within ;
33
+ into.remaining < - src.remaining
26
34
27
35
let update_remaining t ~f = t.remaining < - f t.remaining
28
36
@@ -199,26 +207,24 @@ let rec place t loc_tree ?prev_loc locs cmts =
199
207
200
208
(* * Relocate comments, for Ast transformations such as sugaring. *)
201
209
let relocate (t : t ) ~src ~before ~after =
202
- if t.remove then (
203
- if t.debug then
204
- Caml.Format. eprintf " relocate %a to %a and %a@\n %!" Location. fmt src
205
- Location. fmt before Location. fmt after ;
206
- let merge_and_sort x y =
207
- List. rev_append x y
208
- |> List. sort
209
- ~compare: (Comparable. lift Location. compare_start ~f: Cmt. loc)
210
- in
211
- update_cmts t `Before
212
- ~f: (Multimap. update_multi ~src ~dst: before ~f: merge_and_sort) ;
213
- update_cmts t `After
214
- ~f: (Multimap. update_multi ~src ~dst: after ~f: merge_and_sort) ;
215
- update_cmts t `Within
216
- ~f: (Multimap. update_multi ~src ~dst: after ~f: merge_and_sort) ;
217
- if t.debug then
218
- update_remaining t ~f: (fun s ->
219
- let s = Set. remove s src in
220
- let s = Set. add s after in
221
- Set. add s before ) )
210
+ if t.debug then
211
+ Caml.Format. eprintf " relocate %a to %a and %a@\n %!" Location. fmt src
212
+ Location. fmt before Location. fmt after ;
213
+ let merge_and_sort x y =
214
+ List. rev_append x y
215
+ |> List. sort ~compare: (Comparable. lift Location. compare_start ~f: Cmt. loc)
216
+ in
217
+ update_cmts t `Before
218
+ ~f: (Multimap. update_multi ~src ~dst: before ~f: merge_and_sort) ;
219
+ update_cmts t `After
220
+ ~f: (Multimap. update_multi ~src ~dst: after ~f: merge_and_sort) ;
221
+ update_cmts t `Within
222
+ ~f: (Multimap. update_multi ~src ~dst: after ~f: merge_and_sort) ;
223
+ if t.debug then
224
+ update_remaining t ~f: (fun s ->
225
+ let s = Set. remove s src in
226
+ let s = Set. add s after in
227
+ Set. add s before )
222
228
223
229
let relocate_cmts_before (t : t ) ~src ~sep ~dst =
224
230
let f map =
@@ -281,8 +287,7 @@ let init fragment ~debug source asts comments_n_docstrings =
281
287
; cmts_after= Map. empty (module Location )
282
288
; cmts_within= Map. empty (module Location )
283
289
; source
284
- ; remaining= Set. empty (module Location )
285
- ; remove= true }
290
+ ; remaining= Set. empty (module Location ) }
286
291
in
287
292
let comments = Normalize. dedup_cmts fragment asts comments_n_docstrings in
288
293
if not (List. is_empty comments) then (
@@ -311,21 +316,23 @@ let init fragment ~debug source asts comments_n_docstrings =
311
316
Format. eprintf " @\n %a@\n @\n %!" dump loc_tree ) ) ;
312
317
t
313
318
314
- let preserve fmt_x t =
315
- let buf = Buffer. create 128 in
316
- let fs = Format. formatter_of_buffer buf in
317
- Fmt. eval fs (fmt_x {t with remove= false }) ;
318
- Format. pp_print_flush fs () ;
319
- Buffer. contents buf
319
+ let preserve f t =
320
+ let original = copy t in
321
+ let finally () = restore original ~into: t in
322
+ Exn. protect ~finally ~f: (fun () ->
323
+ let buf = Buffer. create 128 in
324
+ let fs = Format. formatter_of_buffer buf in
325
+ Fmt. eval fs (f () ) ;
326
+ Format. pp_print_flush fs () ;
327
+ Buffer. contents buf )
320
328
321
329
let pop_if_debug t loc =
322
- if t.debug && t.remove then
323
- update_remaining t ~f: (fun s -> Set. remove s loc)
330
+ if t.debug then update_remaining t ~f: (fun s -> Set. remove s loc)
324
331
325
332
let find_cmts t pos loc =
326
333
pop_if_debug t loc ;
327
334
let r = find_at_position t loc pos in
328
- if t.remove then update_cmts t pos ~f: (fun m -> Map. remove m loc) ;
335
+ update_cmts t pos ~f: (fun m -> Map. remove m loc) ;
329
336
r
330
337
331
338
let break_comment_group source margin {Cmt. loc = a ; _} {Cmt. loc = b ; _} =
0 commit comments