Debian Codegraph @

lib/pp_diff.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* DEBUG/UNIT TEST *)
12 let cfprintf oc = Printf.(kfprintf (fun oc -> fprintf oc "") oc)
13 let log_out_ch = ref stdout
14 let cprintf s = cfprintf !log_out_ch s
15
16
17 module StringDiff = Diff2.Make(struct
18 type elem = String.t
19 type t = elem array
20 let get t i = Array.get t i
21 let length t = Array.length t
22 end)
23
24 type diff_type =
25 [ `Removed
26 | `Added
27 | `Common
28 ]
29
30 type diff_list = StringDiff.elem Diff2.edit list
31
32 (* debug print diff data structure *)
33 let db_print_diffs fmt diffs =
34 let open Format in
35 let print_diff = function
36 | `Common (opos, npos, s) ->
37 fprintf fmt "Common '%s' opos = %d npos = %d\n" s opos npos;
38 | `Removed (pos, s) ->
39 fprintf fmt "Removed '%s' opos = %d\n" s pos;
40 | `Added (pos, s) ->
41 fprintf fmt "Added '%s' npos = %d\n" s pos;
42 in
43 pp_open_vbox fmt 0;
44 List.iter print_diff diffs;
45 pp_close_box fmt ();
46 pp_print_flush fmt ()
47
48 let string_of_diffs diffs =
49 Format.asprintf "%a" db_print_diffs diffs
50
51 (* Adjust the diffs returned by the Myers algorithm to reduce the span of the
52 changes. This gives more natural-looking diffs.
53
54 While the Myers algorithm minimizes the number of changes between two
55 sequences, it doesn't minimize the span of the changes. For example,
56 representing elements in common in lower case and inserted elements in upper
57 case (but ignoring case in the algorithm), ABabC and abABC both have 3 changes
58 (A, B and C). However the span of the first sequence is 5 elements (ABabC)
59 while the span of the second is 3 elements (ABC).
60
61 The algorithm modifies the changes iteratively, for example ABabC -> aBAbC -> abABC
62
63 dtype: identifies which of Added OR Removed to use; the other one is ignored.
64 diff_list: output from the Myers algorithm
65 *)
66 let shorten_diff_span dtype diff_list =
67 let changed = ref false in
68 let diffs = Array.of_list diff_list in
69 let len = Array.length diffs in
70 let vinfo index =
71 match diffs.(index) with
72 | `Common (opos, npos, s) -> (`Common, opos, npos, s)
73 | `Removed (pos, s) -> (`Removed, pos, 0, s)
74 | `Added (pos, s) -> (`Added, 0, pos, s) in
75 let get_variant index =
76 let (v, _, _, _) = vinfo index in
77 v in
78 let get_str index =
79 let (_, _, _, s) = vinfo index in
80 s in
81
82 let iter start len lt incr = begin
83 let src = ref start in
84 let dst = ref start in
85 while (lt !src len) do
86 if (get_variant !src) = dtype then begin
87 if (lt !dst !src) then
88 dst := !src;
89 while (lt !dst len) && (get_variant !dst) = dtype do
90 dst := !dst + incr;
91 done;
92 if (lt !dst len) && (get_str !src) = (get_str !dst) then begin
93 (* swap diff *)
94 let (_, c_opos, c_npos, str) = vinfo !dst
95 and (_, v_opos, v_npos, _) = vinfo !src in
96 changed := true;
97 if dtype = `Added then begin
98 diffs.(!src) <- `Common (c_opos, v_npos, str);
99 diffs.(!dst) <- `Added (c_npos, str);
100 end else begin
101 diffs.(!src) <- `Common (v_opos, c_npos, str);
102 diffs.(!dst) <- `Removed (c_opos, str)
103 end
104 end
105 end;
106 src := !src + incr
107 done
108 end in
109
110 iter 0 len (<) 1; (* left to right *)
111 iter (len-1) (-1) (>) (-1); (* right to left *)
112 if !changed then Array.to_list diffs else diff_list
113
114 let has_changes diffs =
115 let rec has_changes_r diffs added removed =
116 match diffs with
117 | `Added _ :: t -> has_changes_r t true removed
118 | `Removed _ :: t -> has_changes_r t added true
119 | h :: t -> has_changes_r t added removed
120 | [] -> (added, removed) in
121 has_changes_r diffs false false
122
123 (* get the Myers diff of 2 lists of strings *)
124 let diff_strs old_strs new_strs =
125 let diffs = List.rev (StringDiff.diff old_strs new_strs) in
126 shorten_diff_span `Removed (shorten_diff_span `Added diffs)
127
128 (* Default string tokenizer. Makes each character a separate string.
129 Whitespace is not ignored. Doesn't handle UTF-8 differences well. *)
130 let def_tokenize_string s =
131 let limit = (String.length s) - 1 in
132 let strs : string list ref = ref [] in
133 for i = 0 to limit do
134 strs := (String.make 1 s.[i]) :: !strs
135 done;
136 List.rev !strs
137
138 (* get the Myers diff of 2 strings *)
139 let diff_str ?(tokenize_string=def_tokenize_string) old_str new_str =
140 let old_toks = Array.of_list (tokenize_string old_str)
141 and new_toks = Array.of_list (tokenize_string new_str) in
142 diff_strs old_toks new_toks
143
144 let get_dinfo = function
145 | `Common (_, _, s) -> (`Common, s)
146 | `Removed (_, s) -> (`Removed, s)
147 | `Added (_, s) -> (`Added, s)
148
149 [@@@ocaml.warning "-32"]
150 let string_of_diff_type = function
151 | `Common -> "Common"
152 | `Removed -> "Removed"
153 | `Added -> "Added"
154 [@@@ocaml.warning "+32"]
155
156 let wrap_in_bg diff_tag pp =
157 let open Pp in
158 (tag (Pp.start_pfx ^ diff_tag ^ ".bg") (str "")) ++ pp ++
159 (tag (Pp.end_pfx ^ diff_tag ^ ".bg") (str ""))
160
161 exception Diff_Failure of string
162
163 let add_diff_tags which pp diffs =
164 let open Pp in
165 let diff_tag = if which = `Added then "diff.added" else "diff.removed" in
166 let diffs : diff_list ref = ref diffs in
167 let in_diff = ref false in (* true = buf chars need a tag *)
168 let in_span = ref false in (* true = last pp had a start tag *)
169 let trans = ref false in (* true = this diff starts/ends highlight *)
170 let buf = Buffer.create 16 in
171 let acc_pp = ref [] in
172 let diff_str, diff_ind, diff_len = ref "", ref 0, ref 0 in
173 let prev_dtype, dtype, next_dtype = ref `Common, ref `Common, ref `Common in
174 let is_white c = List.mem c [' '; '\t'; '\n'; '\r'] in
175
176 let skip () =
177 while !diffs <> [] &&
178 (let (t, _) = get_dinfo (List.hd !diffs) in
179 t <> `Common && t <> which)
180 do
181 diffs := List.tl !diffs
182 done
183 in
184
185 let put_tagged case =
186 if Buffer.length buf > 0 then begin
187 let pp = str (Buffer.contents buf) in
188 Buffer.clear buf;
189 let tagged = match case with
190 | "" -> pp
191 | "tag" -> tag diff_tag pp
192 | "start" -> in_span := true; tag (start_pfx ^ diff_tag) pp
193 | "end" -> in_span := false; tag (end_pfx ^ diff_tag) pp
194 | _ -> raise (Diff_Failure "invalid tag id in put_tagged, should be impossible") in
195 acc_pp := tagged :: !acc_pp
196 end
197 in
198
199 let output_pps () =
200 let next_diff_char_hl = if !diff_ind < !diff_len then !dtype = which else !next_dtype = which in
201 let tag = if not !in_diff then ""
202 else if !in_span then
203 if next_diff_char_hl then "" else "end"
204 else
205 if next_diff_char_hl then "start" else "tag" in
206 put_tagged tag; (* flush any remainder *)
207 let l = !acc_pp in
208 acc_pp := [];
209 match List.length l with
210 | 0 -> str ""
211 | 1 -> List.hd l
212 | _ -> seq (List.rev l)
213 in
214
215 let maybe_next_diff () =
216 if !diff_ind = !diff_len && (skip(); !diffs <> []) then begin
217 let (t, s) = get_dinfo (List.hd !diffs) in
218 diff_str := s; diff_ind := 0; diff_len := String.length !diff_str;
219 diffs := List.tl !diffs; skip();
220 prev_dtype := !dtype;
221 dtype := t;
222 next_dtype := (match !diffs with
223 | diff2 :: _ -> let (nt, _) = get_dinfo diff2 in nt
224 | [] -> `Common);
225 trans := !dtype <> !prev_dtype
226 end;
227 in
228
229 let s_char c =
230 maybe_next_diff ();
231 (* matching first should handle tokens with spaces, e.g. in comments/strings *)
232 if !diff_ind < !diff_len && c = !diff_str.[!diff_ind] then begin
233 if !dtype = which && !trans && !diff_ind = 0 then begin
234 put_tagged "";
235 in_diff := true
236 end;
237 Buffer.add_char buf c;
238 diff_ind := !diff_ind + 1;
239 if !dtype = which && !dtype <> !next_dtype && !diff_ind = !diff_len then begin
240 put_tagged (if !in_span then "end" else "tag");
241 in_diff := false
242 end
243 end else if is_white c then
244 Buffer.add_char buf c
245 else begin
246 cprintf "mismatch: expected '%c' but got '%c'\n" !diff_str.[!diff_ind] c;
247 raise (Diff_Failure "string mismatch, should be impossible")
248 end
249 in
250
251 (* rearrange so existing tags are inside diff tags, provided that those tags
252 only contain Ppcmd_string's. Other cases (e.g. tag of a box) are not supported. *)
253 (* todo: Is there a better way to do this in OCaml without multiple 'repr's? *)
254 let reorder_tags child pp_tag pp =
255 match repr child with
256 | Ppcmd_tag (t1, pp) -> tag t1 (tag pp_tag pp)
257 | Ppcmd_glue l ->
258 if List.exists (fun x ->
259 match repr x with
260 | Ppcmd_tag (_, _) -> true
261 | _ -> false) l
262 then seq (List.map (fun x ->
263 match repr x with
264 | Ppcmd_tag (t2, pp2) -> tag t2 (tag pp_tag pp2)
265 | pp2 -> tag pp_tag (unrepr pp2)) l)
266 else child
267 | _ -> tag pp_tag child
268 in
269
270 let rec add_tags_r pp =
271 let r_pp = repr pp in
272 match r_pp with
273 | Ppcmd_string s -> String.iter s_char s; output_pps ()
274 | Ppcmd_glue l -> seq (List.map add_tags_r l)
275 | Ppcmd_box (block_type, pp) -> unrepr (Ppcmd_box (block_type, add_tags_r pp))
276 | Ppcmd_tag (pp_tag, pp) -> reorder_tags (add_tags_r pp) pp_tag pp
277 | _ -> pp
278 in
279 let (has_added, has_removed) = has_changes !diffs in
280 let rv = add_tags_r pp in
281 skip ();
282 if !diffs <> [] then
283 raise (Diff_Failure "left-over diff info at end of Pp.t, should be impossible");
284 if has_added || has_removed then wrap_in_bg diff_tag rv else rv
285
286 let diff_pp ?(tokenize_string=def_tokenize_string) o_pp n_pp =
287 let open Pp in
288 let o_str = string_of_ppcmds o_pp in
289 let n_str = string_of_ppcmds n_pp in
290 let diffs = diff_str ~tokenize_string o_str n_str in
291 (add_diff_tags `Removed o_pp diffs, add_diff_tags `Added n_pp diffs)
292
293 let diff_pp_combined ?(tokenize_string=def_tokenize_string) ?(show_removed=false) o_pp n_pp =
294 let open Pp in
295 let o_str = string_of_ppcmds o_pp in
296 let n_str = string_of_ppcmds n_pp in
297 let diffs = diff_str ~tokenize_string o_str n_str in
298 let (_, has_removed) = has_changes diffs in
299 let added = add_diff_tags `Added n_pp diffs in
300 if show_removed && has_removed then
301 let removed = add_diff_tags `Removed o_pp diffs in
302 (v 0 (removed ++ cut() ++ added))
303 else added