package patdiff
File Diff using the Patience Diff algorithm
Install
Dune Dependency
Authors
Maintainers
Sources
v0.17.0.tar.gz
sha256=f4f2b060ea39870e238f5be744e84d1d8030864a02f8fc2368866e4d3d7e1b72
doc/src/patdiff.kernel/patdiff_core.ml.html
Source file patdiff_core.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 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
open! Core open! Import include Patdiff_core_intf include struct open Configuration let default_context = default_context let default_line_big_enough = default_line_big_enough let default_word_big_enough = default_word_big_enough end (* Strip whitespace from a string by stripping and replacing with spaces *) let ws_rex = Re.compile Re.(rep1 space) let ws_rex_anchored = Re.compile Re.(seq [ bol; rep space; eol ]) let ws_sub = " " let remove_ws s = String.strip (Re.replace_string ws_rex s ~by:ws_sub) let is_ws = Re.execp ws_rex_anchored (* This regular expression describes the delimiters on which to split the string *) let words_rex = let open Re in let delim = set {|"{}[]#,.;()_|} in let punct = rep1 (set {|=`+-/!@$%^&*:|<>|}) in let space = rep1 space in (* We don't want to split up ANSI color sequences, so let's make sure they get through intact. *) let ansi_sgr_sequence = let esc = char '\027' in seq [ esc; char '['; rep (alt [ char ';'; digit ]); char 'm' ] in compile (alt [ delim; punct; space; ansi_sgr_sequence ]) ;; (* Split a string into a list of string options delimited by words_rex (delimiters included) *) let split s ~keep_ws = let s = if keep_ws then s else String.rstrip s in if String.is_empty s && keep_ws then [ "" ] else Re.split_full words_rex s |> List.filter_map ~f:(fun token -> let string = match token with | `Delim d -> Re.Group.get d 0 | `Text t -> t in if String.is_empty string then None else Some string) ;; (* This function ensures that the tokens passed to Patience diff do not include whitespace. Whitespace is appended to words, and then removed by [~transform] later on. The point is to make the semantic cleanup go well -- we don't want whitespace matches to "count" as part of the length of a match. *) let whitespace_ignorant_split s = if String.is_empty s then [] else ( let istext s = not (Re.execp ws_rex s) in split s ~keep_ws:false |> List.group ~break:(fun split_result1 _ -> istext split_result1) |> List.map ~f:String.concat) ;; include struct let%expect_test _ = print_s ([%sexp_of: string list] (split ~keep_ws:true "")); [%expect {| ("") |}] ;; end module Make (Output_impls : Output_impls) = struct module Output_ops = struct module Rule = struct let apply text ~rule ~output ~refined = let (module O) = Output_impls.implementation output in O.Rule.apply text ~rule ~refined ;; end module Rules = struct let to_string (rules : Format.Rules.t) output : string Patience_diff.Range.t -> string Patience_diff.Range.t = let apply text ~rule ~refined = Rule.apply text ~rule ~output ~refined in function | Same ar -> let formatted_ar = Array.map ar ~f:(fun (x, y) -> let app = apply ~rule:rules.line_same ~refined:false in app x, app y) in Same formatted_ar | Next (ar, move_kind) -> Next ( Array.map ar ~f: (apply ~refined:false ~rule: (match move_kind with | Some (Move _) -> rules.moved_to_next | Some (Within_move _) -> rules.added_in_move | None -> rules.line_next)) , move_kind ) | Prev (ar, move_kind) -> Prev ( Array.map ar ~f: (apply ~refined:false ~rule: (match move_kind with | Some (Move _) -> rules.moved_from_prev | Some (Within_move _) -> rules.removed_in_move | None -> rules.line_prev)) , move_kind ) | Unified (ar, move_id) -> Unified ( Array.map ar ~f: (apply ~refined:true ~rule: (match move_id with | None -> rules.line_unified | Some _ -> rules.line_unified_in_move)) , move_id ) | Replace (ar1, ar2, move_id) -> let prev_rule, next_rule = match move_id with | None -> rules.line_prev, rules.line_next | Some _ -> rules.removed_in_move, rules.added_in_move in let ar1 = Array.map ar1 ~f:(apply ~refined:true ~rule:prev_rule) in let ar2 = Array.map ar2 ~f:(apply ~refined:true ~rule:next_rule) in Replace (ar1, ar2, move_id) ;; let map_ranges (hunks : _ Patience_diff.Hunk.t list) ~f = List.map hunks ~f:(fun hunk -> { hunk with ranges = List.map hunk.ranges ~f }) ;; let apply hunks ~rules ~output = map_ranges hunks ~f:(to_string rules output) end let print ~print_global_header ~file_names ~rules ~output ~print ~location_style hunks = let formatted_hunks = Rules.apply ~rules ~output hunks in let (module O) = Output_impls.implementation output in O.print ~print_global_header ~file_names ~rules ~print ~location_style formatted_hunks ;; end let indentation line = let rec loop line len i n = if i >= len then n, i else ( match line.[i] with | ' ' -> loop line len (i + 1) (n + 1) (* tabs count for 4 spaces *) | '\t' -> loop line len (i + 1) (n + 4) | _ -> n, i) in loop line (String.length line) 0 0 ;; let score_line (side : [ `left | `right ]) line1 line2 : int = let i1, start_of_1 = indentation line1 in let i2, start_of_2 = indentation line2 in (* Order of priority is roughly: 1. low indentation for second line 2. lower indentation for second line than first 3. bonus points for certain patterns at the (non-whitespace) start of the line But it isn’t priority, we just add things. So a failure case may cause us to pick a boundary like this: {v (some subfield))) ------------- BOUNDARY ---------------- (other_field .)) ((new entry) ....) v} instead of between other_field and new entry. We try to counteract that for the case where the line below the boundary starts with e.g. ‘;;’ by removing the ‘decreasing indentation bonus’ in that case. Plausibly we could do something by taking into account the count of closing parens minus opening parens. A secondary issue is that [line1] or [line2] may be entirely whitespace which can be misleading (e.g. editors will typically remove indentation from pure-whitespace lines) and cause us to miss good information (e.g. for an added function we would like to have bonus points for the boundaries being just before ‘let ...’ and just after ‘;;’ but typically there is some whitespace added to be put before/after the function instead and that is what we score) *) let some_lines_are_blank = String.length line1 = 0 || String.length line2 = 0 in let base_score = let i2 = if some_lines_are_blank then max i1 i2 else i2 in max (-90) (90 - (i2 * 2)) in let decreasing_indentation_bonus = if some_lines_are_blank then 0 else (if i1 = i2 (* This funky thing hopefully us to prefer a diff like ‘end [ module ... end ]’ to one like ‘[ end module ... ] end’, where [] marks the boundary of the diff. *) then ( match side with | `left -> 1 | `right -> 0) else i1 - i2) |> Int.clamp_exn ~min:(-2) ~max:3 in let bonus_for_chars = (* [bonus n line sides str] returns [n] if a bonus score applies, or 0 otherwise. [line] can be [`above] or [`below]. [sides] can be [`left], [`right], or [`any]. The bonus score applies if [str] is found at the beginning of the line immediately [`above] or [`below] the boundary of the inserted/deleted region. If [sides] is [`left] or [`right], the bonus score only applies to that boundary of the diff region. So for example, [bonus 5 `above `any "</"] would add a bonus score of 5 if either boundary is immediately before a closing XML tag. *) let bonus n line sides str = let line, i = match line with | `above -> line1, start_of_1 | `below -> line2, start_of_2 in match sides, side with | `any, _ | `left, `left | `right, `right -> if String.is_substring_at line ~substring:str ~pos:i then n else 0 | _ -> 0 in bonus 1 `below `any "((" (* start of record bonus *) + bonus 3 `below `any "(" + bonus 1 `above `right "}" + bonus (-1) `below `any "}" + bonus 1 `below `any "{" (* XML. Big bonus here as we prefer to break between </ and < despite equal indentation. *) + bonus 5 `above `any "</" + bonus (-4) `below `left "</" (* discount for starting diff on a </...> *) + bonus 3 `below `any "<" + bonus 2 `below `any "*" (* heading *) + bonus 1 `below `any "-" (* bullet point *) + bonus 3 `above `right ";;" + bonus 1 `above `left ";;" + bonus 4 `below `left "let" + bonus (-1) `below `left "let%" + bonus 2 `below `left "let%test" + bonus 2 `below `left "let%expect" + bonus 2 `below `right "let" + bonus 1 `above `any "in" + bonus 4 `below `left "module" + bonus 3 `above `right "end" (* In these cases, we typically get decreasing indentation but we want the ending token (e.g. ;;) above the boundaries *) + bonus (min (-1) (-decreasing_indentation_bonus)) `below `any ";;" + bonus (min (-1) (-decreasing_indentation_bonus)) `below `any "end" (* starting on a blank line gives bonus *) + if start_of_2 >= String.length line2 then 2 else 0 in base_score + decreasing_indentation_bonus + bonus_for_chars ;; module Range_info = struct module T = struct type t = { range_index : int ; size_of_range : int ; replace_id : int option } [@@deriving compare, hash, sexp_of, fields ~getters] let compare_by_size = Comparable.lift Int.compare ~f:size_of_range end include T include Hashable.Make_plain (T) end (* Used to track the state of [Replace] ranges when we explode them into a [Prev] and a [Next] *) module Range_with_replaces_info = struct type t = { hunk_index : int ; range_type : [ `Original | `Former_replace of int | `Move ] } end let find_moves ~line_big_enough ~keep_ws (hunks : Hunks.t) = let minimum_match_perc = 0.7 in let minimum_lines = 3 in (* Rewrite [Replace] ranges as a [Prev] and [Next] so we can consider them for moves. Extract all ranges from the hunks so they are easier to work with *) let all_ranges = Queue.create () in let replace_id = ref 0 in List.iteri hunks ~f:(fun hunk_index hunk -> List.iter hunk.ranges ~f:(fun range -> match range with | Replace (prev, next, None) -> Queue.enqueue all_ranges ( { Range_with_replaces_info.hunk_index ; range_type = `Former_replace !replace_id } , Patience_diff.Range.Prev (prev, None) ); Queue.enqueue all_ranges ( { Range_with_replaces_info.hunk_index ; range_type = `Former_replace !replace_id } , Patience_diff.Range.Next (next, None) ); Int.incr replace_id | _ -> Queue.enqueue all_ranges ({ Range_with_replaces_info.hunk_index; range_type = `Original }, range))); let prev_ranges = Queue.create () in let next_ranges = Pairing_heap.create ~cmp:(Comparable.lift Range_info.compare_by_size ~f:fst) () in Queue.iteri all_ranges ~f:(fun range_index (replace_info, range) -> let replace_id = match replace_info.range_type with | `Former_replace id -> Some id | `Move | `Original -> None in match range with | Prev (range_contents, None) when Array.length range_contents >= minimum_lines -> Queue.enqueue prev_ranges ( { Range_info.range_index ; size_of_range = Array.sum (module Int) ~f:String.length range_contents ; replace_id } , range_contents ) | Next (range_contents, None) when Array.length range_contents >= minimum_lines -> Pairing_heap.add next_ranges ( { Range_info.range_index ; size_of_range = Array.sum (module Int) ~f:String.length range_contents ; replace_id } , range_contents ) | _ -> ()); let prevs_used = Range_info.Table.create () in let nexts_to_replace = Range_info.Table.create () in (* Find ranges that are similar enough to be moves *) let next_ranges = Array.init (Pairing_heap.length next_ranges) ~f:(fun _ -> Pairing_heap.pop_exn next_ranges) in let move_id = ref Patience_diff.Move_id.zero in Queue.iter prev_ranges ~f:(fun (prev_location, prev_contents) -> let starting_index = Array.binary_search next_ranges ~compare:(fun (next_range_info, _next_contents) prev_range_info -> Range_info.compare_by_size next_range_info prev_range_info) `Last_less_than_or_equal_to prev_location |> Option.value ~default:(Array.length next_ranges - 1) in let starting_index = if starting_index < 0 then 0 else starting_index in let left_index = ref starting_index in let right_index = ref (starting_index + 1) in let max_similarity range_a range_b = let a_size = Int.to_float range_a.Range_info.size_of_range in let b_size = Int.to_float range_b.Range_info.size_of_range in Float.min a_size b_size /. Float.max a_size b_size in let next_closest_range () = let left_range = if !left_index < 0 || !left_index >= Array.length next_ranges then None else Some next_ranges.(!left_index) in let right_range = if !right_index < 0 || !right_index >= Array.length next_ranges then None else Some next_ranges.(!right_index) in match left_range, right_range with | None, None -> None | Some left_range, None -> Int.decr left_index; Some left_range | None, Some right_range -> Int.incr right_index; Some right_range | Some (left_info, left_range), Some (right_info, right_range) -> if Float.compare (max_similarity left_info prev_location) (max_similarity right_info prev_location) >= 0 then ( Int.decr left_index; Some (left_info, left_range)) else ( Int.incr right_index; Some (right_info, right_range)) in let rec find_best_next_range best_match_so_far = let finish () = match best_match_so_far with | None -> () | Some (_, select_hunk) -> select_hunk () in match next_closest_range () with | None -> finish () | Some (next_location, next_contents) -> let max_similarity = max_similarity prev_location next_location in (* If this range can't possibly have the required similarity then none of the subsequent ranges can either so stop our search here *) if Float.(max_similarity < minimum_match_perc) || match best_match_so_far with | None -> false | Some (best_match_ratio, _) -> Float.(max_similarity < best_match_ratio) then finish () else if Hashtbl.mem nexts_to_replace next_location (* Don't use the two parts of a the same replace for moves *) || match next_location.replace_id, prev_location.replace_id with | Some next_id, Some prev_id when next_id = prev_id -> true | _ -> false then find_best_next_range best_match_so_far else ( let match_ratio = Patience_diff.String.match_ratio prev_contents next_contents in let select_hunk () = let hunk = let transform = if keep_ws then Fn.id else remove_ws in Patience_diff.String.get_hunks ~transform ~context:(-1) ~big_enough:line_big_enough ~max_slide:100 ~score:score_line ~prev:prev_contents ~next:next_contents () (* Negative [context] returns a singleton hunk *) |> List.hd_exn in let move_index = !move_id in Hashtbl.add_exn prevs_used ~key:prev_location ~data:(move_index, None, None); move_id := Patience_diff.Move_id.succ !move_id; let num_ranges = List.length hunk.ranges in let range_index_is_on_edge range_index = range_index = 0 || range_index = num_ranges - 1 in Hashtbl.add_exn nexts_to_replace ~key:next_location ~data: (List.filter_mapi hunk.ranges ~f:(fun range_index_within_move range -> match range with | Same contents -> Some (Patience_diff.Range.Next (Array.map ~f:snd contents, Some (Move move_index))) | Replace (prev, next, _) -> Some (Replace (prev, next, Some move_index)) | Prev (prev, _) -> if range_index_is_on_edge range_index_within_move then ( Hashtbl.update prevs_used prev_location ~f:(function | Some (move_index, beg_lines, end_lines) -> ( move_index , (if range_index_within_move = 0 then Some (Array.length prev) else beg_lines) , if range_index_within_move = num_ranges - 1 then Some (Array.length prev) else end_lines ) (* We should have added this prev range above *) | None -> assert false); None) else Some (Prev (prev, Some (Within_move move_index))) | Next (next, _) -> Some (Next ( next , if range_index_is_on_edge range_index_within_move then None else Some (Within_move move_index) )) | Unified (contents, _) -> Some (Unified (contents, Some move_index)))) in let best_match_so_far = match best_match_so_far with | None when Float.(match_ratio >= minimum_match_perc) -> Some (match_ratio, select_hunk) | None -> None | Some (best_match_ratio, _) -> if Float.(match_ratio > best_match_ratio) then Some (match_ratio, select_hunk) else best_match_so_far in find_best_next_range best_match_so_far) in find_best_next_range None); let prevs_by_range_index = Hashtbl.to_alist prevs_used |> List.map ~f:(fun (range_info, move_info) -> range_info.Range_info.range_index, move_info) |> Int.Table.of_alist_exn in let nexts_by_range_index = Hashtbl.to_alist nexts_to_replace |> List.map ~f:(fun (range_info, ranges_to_insert) -> range_info.Range_info.range_index, ranges_to_insert) |> Int.Table.of_alist_exn in (* update the [Next] ranges *) let ranges = Queue.mapi all_ranges ~f:(fun range_index (range_data, range) -> match ( Hashtbl.find prevs_by_range_index range_index , Hashtbl.find nexts_by_range_index range_index ) with (* This means we think the range is both a next and prev which is impossible *) | Some _, Some _ -> assert false | None, None -> [ range_data, range ] | Some (move_id, lines_to_trim_at_beg, lines_to_trim_at_end), None -> (match range with | Patience_diff.Range.Prev (contents, None) -> let lines_to_trim_at_beg = Option.value lines_to_trim_at_beg ~default:0 in let lines_to_trim_at_end = Option.value lines_to_trim_at_end ~default:0 in List.filter_opt [ (if lines_to_trim_at_beg = 0 then None else Some ( { range_data with range_type = `Original } , Patience_diff.Range.Prev (Array.sub contents ~pos:0 ~len:lines_to_trim_at_beg, None) )) ; Some ( { range_data with range_type = `Move } , Patience_diff.Range.Prev ( Array.sub contents ~pos:lines_to_trim_at_beg ~len: (Array.length contents - lines_to_trim_at_beg - lines_to_trim_at_end) , Some (Move move_id) ) ) ; (if lines_to_trim_at_end = 0 then None else Some ( { range_data with range_type = `Original } , Patience_diff.Range.Prev ( Array.sub contents ~pos:(Array.length contents - lines_to_trim_at_end) ~len:lines_to_trim_at_end , None ) )) ] | _ -> (* we should never reference anything except a [Prev] that hasn't been moved *) assert false) | None, Some ranges_to_replace -> let range_data = { range_data with range_type = `Move } in List.map ranges_to_replace ~f:(fun range -> range_data, range)) |> Queue.to_list |> List.concat in (* Recover any [Replace] ranges we broke up if we didn't use them for moves. *) let final_ranges = Queue.create () in let rec recover_replaces = function | ( { Range_with_replaces_info.range_type = `Former_replace _; hunk_index } , Patience_diff.Range.Prev (prev, None) ) :: ( { Range_with_replaces_info.range_type = `Former_replace _; hunk_index = _ } , Next (next, None) ) :: rest_ranges -> Queue.enqueue final_ranges ( { Range_with_replaces_info.range_type = `Original; hunk_index } , Patience_diff.Range.Replace (prev, next, None) ); recover_replaces rest_ranges | range :: rest_ranges -> Queue.enqueue final_ranges range; recover_replaces rest_ranges | [] -> () in recover_replaces ranges; (* Place the ranges in the correct hunks *) let final_hunks = List.mapi hunks ~f:(fun hunk_index hunk -> let ranges = let hunk_ranges = Queue.create () in Queue.drain final_ranges ~f:(fun (_, range) -> Queue.enqueue hunk_ranges range) ~while_:(fun (range_data, _) -> range_data.Range_with_replaces_info.hunk_index = hunk_index); Queue.to_list hunk_ranges in { hunk with ranges }) in final_hunks ;; let diff ~context ~line_big_enough ~keep_ws ~find_moves:should_find_moves ~prev ~next = let transform = if keep_ws then Fn.id else remove_ws in Patience_diff.String.get_hunks ~transform ~context ~big_enough:line_big_enough ~max_slide:100 ~score:score_line ~prev ~next () |> fun hunks -> if should_find_moves then find_moves ~line_big_enough ~keep_ws hunks else hunks ;; type word_or_newline = [ `Newline of int * string option (* (number of newlines, subsequent_whitespace) *) | `Word of string ] [@@deriving sexp_of] (* Splits an array of lines into an array of pieces (`Newlines and R.Words) *) let explode ar ~keep_ws = let words = Array.to_list ar in let words = if keep_ws then List.map words ~f:(split ~keep_ws) else List.map words ~f:whitespace_ignorant_split in let to_words l = List.map l ~f:(fun s -> `Word s) in (* [`Newline of (int * string option)] can be thought of as: [`Newline of ([`How_many_consecutive_newlines of int] * [`Some_subsequent_whitespace of string |`Empty_string ])] This representation is used to try to collapse consecutive whitespace as tightly as possible, but it's not a great abstraction, so some consecutive whitespace does not get collapsed. *) let words = List.concat_map words ~f:(fun x -> match x with | hd :: tl -> if keep_ws && (not (String.is_empty hd)) && is_ws hd then `Newline (1, Some hd) :: to_words tl else `Newline (1, None) :: `Word hd :: to_words tl | [] -> [ `Newline (1, None) ]) in let words = List.fold_right words ~init:[] ~f:(fun x acc -> (* look back at what we've accumulated so far to see if there's any whitespace that can be collapsed. *) match acc with | `Word s :: tl -> x :: `Word s :: tl | `Newline (i, None) :: tl -> (match x with | `Word s -> `Word s :: `Newline (i, None) :: tl | `Newline (j, opt) -> (* collapse the whitespace from each [`Newline] by summing how_many_consecutive_newlines from each (i+j) *) `Newline (i + j, opt) :: tl) | `Newline (i, Some s1) :: tl -> (match x with | `Word s2 -> `Word s2 :: `Newline (i, Some s1) :: tl | `Newline (j, opt) -> (* collapse the whitespace from each [`Newline] by concatenating any subsequent_whitespace (opt ^ s1) and summing how_many_consecutive_newlines (i+j) from each. *) let s1 = Option.value opt ~default:"" ^ s1 in `Newline (i + j, Some s1) :: tl) | [] -> [ x ]) in (* Throw away the very first `Newline *) let words = match words with | `Newline (i, opt) :: tl -> `Newline (i - 1, opt) :: tl | `Word _ :: _ | [] -> raise_s [%message "Expected words to start with a `Newline." (words : word_or_newline list)] in (* Append a newline to the end, if this array has any words *) let words = match words with | [] -> [] | [ `Newline (0, None) ] -> [] | list -> List.append list [ `Newline (1, None) ] in Array.of_list words ;; (* Takes hunks of `Words and `Newlines and collapses them back into lines, * formatting appropriately. *) let collapse ranges ~rule_same ~rule_prev ~rule_next ~kind ~output = (* flag indicates what kind of range is currently being collapsed *) let flag = ref `Same in (* segment is the current series of words being processed. *) let segment = ref [] in (* line is the current series of formatted segments *) let line = ref [] in (* lines is the return array *) let lines = ref [] in let apply ~rule = function | "" -> "" | s -> Output_ops.Rule.apply s ~rule ~output ~refined:false in (* * Finish the current segment by applying the appropriate format * and popping it on to the end of the current line *) let finish_segment () = let rule = match !flag with | `Same -> rule_same | `Prev -> rule_prev | `Next -> rule_next in let formatted_segment = List.rev !segment |> String.concat |> apply ~rule in line := formatted_segment :: !line; segment := [] in (* * Finish the current segment, apply the reset rule to the line, * and pop the finished line onto the return array *) let newline i = for _ = 1 to i do finish_segment (); lines := String.concat (List.rev !line) :: !lines; line := [] done in let f range = (* Extract the array, set flag appropriately, *) let ar = match (range : _ Patience_diff.Range.t) with | Same ar -> flag := `Same; (* R.Same ar is an array of tuples. The first tuple is an * element from the old file, the second tuple, an element * from the new file. Depending on what kind of collapse * this is, we want only one or the other. *) let f = match kind with | `Prev_only -> fst | `Next_only -> snd | `Unified -> snd in Array.map ar ~f | Prev (ar, _) -> flag := `Prev; ar | Next (ar, _) -> flag := `Next; ar | Replace _ | Unified _ -> (* When calling collapse, we always call * Patience_diff.unified first, which removes all R.Replaces * and R.Unifieds. *) assert false in (* Iterate through the elements of the range, appending each `Word to * segment and calling newline on each `Newline *) Array.iter ar ~f:(function | `Newline (i, None) -> newline i | `Newline (i, Some s) -> newline i; segment := s :: !segment | `Word s -> segment := s :: !segment); finish_segment () in List.iter ranges ~f; (match !line with | [] | [ "" ] -> () | line -> let line = String.concat (List.rev line) in if is_ws line then (* This branch was unreachable in our regression tests, but I can't prove it's unreachable in all cases. Rather than raise in production, let's drop this whitespace. *) () else raise_s [%message "Invariant violated: [collapse] got a line not terminated with a newline" (line : string)]); Array.of_list (List.rev !lines) ;; (* Get the hunks from two arrays of pieces (`Words and `Newlines) *) let diff_pieces ~prev_pieces ~next_pieces ~keep_ws ~word_big_enough = let context = -1 in let transform = if keep_ws then function | `Word s -> s | `Newline (lines, trailing_whitespace) -> Option.fold trailing_whitespace ~init:(String.make lines '\n') ~f:String.( ^ ) else function | `Word s -> remove_ws s | `Newline (0, _) -> "" | `Newline (_, _) -> " " in Patience_diff.String.get_hunks ~transform ~context ~big_enough:word_big_enough ~max_slide:0 ~prev:prev_pieces ~next:next_pieces () ;; let ranges_are_just_whitespace (ranges : _ Patience_diff.Range.t list) = List.for_all ranges ~f:(function | Prev (piece_array, _) | Next (piece_array, _) -> Array.for_all piece_array ~f:(function | `Word s -> String.is_empty (remove_ws s) | `Newline _ -> true) | _ -> true) ;; (* Interleaves the display of minus lines and plus lines so that equal words are presented close together. There is some heuristic for when we think doing this improves the diff. *) let split_for_readability rangelist = let ans : _ Patience_diff.Range.t list list ref = ref [] in let pending_ranges : _ Patience_diff.Range.t list ref = ref [] in let append_range range = pending_ranges := range :: !pending_ranges in List.iter rangelist ~f:(fun range -> let split_was_executed = match (range : _ Patience_diff.Range.t) with | Next _ | Prev _ | Replace _ | Unified _ -> false | Same seq -> let first_newline = Array.find_mapi seq ~f:(fun i -> function | `Word _, _ | _, `Word _ | `Newline (0, _), _ | _, `Newline (0, _) -> None | `Newline first_nlA, `Newline first_nlB -> Some (i, first_nlA, first_nlB)) in (match first_newline with | None -> false | Some (i, first_nlA, first_nlB) -> if Array.length seq - i <= Configuration.too_short_to_split then false else ( append_range (Same (Array.sub seq ~pos:0 ~len:i)); (* A non-zero `Newline is required for [collapse] to work properly. *) append_range (Same [| `Newline (1, None), `Newline (1, None) |]); ans := List.rev !pending_ranges :: !ans; pending_ranges := []; let suf = Array.sub seq ~pos:i ~len:(Array.length seq - i) in let decr_first (x, y) = x - 1, y in suf.(0) <- `Newline (decr_first first_nlA), `Newline (decr_first first_nlB); append_range (Same suf); true)) in if not split_was_executed then append_range range); List.rev (match !pending_ranges with | [] -> !ans | _ :: _ as ranges -> List.rev ranges :: !ans) ;; (* Refines the diff, splitting the lines into smaller arrays and diffing them, then collapsing them back into their initial lines after applying a format. *) let refine ~(rules : Format.Rules.t) ~produce_unified_lines ~output ~keep_ws ~split_long_lines ~interleave ~word_big_enough (hunks : string Patience_diff.Hunk.t list) = let rule_prev = rules.word_prev in let rule_next = rules.word_next in let collapse = collapse ~rule_prev ~rule_next ~output in let () = match output with | Ansi | Html -> () | Ascii -> if produce_unified_lines then failwith "produce_unified_lines is not supported in Ascii mode" in let console_width = lazy (match Output_impls.console_width () with | Error _ -> 80 | Ok width -> width) in let refine_range : _ Patience_diff.Range.t -> _ Patience_diff.Range.t list = function | Next (a, _) when (not keep_ws) && Array.for_all a ~f:is_ws -> [ Same (Array.zip_exn a a) ] | Prev (a, _) when (not keep_ws) && Array.for_all a ~f:is_ws -> [] | (Next _ | Prev _ | Same _ | Unified _) as range -> [ range ] | Replace (prev_ar, next_ar, move_kind) -> (* Explode the arrays *) let prev_pieces = explode prev_ar ~keep_ws in let next_pieces = explode next_ar ~keep_ws in (* Diff the pieces *) let sub_diff = diff_pieces ~prev_pieces ~next_pieces ~keep_ws ~word_big_enough in (* Smash the hunks' ranges all together *) let sub_diff = Patience_diff.Hunks.ranges sub_diff in (* Break it up where lines are too long *) let sub_diff_pieces = if not split_long_lines then [ sub_diff ] else ( let max_len = Int.max 20 (force console_width - 2) in (* Accumulates the total length of the line so far, summing lengths of word tokens but resetting when newlines are hit *) let get_new_len_so_far ~len_so_far tokens_arr = Array.fold ~init:len_so_far tokens_arr ~f:(fun len_so_far token -> match token with | `Newline _ -> 0 | `Word word -> len_so_far + String.length word) in (* Iteratively split long lines up. Produces a list of "range lists", where each range list should be displayed all together in one unbroken piece before being followed by the next range list, etc. *) let rec split_lines len_so_far sub_diff rangeaccum rangelistaccum = match sub_diff with | [] -> (match rangeaccum with | [] -> List.rev rangelistaccum | _ -> List.rev (List.rev rangeaccum :: rangelistaccum)) (* More tokens ranges left to process *) | range :: rest -> (match (range : _ Patience_diff.Range.t) with | Same tokenpairs_arr -> let range_of_tokens tokenpairs = Patience_diff.Range.Same (Array.of_list tokenpairs) in (* Keep taking tokens until we exceed max_len or hit a newline. Returns (new len_so_far, new range, remaining tokens, hit newline) *) let rec take_until_max len_so_far tokenpairs accum = match tokenpairs with | [] -> len_so_far, range_of_tokens (List.rev accum), [], false | ((token, _) as tokenpair) :: rest -> (match token with | `Newline _ -> 0, range_of_tokens (List.rev (tokenpair :: accum)), rest, true | `Word word -> let wordlen = String.length word in if wordlen + len_so_far > max_len && len_so_far > 0 then 0, range_of_tokens (List.rev accum), tokenpairs, false else take_until_max (wordlen + len_so_far) rest (tokenpair :: accum)) in let make_newline () = Patience_diff.Range.Same [| `Newline (1, None), `Newline (1, None) |] in (* Keep taking ranges until all tokens exhausted. Returns (new len_so_far, range list) *) let rec take_ranges_until_exhausted len_so_far tokenpairs accum = match tokenpairs with | [] -> len_so_far, List.rev accum | _ -> let new_len_so_far, new_range, new_tokenpairs, hit_newline = take_until_max len_so_far tokenpairs [] in let new_accum = `Range new_range :: accum in (* If there are token pairs left, that means we hit the max_len, so add a break at this point *) let new_accum = match new_tokenpairs with | _ :: _ when not hit_newline -> `Break :: `Range (make_newline ()) :: new_accum | _ -> new_accum in take_ranges_until_exhausted new_len_so_far new_tokenpairs new_accum in let new_len_so_far, new_ranges = take_ranges_until_exhausted len_so_far (Array.to_list tokenpairs_arr) [] in (* Update rangeaccum and rangelistaccum according to the `Ranges and `Breaks. `Ranges accumulate on to the existing range list to be displayed contiguously, `Breaks start a new range list. *) let rangeaccum, rangelistaccum = List.fold new_ranges ~init:(rangeaccum, rangelistaccum) ~f:(fun (rangeaccum, rangelistaccum) r -> match r with | `Break -> [], List.rev rangeaccum :: rangelistaccum | `Range r -> r :: rangeaccum, rangelistaccum) in split_lines new_len_so_far rest rangeaccum rangelistaccum | Next (tokens_arr, _) | Prev (tokens_arr, _) -> let new_len_so_far = get_new_len_so_far ~len_so_far tokens_arr in split_lines new_len_so_far rest (range :: rangeaccum) rangelistaccum | Replace (prev_arr, next_arr, _move_kind) -> let new_len_so_far = Int.max (get_new_len_so_far ~len_so_far prev_arr) (get_new_len_so_far ~len_so_far next_arr) in split_lines new_len_so_far rest (range :: rangeaccum) rangelistaccum | Unified _ -> assert false) in split_lines 0 sub_diff [] []) in let sub_diff_pieces = if interleave then List.concat_map sub_diff_pieces ~f:split_for_readability else sub_diff_pieces in List.concat_map sub_diff_pieces ~f:(fun sub_diff -> let sub_prev = Patience_diff.Range.prev_only sub_diff in let sub_next = Patience_diff.Range.next_only sub_diff in let all_same ranges = List.for_all ranges ~f:(fun range -> match (range : _ Patience_diff.Range.t) with | Same _ -> true | Prev (a, _) | Next (a, _) -> if keep_ws then false else Array.for_all a ~f:(function | `Newline _ -> true | `Word _ -> false) | _ -> false) in let prev_all_same = all_same sub_prev in let next_all_same = all_same sub_next in let produce_unified_lines = produce_unified_lines && (((not (ranges_are_just_whitespace sub_prev)) && next_all_same) || ((not (ranges_are_just_whitespace sub_next)) && prev_all_same)) in (* Collapse the pieces back into lines *) let prev_next_pairs = match prev_all_same, next_all_same with | true, true -> let kind = `Next_only in let rule_same = match move_kind with | None -> rules.word_same_unified | Some _ -> rules.word_same_unified_in_move in let next_ar = collapse sub_next ~rule_same ~kind in [ next_ar, next_ar ] | false, true -> let kind = `Prev_only in let rule_same = if produce_unified_lines then ( match move_kind with | None -> rules.word_same_unified | Some _ -> rules.word_same_unified_in_move) else rules.word_same_prev in let prev_ar = collapse sub_prev ~rule_same ~kind in let kind = `Next_only in let rule_same = rules.word_same_next in let next_ar = collapse sub_next ~rule_same ~kind in [ prev_ar, next_ar ] | true, false -> let kind = `Next_only in let rule_same = if produce_unified_lines then ( match move_kind with | None -> rules.word_same_unified | Some _ -> rules.word_same_unified_in_move) else rules.word_same_next in let next_ar = collapse sub_next ~rule_same ~kind in let kind = `Prev_only in let rule_same = rules.word_same_prev in let prev_ar = collapse sub_prev ~rule_same ~kind in [ prev_ar, next_ar ] | false, false -> let kind = `Prev_only in let rule_same = rules.word_same_prev in let prev_ar = collapse sub_prev ~rule_same ~kind in let kind = `Next_only in let rule_same = rules.word_same_next in let next_ar = collapse sub_next ~rule_same ~kind in [ prev_ar, next_ar ] in List.map prev_next_pairs ~f:(fun (prev_ar, next_ar) -> let range : _ Patience_diff.Range.t = match prev_all_same, next_all_same with | true, true -> Same (Array.map next_ar ~f:(fun x -> x, x)) | _ -> (match prev_ar, next_ar with (* Ugly hack that takes care of empty files *) | [| "" |], next_ar -> Replace ([||], next_ar, move_kind) | prev_ar, [| "" |] -> Replace (prev_ar, [||], move_kind) | prev_ar, next_ar -> (match produce_unified_lines, prev_all_same, next_all_same with | true, true, false -> Unified (next_ar, move_kind) | true, false, true -> Unified (prev_ar, move_kind) | false, _, _ | _, false, false -> Replace (prev_ar, next_ar, move_kind) | _ -> assert false)) in range)) in hunks |> List.map ~f:(fun hunk -> { hunk with ranges = List.concat_map hunk.ranges ~f:refine_range }) |> List.filter ~f:(not << Patience_diff.Hunk.all_same) ;; let print ~file_names ~rules ~output ~location_style hunks = Output_ops.print hunks ~rules ~output ~file_names ~print:(Printf.printf "%s\n") ~location_style ~print_global_header:true ;; let output_to_string ?(print_global_header = false) ~file_names ~rules ~output ~location_style hunks = let buf = Queue.create () in Output_ops.print hunks ~file_names ~location_style ~output ~print_global_header ~print:(Queue.enqueue buf) ~rules; String.concat (Queue.to_list buf) ~sep:"\n" ;; let iter_ansi ~rules ~f_hunk_break ~f_line hunks = let hunks = Output_ops.Rules.apply hunks ~rules ~output:Ansi in Hunks.iter ~f_hunk_break ~f_line hunks ;; let patdiff ?(context = Configuration.default_context) ?(keep_ws = false) ?(find_moves = false) ?(rules = Format.Rules.default) ?(output = Output.Ansi) ?(produce_unified_lines = true) ?(split_long_lines = true) ?print_global_header ?(location_style = Format.Location_style.Diff) ?(interleave = true) ?float_tolerance ?(line_big_enough = Configuration.default_line_big_enough) ?(word_big_enough = Configuration.default_word_big_enough) ~(prev : Diff_input.t) ~(next : Diff_input.t) () = let keep_ws = keep_ws || Should_keep_whitespace.for_diff ~prev ~next in let hunks = diff ~context ~keep_ws ~find_moves ~line_big_enough ~prev:(List.to_array (String.split_lines prev.text)) ~next:(List.to_array (String.split_lines next.text)) |> refine ~rules ~produce_unified_lines ~output ~keep_ws ~split_long_lines ~interleave ~word_big_enough in let hunks = match float_tolerance with | None -> hunks | Some tolerance -> Float_tolerance.apply hunks tolerance ~context in output_to_string ?print_global_header ~file_names:(Fake prev.name, Fake next.name) ~rules ~output ~location_style hunks ;; end module Without_unix = Make (struct let console_width () = Ok 80 let implementation : Output.t -> (module Output.S) = function | Ansi -> (module Ansi_output) | Ascii -> (module Ascii_output) | Html -> (module Html_output.Without_mtime) ;; end) module Private = struct module Make = Make end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>