Source file cr_comment0.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
module Digest_hex = struct
type t = string
let compare = String.compare
let equal = String.equal
let to_dyn = Dyn.string
let to_string t = t
let create str = str |> Digest.string |> Digest.to_hex
end
let or_dyn to_dyn r =
match r with
| Ok ok -> Dyn.Variant ("Ok", [ to_dyn ok ])
| Error dyn -> Dyn.Variant ("Error", [ dyn ])
;;
module T = struct
[@@@coverage off]
type t =
{ path : Vcs.Path_in_repo.t
; whole_loc : Loc.t
; content_start_offset : int
; header : (Header.t, Dyn.t) Result.t
; comment_prefix : string
; digest_of_condensed_content : Digest_hex.t
; content : string
}
let equal
t
({ path
; whole_loc
; content_start_offset
;
;
; digest_of_condensed_content
; content
} as t2)
=
phys_equal t t2
|| (Vcs.Path_in_repo.equal t.path path
&& Digest_hex.equal t.digest_of_condensed_content digest_of_condensed_content
&& Loc.equal t.whole_loc whole_loc
&& Int.equal t.content_start_offset content_start_offset
&& Result.equal ~ok:Header.equal ~error:Dyn.equal t.header header
&& String.equal t.comment_prefix comment_prefix
&& String.equal t.content content)
;;
let to_dyn
{ path : Vcs.Path_in_repo.t
; whole_loc : Loc.t
; content_start_offset : int
; : (Header.t, Dyn.t) Result.t
; : string
; digest_of_condensed_content : Digest_hex.t
; content : string
}
=
Dyn.record
[ "path", path |> Dyn.stringable (module Vcs.Path_in_repo)
; "whole_loc", whole_loc |> Loc.to_dyn
; "content_start_offset", content_start_offset |> Dyn.int
; "header", header |> or_dyn Header.to_dyn
; "comment_prefix", comment_prefix |> Dyn.string
; "digest_of_condensed_content", digest_of_condensed_content |> Digest_hex.to_dyn
; "content", content |> Dyn.string
]
;;
end
include T
let path t = t.path
let content t = t.content
let content_start_offset t = t.content_start_offset
let t = t.comment_prefix
let whole_loc t = t.whole_loc
let t = t.header
let create
~path
~whole_loc
~content_start_offset
~
~
~digest_of_condensed_content
~content
=
{ path
; whole_loc
; content_start_offset
; header
; comment_prefix
; digest_of_condensed_content
; content
}
;;
let digest_ignoring_minor_text_changes t = t.digest_of_condensed_content
module For_sorted_output : sig
type nonrec t = t
val compare : t -> t -> int
end = struct
type nonrec t = t
let compare t1 t2 =
let c = Vcs.Path_in_repo.compare t1.path t2.path in
if c <> 0
then c
else (
let c = Int.compare (Loc.start_line t1.whole_loc) (Loc.start_line t2.whole_loc) in
if c <> 0
then c
else Int.compare (Loc.start_offset t1.whole_loc) (Loc.start_offset t2.whole_loc))
;;
end
let reindented_content ?(new_line_prefix = "") t =
let rstrip_prefix = String.rstrip new_line_prefix in
let =
match t.comment_prefix with
| "--" | ";" | ";;" | "//" | "#" | "##" -> true
| _ -> false
in
let indent =
let len =
if expect_comment_prefixed_lines
then (
let start = Loc.start t.whole_loc in
start.pos_cnum - start.pos_bol)
else (
match t.header with
| Error _ ->
let start = Loc.start t.whole_loc in
start.pos_cnum - start.pos_bol + String.length t.comment_prefix + 1
| Ok h ->
let start = Loc.start h.status.loc in
start.pos_cnum - start.pos_bol)
in
String.make len ' '
in
let str = t.content in
let lines = String.split str ~on:'\n' in
let lines =
List.map lines ~f:(fun line ->
let line = String.rstrip line in
if String.is_empty line
then rstrip_prefix
else (
let line =
match String.chop_prefix line ~prefix:indent with
| Some s -> s
| None ->
String.lstrip line
in
let line =
match expect_comment_prefixed_lines with
| false -> line
| true ->
(match String.chop_prefix line ~prefix:(t.comment_prefix ^ " ") with
| Some s -> s
| None ->
(match String.chop_prefix line ~prefix:t.comment_prefix with
| Some s -> s
| None -> line))
in
new_line_prefix ^ line))
in
String.concat lines ~sep:"\n"
;;
let sort ts = List.sort ts ~compare:For_sorted_output.compare
let status t : Status.t =
match t.header with
| Error _ -> CR
| Ok p -> Header.status p
;;
let priority t : Priority.t =
match t.header with
| Error _ -> Now
| Ok p -> Header.priority p
;;
let to_string t =
String.concat
~sep:"\n"
[ Loc.to_string t.whole_loc; reindented_content t ~new_line_prefix:" "; "" ]
;;
let output_one ~include_delim cr ~oc =
let str = to_string cr in
let nl = if include_delim then "\n" else "" in
Out_channel.output_string oc (Printf.sprintf "%s%s" nl str)
;;
let output_list crs ~oc =
let crs = sort crs in
let include_delim = ref false in
List.iter crs ~f:(fun cr ->
output_one ~include_delim:!include_delim cr ~oc;
include_delim := true)
;;
let print_list crs = output_list crs ~oc:Out_channel.stdout
module Private = struct
let create = create
end
let work_on = priority
let kind = status