Source file cst.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
open! Base
open! Import
let for_all_string s ~f =
let b = ref true in
for i = 0 to String.length s - 1 do
b := !b && f s.[i]
done;
!b
;;
let is_blank = function
| ' ' | '\t' -> true
| _ -> false
;;
let is_space = function
| ' ' | '\t' | '\n' -> true
| _ -> false
;;
let is_blanks s = for_all_string s ~f:is_blank
let is_conflict_marker s =
String.equal s "======="
|| List.exists [ "<<<<<<< "; "||||||| "; ">>>>>>> " ] ~f:(fun prefix ->
String.is_prefix s ~prefix)
;;
let is_spaces s = for_all_string s ~f:is_space
let no_nl s = for_all_string s ~f:(fun c -> Char.( <> ) c '\n')
let has_nl s = not (no_nl s)
module Line = struct
type 'a not_blank =
{ trailing_blanks : string
; orig : string
; data : 'a
}
[@@deriving_inline sexp_of, compare, equal]
let _ = fun (_ : 'a not_blank) -> ()
let sexp_of_not_blank : 'a. ('a -> Sexplib0.Sexp.t) -> 'a not_blank -> Sexplib0.Sexp.t =
fun _of_a__001_
{ trailing_blanks = trailing_blanks__003_; orig = orig__005_; data = data__007_ } ->
let bnds__002_ = ([] : _ Stdlib.List.t) in
let bnds__002_ =
let arg__008_ = _of_a__001_ data__007_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "data"; arg__008_ ] :: bnds__002_
: _ Stdlib.List.t)
in
let bnds__002_ =
let arg__006_ = sexp_of_string orig__005_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "orig"; arg__006_ ] :: bnds__002_
: _ Stdlib.List.t)
in
let bnds__002_ =
let arg__004_ = sexp_of_string trailing_blanks__003_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "trailing_blanks"; arg__004_ ]
:: bnds__002_
: _ Stdlib.List.t)
in
Sexplib0.Sexp.List bnds__002_
;;
let _ = sexp_of_not_blank
let compare_not_blank : 'a. ('a -> 'a -> int) -> 'a not_blank -> 'a not_blank -> int =
fun _cmp__a a__009_ b__010_ ->
if Stdlib.( == ) a__009_ b__010_
then 0
else (
match compare_string a__009_.trailing_blanks b__010_.trailing_blanks with
| 0 ->
(match compare_string a__009_.orig b__010_.orig with
| 0 -> _cmp__a a__009_.data b__010_.data
| n -> n)
| n -> n)
;;
let _ = compare_not_blank
let equal_not_blank : 'a. ('a -> 'a -> bool) -> 'a not_blank -> 'a not_blank -> bool =
fun _cmp__a a__011_ b__012_ ->
if Stdlib.( == ) a__011_ b__012_
then true
else
Stdlib.( && )
(equal_string a__011_.trailing_blanks b__012_.trailing_blanks)
(Stdlib.( && )
(equal_string a__011_.orig b__012_.orig)
(_cmp__a a__011_.data b__012_.data))
;;
let _ = equal_not_blank
[@@@end]
type 'a t =
| Blank of string
| Conflict_marker of string
| Not_blank of 'a not_blank
[@@deriving_inline sexp_of, compare, equal]
let _ = fun (_ : 'a t) -> ()
let sexp_of_t : 'a. ('a -> Sexplib0.Sexp.t) -> 'a t -> Sexplib0.Sexp.t =
fun (type a__020_) : ((a__020_ -> Sexplib0.Sexp.t) -> a__020_ t -> Sexplib0.Sexp.t) ->
fun _of_a__013_ -> function
| Blank arg0__014_ ->
let res0__015_ = sexp_of_string arg0__014_ in
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Blank"; res0__015_ ]
| Conflict_marker arg0__016_ ->
let res0__017_ = sexp_of_string arg0__016_ in
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Conflict_marker"; res0__017_ ]
| Not_blank arg0__018_ ->
let res0__019_ = sexp_of_not_blank _of_a__013_ arg0__018_ in
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Not_blank"; res0__019_ ]
;;
let _ = sexp_of_t
let compare : 'a. ('a -> 'a -> int) -> 'a t -> 'a t -> int =
fun _cmp__a a__021_ b__022_ ->
if Stdlib.( == ) a__021_ b__022_
then 0
else (
match a__021_, b__022_ with
| Blank _a__023_, Blank _b__024_ -> compare_string _a__023_ _b__024_
| Blank _, _ -> -1
| _, Blank _ -> 1
| Conflict_marker _a__025_, Conflict_marker _b__026_ ->
compare_string _a__025_ _b__026_
| Conflict_marker _, _ -> -1
| _, Conflict_marker _ -> 1
| Not_blank _a__027_, Not_blank _b__028_ ->
compare_not_blank _cmp__a _a__027_ _b__028_)
;;
let _ = compare
let equal : 'a. ('a -> 'a -> bool) -> 'a t -> 'a t -> bool =
fun _cmp__a a__031_ b__032_ ->
if Stdlib.( == ) a__031_ b__032_
then true
else (
match a__031_, b__032_ with
| Blank _a__033_, Blank _b__034_ -> equal_string _a__033_ _b__034_
| Blank _, _ -> false
| _, Blank _ -> false
| Conflict_marker _a__035_, Conflict_marker _b__036_ ->
equal_string _a__035_ _b__036_
| Conflict_marker _, _ -> false
| _, Conflict_marker _ -> false
| Not_blank _a__037_, Not_blank _b__038_ ->
equal_not_blank _cmp__a _a__037_ _b__038_)
;;
let _ = equal
[@@@end]
let map t ~f =
match t with
| Blank b -> Blank b
| Conflict_marker c -> Conflict_marker c
| Not_blank n -> Not_blank { n with data = f n.orig n.data }
;;
let strip = function
| Blank _ -> Blank ""
| Conflict_marker c -> Conflict_marker (String.rstrip c)
| Not_blank n -> Not_blank { n with trailing_blanks = "" }
;;
let invariant inv = function
| Blank s -> assert (is_blanks s)
| Conflict_marker c -> assert (is_conflict_marker c)
| Not_blank n ->
assert (is_blanks n.trailing_blanks);
inv n.data;
assert (no_nl n.orig);
let len = String.length n.orig in
assert (len > 0 && not (is_blank n.orig.[len - 1]))
;;
let data t ~blank ~conflict_marker =
match t with
| Blank _ -> blank
| Conflict_marker marker -> conflict_marker marker
| Not_blank n -> n.data
;;
let orig = function
| Blank _ -> ""
| Conflict_marker c -> c
| Not_blank n -> n.orig
;;
end
type 'a single_line =
{ leading_blanks : string
; trailing_spaces : string
; orig : string
; data : 'a
}
[@@deriving_inline sexp_of, compare, equal]
let _ = fun (_ : 'a single_line) -> ()
let sexp_of_single_line : 'a. ('a -> Sexplib0.Sexp.t) -> 'a single_line -> Sexplib0.Sexp.t
=
fun _of_a__041_
{ leading_blanks = leading_blanks__043_
; trailing_spaces = trailing_spaces__045_
; orig = orig__047_
; data = data__049_
} ->
let bnds__042_ = ([] : _ Stdlib.List.t) in
let bnds__042_ =
let arg__050_ = _of_a__041_ data__049_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "data"; arg__050_ ] :: bnds__042_
: _ Stdlib.List.t)
in
let bnds__042_ =
let arg__048_ = sexp_of_string orig__047_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "orig"; arg__048_ ] :: bnds__042_
: _ Stdlib.List.t)
in
let bnds__042_ =
let arg__046_ = sexp_of_string trailing_spaces__045_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "trailing_spaces"; arg__046_ ] :: bnds__042_
: _ Stdlib.List.t)
in
let bnds__042_ =
let arg__044_ = sexp_of_string leading_blanks__043_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "leading_blanks"; arg__044_ ] :: bnds__042_
: _ Stdlib.List.t)
in
Sexplib0.Sexp.List bnds__042_
;;
let _ = sexp_of_single_line
let compare_single_line : 'a. ('a -> 'a -> int) -> 'a single_line -> 'a single_line -> int
=
fun _cmp__a a__051_ b__052_ ->
if Stdlib.( == ) a__051_ b__052_
then 0
else (
match compare_string a__051_.leading_blanks b__052_.leading_blanks with
| 0 ->
(match compare_string a__051_.trailing_spaces b__052_.trailing_spaces with
| 0 ->
(match compare_string a__051_.orig b__052_.orig with
| 0 -> _cmp__a a__051_.data b__052_.data
| n -> n)
| n -> n)
| n -> n)
;;
let _ = compare_single_line
let equal_single_line : 'a. ('a -> 'a -> bool) -> 'a single_line -> 'a single_line -> bool
=
fun _cmp__a a__053_ b__054_ ->
if Stdlib.( == ) a__053_ b__054_
then true
else
Stdlib.( && )
(equal_string a__053_.leading_blanks b__054_.leading_blanks)
(Stdlib.( && )
(equal_string a__053_.trailing_spaces b__054_.trailing_spaces)
(Stdlib.( && )
(equal_string a__053_.orig b__054_.orig)
(_cmp__a a__053_.data b__054_.data)))
;;
let _ = equal_single_line
[@@@end]
type 'a multi_lines =
{ leading_spaces : string
; trailing_spaces : string
; indentation : string
; lines : 'a Line.t list
}
[@@deriving_inline sexp_of, compare, equal]
let _ = fun (_ : 'a multi_lines) -> ()
let sexp_of_multi_lines : 'a. ('a -> Sexplib0.Sexp.t) -> 'a multi_lines -> Sexplib0.Sexp.t
=
fun _of_a__055_
{ leading_spaces = leading_spaces__057_
; trailing_spaces = trailing_spaces__059_
; indentation = indentation__061_
; lines = lines__063_
} ->
let bnds__056_ = ([] : _ Stdlib.List.t) in
let bnds__056_ =
let arg__064_ = sexp_of_list (Line.sexp_of_t _of_a__055_) lines__063_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "lines"; arg__064_ ] :: bnds__056_
: _ Stdlib.List.t)
in
let bnds__056_ =
let arg__062_ = sexp_of_string indentation__061_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "indentation"; arg__062_ ] :: bnds__056_
: _ Stdlib.List.t)
in
let bnds__056_ =
let arg__060_ = sexp_of_string trailing_spaces__059_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "trailing_spaces"; arg__060_ ] :: bnds__056_
: _ Stdlib.List.t)
in
let bnds__056_ =
let arg__058_ = sexp_of_string leading_spaces__057_ in
(Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "leading_spaces"; arg__058_ ] :: bnds__056_
: _ Stdlib.List.t)
in
Sexplib0.Sexp.List bnds__056_
;;
let _ = sexp_of_multi_lines
let compare_multi_lines : 'a. ('a -> 'a -> int) -> 'a multi_lines -> 'a multi_lines -> int
=
fun _cmp__a a__065_ b__066_ ->
if Stdlib.( == ) a__065_ b__066_
then 0
else (
match compare_string a__065_.leading_spaces b__066_.leading_spaces with
| 0 ->
(match compare_string a__065_.trailing_spaces b__066_.trailing_spaces with
| 0 ->
(match compare_string a__065_.indentation b__066_.indentation with
| 0 ->
compare_list
(fun a__067_ b__068_ -> Line.compare _cmp__a a__067_ b__068_)
a__065_.lines
b__066_.lines
| n -> n)
| n -> n)
| n -> n)
;;
let _ = compare_multi_lines
let equal_multi_lines : 'a. ('a -> 'a -> bool) -> 'a multi_lines -> 'a multi_lines -> bool
=
fun _cmp__a a__071_ b__072_ ->
if Stdlib.( == ) a__071_ b__072_
then true
else
Stdlib.( && )
(equal_string a__071_.leading_spaces b__072_.leading_spaces)
(Stdlib.( && )
(equal_string a__071_.trailing_spaces b__072_.trailing_spaces)
(Stdlib.( && )
(equal_string a__071_.indentation b__072_.indentation)
(equal_list
(fun a__073_ b__074_ -> Line.equal _cmp__a a__073_ b__074_)
a__071_.lines
b__072_.lines)))
;;
let _ = equal_multi_lines
[@@@end]
type 'a t =
| Empty of string
| Single_line of 'a single_line
| Multi_lines of 'a multi_lines
[@@deriving_inline sexp_of, compare, equal]
let _ = fun (_ : 'a t) -> ()
let sexp_of_t : 'a. ('a -> Sexplib0.Sexp.t) -> 'a t -> Sexplib0.Sexp.t =
fun (type a__084_) : ((a__084_ -> Sexplib0.Sexp.t) -> a__084_ t -> Sexplib0.Sexp.t) ->
fun _of_a__077_ -> function
| Empty arg0__078_ ->
let res0__079_ = sexp_of_string arg0__078_ in
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Empty"; res0__079_ ]
| Single_line arg0__080_ ->
let res0__081_ = sexp_of_single_line _of_a__077_ arg0__080_ in
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Single_line"; res0__081_ ]
| Multi_lines arg0__082_ ->
let res0__083_ = sexp_of_multi_lines _of_a__077_ arg0__082_ in
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Multi_lines"; res0__083_ ]
;;
let _ = sexp_of_t
let compare : 'a. ('a -> 'a -> int) -> 'a t -> 'a t -> int =
fun _cmp__a a__085_ b__086_ ->
if Stdlib.( == ) a__085_ b__086_
then 0
else (
match a__085_, b__086_ with
| Empty _a__087_, Empty _b__088_ -> compare_string _a__087_ _b__088_
| Empty _, _ -> -1
| _, Empty _ -> 1
| Single_line _a__089_, Single_line _b__090_ ->
compare_single_line _cmp__a _a__089_ _b__090_
| Single_line _, _ -> -1
| _, Single_line _ -> 1
| Multi_lines _a__093_, Multi_lines _b__094_ ->
compare_multi_lines _cmp__a _a__093_ _b__094_)
;;
let _ = compare
let equal : 'a. ('a -> 'a -> bool) -> 'a t -> 'a t -> bool =
fun _cmp__a a__097_ b__098_ ->
if Stdlib.( == ) a__097_ b__098_
then true
else (
match a__097_, b__098_ with
| Empty _a__099_, Empty _b__100_ -> equal_string _a__099_ _b__100_
| Empty _, _ -> false
| _, Empty _ -> false
| Single_line _a__101_, Single_line _b__102_ ->
equal_single_line _cmp__a _a__101_ _b__102_
| Single_line _, _ -> false
| _, Single_line _ -> false
| Multi_lines _a__105_, Multi_lines _b__106_ ->
equal_multi_lines _cmp__a _a__105_ _b__106_)
;;
let _ = equal
[@@@end]
let invariant inv t =
match t with
| Empty s -> assert (is_spaces s)
| Single_line s ->
assert (is_blanks s.leading_blanks);
assert (is_spaces s.trailing_spaces);
inv s.data;
assert (no_nl s.orig);
let len = String.length s.orig in
assert (len > 0 && (not (is_blank s.orig.[0])) && not (is_blank s.orig.[len - 1]))
| Multi_lines m ->
assert (is_spaces m.leading_spaces);
let ld_len = String.length m.leading_spaces in
assert (ld_len = 0 || Char.equal m.leading_spaces.[ld_len - 1] '\n');
let tr_has_nl = has_nl m.trailing_spaces in
assert (
is_spaces m.trailing_spaces
&& ((not tr_has_nl) || Char.equal m.trailing_spaces.[0] '\n'));
assert (is_blanks m.indentation);
List.iter m.lines ~f:(Line.invariant inv);
(match m.lines with
| [] -> assert false
| Blank _ :: _ -> assert false
| [ Not_blank n ] ->
assert (ld_len > 0 && (tr_has_nl || String.is_empty n.trailing_blanks))
| l ->
let rec check_last = function
| ([] : _ Line.t list) -> assert false
| [ Blank _ ] -> assert false
| [ Not_blank n ] -> assert (tr_has_nl || String.is_empty n.trailing_blanks)
| [ Conflict_marker m ] -> assert (not (String.is_empty m))
| _ :: (_ :: _ as l) -> check_last l
in
check_last l)
;;
let empty = Empty ""
let map t ~f =
match t with
| Empty e -> Empty e
| Single_line s -> Single_line { s with data = f s.orig s.data }
| Multi_lines m -> Multi_lines { m with lines = List.map m.lines ~f:(Line.map ~f) }
;;
let data t ~blank ~conflict_marker =
match t with
| Empty _ -> []
| Single_line s -> [ s.data ]
| Multi_lines m -> List.map m.lines ~f:(Line.data ~blank ~conflict_marker)
;;
let stripped_original_lines t =
match t with
| Empty _ -> []
| Single_line s -> [ s.orig ]
| Multi_lines m -> List.map m.lines ~f:Line.orig
;;
let line_of_single s : _ Line.t =
Not_blank { trailing_blanks = ""; orig = s.orig; data = s.data }
;;
let to_lines t =
match t with
| Empty _ -> []
| Single_line s -> [ line_of_single s ]
| Multi_lines m -> m.lines
;;
let strip t =
match t with
| Empty _ -> Empty ""
| Single_line s -> Single_line { s with leading_blanks = ""; trailing_spaces = "" }
| Multi_lines m ->
(match m.lines with
| [] -> Empty ""
| [ Blank _ ] -> assert false
| [ Not_blank n ] ->
Single_line
{ leading_blanks = ""; trailing_spaces = ""; orig = n.orig; data = n.data }
| lines ->
Multi_lines
{ leading_spaces = ""
; trailing_spaces = ""
; indentation = ""
; lines = List.map lines ~f:Line.strip
})
;;
let to_string t =
match t with
| Empty s -> s
| Single_line s -> s.leading_blanks ^ s.orig ^ s.trailing_spaces
| Multi_lines m ->
let indent (line : _ Line.t) =
match line with
| Blank b -> b
| Conflict_marker c -> c
| Not_blank n -> m.indentation ^ n.orig ^ n.trailing_blanks
in
let s = List.map m.lines ~f:indent |> String.concat ~sep:"\n" in
m.leading_spaces ^ s ^ m.trailing_spaces
;;
let trim_lines lines =
let rec loop0 : _ Line.t list -> _ = function
| Blank _ :: l -> loop0 l
| l -> loop1 l ~acc:[] ~acc_with_trailing_blanks:[]
and loop1 ~acc ~acc_with_trailing_blanks = function
| (Blank _ as x) :: l ->
loop1 l ~acc ~acc_with_trailing_blanks:(x :: acc_with_trailing_blanks)
| ((Conflict_marker _ | Not_blank _) as x) :: l ->
let acc = x :: acc_with_trailing_blanks in
loop1 l ~acc ~acc_with_trailing_blanks:acc
| [] -> List.rev acc
in
loop0 lines
;;
let not_blank_or_conflict_lines lines =
List.fold_left lines ~init:[] ~f:(fun acc (l : _ Line.t) ->
match l with
| Blank _ | Conflict_marker _ -> acc
| Not_blank n -> n.orig :: acc)
|> List.rev
;;
let longest_common_prefix a b =
let len_a = String.length a in
let len_b = String.length b in
let len = min len_a len_b in
let i = ref 0 in
while !i < len && Char.equal a.[!i] b.[!i] do
Int.incr i
done;
String.sub a ~pos:0 ~len:!i
;;
let indentation s =
let len = String.length s in
let i = ref 0 in
while !i < len && is_blank s.[!i] do
Int.incr i
done;
String.sub s ~pos:0 ~len:!i
;;
let lines =
match not_blank_or_conflict_lines lines with
| [] -> "", lines
| first :: rest ->
let indent = List.fold_left rest ~init:(indentation first) ~f:longest_common_prefix in
let indent_len = String.length indent in
let update_line : 'a Line.t -> 'a Line.t = function
| Blank b -> Blank b
| Conflict_marker c -> Conflict_marker c
| Not_blank n ->
let orig =
String.sub n.orig ~pos:indent_len ~len:(String.length n.orig - indent_len)
in
Not_blank { n with orig }
in
indent, List.map lines ~f:update_line
;;
let break s at = String.prefix s at, String.drop_prefix s at
let reconcile (type a) t ~lines ~default_indentation ~pad_single_line =
let module M = struct
type t =
| Empty
| Single_line of a Line.not_blank
| Multi_lines of a Line.t list
end
in
let lines =
match trim_lines lines |> extract_indentation |> snd with
| [] -> M.Empty
| [ Blank _ ] -> assert false
| [ Not_blank n ] -> M.Single_line n
| lines -> M.Multi_lines lines
in
let padding = if pad_single_line then " " else "" in
let res =
match t, lines with
| Empty _, Empty -> t
| Single_line s, Single_line n -> Single_line { s with orig = n.orig; data = n.data }
| Multi_lines m, Multi_lines l -> Multi_lines { m with lines = l }
| Empty e, Multi_lines l ->
let ld, tr =
if has_nl e
then (
let ld, tr = break e (String.index_exn e '\n') in
ld ^ "\n", tr)
else "\n", padding
in
Multi_lines
{ leading_spaces = ld
; trailing_spaces = tr
; indentation = String.make (default_indentation + 2) ' '
; lines = l
}
| Single_line m, Multi_lines l ->
Multi_lines
{ leading_spaces = "\n"
; trailing_spaces = m.trailing_spaces
; indentation = String.make (default_indentation + 2) ' '
; lines = l
}
| Single_line _, Empty | Multi_lines _, Empty -> Empty padding
| Empty _, Single_line n ->
Single_line
{ orig = n.orig
; data = n.data
; leading_blanks = padding
; trailing_spaces = padding
}
| Multi_lines m, Single_line n -> Multi_lines { m with lines = [ Not_blank n ] }
in
invariant ignore res;
res
;;