@@ -20,10 +20,12 @@ module Part = struct
2020
2121 type t =
2222 { name : string ;
23+ sep_indent : string ; (* * Whitespaces before the [@@@part] separator *)
2324 body : string ; }
2425
25- let v ~name ~body = { name; body }
26+ let v ~name ~sep_indent ~ body = { name; sep_indent ; body }
2627 let name {name;_} = name
28+ let sep_indent {sep_indent;_} = sep_indent
2729 let body {body;_} = body
2830
2931end
@@ -40,29 +42,33 @@ struct
4042 let open Re in
4143 let ws = rep space in
4244 compile @@ whole_string @@ seq [
43- ws; str " [@@@" ; ws; str " part" ; ws;
45+ group ws; str " [@@@" ; ws; str " part" ; ws;
4446 str " \" " ; group (rep1 any); str " \" " ;
4547 ws; str " ]" ; ws; opt (str " ;;" ); ws;
4648 ]
4749
48- let make_part ~name ~lines =
50+ let next_part ~name ~sep_indent = fun lines ->
4951 let body = String. concat " \n " (List. rev (remove_empty_heads lines)) in
50- Part. v ~name ~body
52+ Part. v ~name ~sep_indent ~ body
5153
52- let rec parse_parts input name lines =
54+ let next_part_of_groups groups =
55+ let sep_indent = Re.Group. get groups 1 in
56+ let name = Re.Group. get groups 2 in
57+ next_part ~name ~sep_indent
58+
59+ let rec parse_parts input make_part lines =
5360 match input_line input with
54- | exception End_of_file -> [make_part ~name ~ lines ]
61+ | exception End_of_file -> [make_part lines]
5562 | line ->
5663 match Re. exec_opt part_statement_re line with
57- | None -> parse_parts input name (line :: lines)
64+ | None -> parse_parts input make_part (line :: lines)
5865 | Some groups ->
59- let part = make_part ~name ~lines in
60- let new_name = Re.Group. get groups 1 in
61- part :: parse_parts input new_name []
66+ let next_part = next_part_of_groups groups in
67+ make_part lines :: parse_parts input next_part []
6268
6369 let of_file name =
6470 let input = open_in name in
65- parse_parts input " " []
71+ parse_parts input (next_part ~name: " " ~sep_indent: " " ) []
6672
6773end
6874
@@ -86,7 +92,7 @@ let rec replace_or_append part_name body = function
8692 | p :: tl ->
8793 p :: replace_or_append part_name body tl
8894 | [] ->
89- [{ name = part_name; body }]
95+ [{ name = part_name; sep_indent = " " ; body }]
9096
9197let replace file ~part ~lines =
9298 let part = match part with None -> " " | Some p -> p in
@@ -98,7 +104,9 @@ let contents file =
98104 let body = Part. body p in
99105 match Part. name p with
100106 | "" -> body :: acc
101- | n -> body :: (" \n [@@@part \" " ^ n ^ " \" ] ;;\n " ) :: acc
107+ | n ->
108+ let indent = Part. sep_indent p in
109+ body :: (" \n " ^ indent ^ " [@@@part \" " ^ n ^ " \" ] ;;\n " ) :: acc
102110 ) [] file
103111 in
104112 let lines = List. rev lines in
0 commit comments