1- open Types
2-
31type conflict2 = { orig : string list ; new_ : string list }
4- type t = conflict2 list String_map .t
5- type printer = { same : string Fmt .t ; diff : conflict2 Fmt .t }
2+ type hunk = Change of conflict2 | Same of string
3+ type t = hunk list String_map .t
4+ type printer = { same : string Fmt .t ; diff : hunk Fmt .t }
65
76let printer ~same ~diff = { same; diff }
87
8+ let git_diff_printer ppf c =
9+ match c with
10+ | Change { orig; new_ } ->
11+ List. iter (fun line -> Fmt. pf ppf " -%s\n " line) orig;
12+ List. iter (fun line -> Fmt. pf ppf " +%s\n " line) new_
13+ | Same common -> Fmt. pf ppf " %s\n " common
14+
915let git_printer =
10- {
11- same = (fun ppf -> Fmt. pf ppf " %s\n " );
12- diff =
13- (fun ppf { orig; new_ } ->
14- List. iter (fun line -> Fmt. pf ppf " -%s\n " line) orig;
15- List. iter (fun line -> Fmt. pf ppf " +%s\n " line) new_);
16- }
16+ { same = (fun ppf -> Fmt. pf ppf " %s\n " ); diff = git_diff_printer }
1717
1818let pp_ diff_printer =
1919 let pp_dh ppf dh = match dh with c -> diff_printer.diff ppf c in
@@ -26,23 +26,30 @@ let vd_to_lines name vd =
2626 Format. pp_print_flush formatter () ;
2727 CCString. lines (Buffer. contents buf)
2828
29- let td_to_lines name vd =
29+ let td_to_lines name td =
30+ let buf = Buffer. create 256 in
31+ let formatter = Format. formatter_of_buffer buf in
32+ Printtyp. type_declaration (Ident. create_local name) formatter td;
33+ Format. pp_print_flush formatter () ;
34+ CCString. lines (Buffer. contents buf)
35+
36+ let lbl_to_lines ld =
3037 let buf = Buffer. create 256 in
3138 let formatter = Format. formatter_of_buffer buf in
32- Printtyp. type_declaration ( Ident. create_local name) formatter vd ;
39+ Printtyp. label formatter ld ;
3340 Format. pp_print_flush formatter () ;
3441 CCString. lines (Buffer. contents buf)
3542
3643let md_to_lines name md =
3744 let buf = Buffer. create 256 in
3845 let formatter = Format. formatter_of_buffer buf in
39- Printtyp. modtype formatter md.md_type;
46+ Printtyp. modtype formatter Types. ( md.md_type) ;
4047 Format. pp_print_flush formatter () ;
4148 let module_str = " module " ^ name ^ " : " ^ Buffer. contents buf in
4249 CCString. lines module_str
4350
4451let mtd_to_lines name mtd =
45- match mtd.mtd_type with
52+ match Types. ( mtd.mtd_type) with
4653 | Some m ->
4754 let buf = Buffer. create 256 in
4855 let formatter = Format. formatter_of_buffer buf in
@@ -72,50 +79,96 @@ let ctd_to_lines name cd =
7279 let class_str = Buffer. contents buf in
7380 CCString. lines class_str
7481
75- let process_diff (diff : (_, _ Diff.atomic_modification) Diff.t ) name to_lines =
82+ let process_atomic_diff (diff : (_, _ Diff.atomic_modification) Diff.t ) name
83+ to_lines =
7684 match diff with
77- | Added item -> [ { orig = [] ; new_ = to_lines name item } ]
78- | Removed item -> [ { orig = to_lines name item; new_ = [] } ]
85+ | Added item -> [ Change { orig = [] ; new_ = to_lines name item } ]
86+ | Removed item -> [ Change { orig = to_lines name item; new_ = [] } ]
7987 | Modified { reference; current } ->
80- [ { orig = to_lines name reference; new_ = to_lines name current } ]
88+ [
89+ Change { orig = to_lines name reference; new_ = to_lines name current };
90+ ]
8191
8292let process_value_diff (val_diff : Diff.value ) =
83- process_diff val_diff.vdiff val_diff.vname vd_to_lines
93+ process_atomic_diff val_diff.vdiff val_diff.vname vd_to_lines
94+
95+ let process_lbl_diff
96+ (diff :
97+ ( Types.label_declaration,
98+ Types.label_declaration Diff.atomic_modification )
99+ Diff.t ) =
100+ match diff with
101+ | Added item -> [ Change { orig = [] ; new_ = lbl_to_lines item } ]
102+ | Removed item -> [ Change { orig = lbl_to_lines item; new_ = [] } ]
103+ | Modified { reference; current } ->
104+ [ Change { orig = lbl_to_lines reference; new_ = lbl_to_lines current } ]
105+
106+ let rec process_type_diff (type_diff : Diff.type_ ) =
107+ match type_diff.tdiff with
108+ | Diff. Modified (Compound change_lst ) ->
109+ process_modified_record_type_diff type_diff.tname change_lst
110+ | Diff. Modified (Atomic mods ) ->
111+ process_atomic_diff (Diff. Modified mods) type_diff.tname td_to_lines
112+ | Diff. Added td ->
113+ process_atomic_diff (Diff. Added td) type_diff.tname td_to_lines
114+ | Diff. Removed td ->
115+ process_atomic_diff (Diff. Removed td) type_diff.tname td_to_lines
116+
117+ and process_modified_record_type_diff name diff =
118+ let indent_lbl c =
119+ match c with
120+ | Same c -> Same (" " ^ c)
121+ | Change { orig; new_ } ->
122+ Change
123+ {
124+ orig = List. map (fun s -> " " ^ s) orig;
125+ new_ = List. map (fun s -> " " ^ s) new_;
126+ }
127+ in
128+ let changes = process_changed_labels diff in
129+ [ Same (" type " ^ name ^ " = {" ) ]
130+ @ [ indent_lbl (Same " ..." ) ]
131+ @ List. map (fun c -> indent_lbl c) changes
132+ @ [ Same " }" ]
84133
85- let process_type_diff (type_diff : Diff.type_ ) =
86- process_diff type_diff.tdiff type_diff.tname td_to_lines
134+ and process_changed_labels (lbls_diffs : Diff.record_field list ) =
135+ List. flatten
136+ (List. map
137+ (fun (lbl_diff : Diff.record_field ) -> process_lbl_diff lbl_diff.ldiff)
138+ lbls_diffs)
87139
88140let process_class_diff (class_diff : Diff.class_ ) =
89141 match class_diff.cdiff with
90- | Added cd -> [ { orig = [] ; new_ = cd_to_lines class_diff.cname cd } ]
91- | Removed cd -> [ { orig = cd_to_lines class_diff.cname cd; new_ = [] } ]
142+ | Added cd -> [ Change { orig = [] ; new_ = cd_to_lines class_diff.cname cd } ]
143+ | Removed cd ->
144+ [ Change { orig = cd_to_lines class_diff.cname cd; new_ = [] } ]
92145 | Modified _ -> []
93146
94147let process_class_type_diff (class_type_diff : Diff.cltype ) =
95148 match class_type_diff.ctdiff with
96149 | Added ctd ->
97- [ { orig = [] ; new_ = ctd_to_lines class_type_diff.ctname ctd } ]
150+ [ Change { orig = [] ; new_ = ctd_to_lines class_type_diff.ctname ctd } ]
98151 | Removed ctd ->
99- [ { orig = ctd_to_lines class_type_diff.ctname ctd; new_ = [] } ]
152+ [ Change { orig = ctd_to_lines class_type_diff.ctname ctd; new_ = [] } ]
100153 | Modified _ -> []
101154
102155let rec process_sig_diff :
103156 type a . _ -> (string -> a -> string list ) -> (a , _ ) Diff. t * _ -> _ -> _ =
104157 fun path to_lines ((diff : (a, _) Diff.t ), name ) acc ->
105158 match diff with
106159 | Added curr_mtd ->
107- let diff = [ { orig = [] ; new_ = to_lines name curr_mtd } ] in
160+ let diff = [ Change { orig = [] ; new_ = to_lines name curr_mtd } ] in
108161 String_map. update path
109162 (function None -> Some diff | Some existing -> Some (existing @ diff))
110163 acc
111164 | Removed ref_mtd ->
112- let diff = [ { orig = to_lines name ref_mtd; new_ = [] } ] in
165+ let diff = [ Change { orig = to_lines name ref_mtd; new_ = [] } ] in
113166 String_map. update path
114167 (function None -> Some diff | Some existing -> Some (existing @ diff))
115168 acc
116169 | Modified Diff. Unsupported ->
117170 String_map. add path
118- [ { orig = [] ; new_ = [ " <unsupported change>" ] } ]
171+ [ Change { orig = [] ; new_ = [ " <unsupported change>" ] } ]
119172 acc
120173 | Modified (Supported changes ) -> signature_changes path changes acc
121174
@@ -200,9 +253,12 @@ module With_colors = struct
200253 let pp_keep fmt line = Format. fprintf fmt " %s\n " line
201254
202255 let printer =
203- printer ~same: pp_keep ~diff: (fun fmt { orig; new_ } ->
204- List. iter (pp_remove fmt) orig;
205- List. iter (pp_add fmt) new_)
256+ printer ~same: pp_keep ~diff: (fun fmt c ->
257+ match c with
258+ | Change { orig; new_ } ->
259+ List. iter (pp_remove fmt) orig;
260+ List. iter (pp_add fmt) new_
261+ | Same common -> (pp_keep fmt) common)
206262
207263 let pp_diff fmt diff = pp_ printer fmt diff
208264 let pp fmt t = gen_pp pp_diff fmt t
0 commit comments