| 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 | (** |
| 12 | Computes the differences between 2 Pp's and adds additional tags to a Pp |
| 13 | to highlight them. Strings are split into tokens using the Rocq lexer, |
| 14 | then the lists of tokens are diffed using the Myers algorithm. A fixup routine, |
| 15 | shorten_diff_span, shortens the span of the diff result in some cases. |
| 16 | |
| 17 | Highlights use 4 tags to specify the color and underline/strikeout. These are |
| 18 | "diffs.added", "diffs.removed", "diffs.added.bg" and "diffs.removed.bg". The |
| 19 | first two are for added or removed text; the last two are for unmodified parts |
| 20 | of a modified item. Diffs that span multiple strings in the Pp are tagged with |
| 21 | "start.diff.*" and "end.diff.*", but only on the first and last strings of the span. |
| 22 | |
| 23 | If the inputs are not acceptable to the lexer, break the strings into |
| 24 | lists of tokens and call diff_strs, then add_diff_tags with a Pp.t that matches |
| 25 | the input lists of strings. Tokens that the lexer doesn't return exactly as they |
| 26 | appeared in the input will raise an exception in add_diff_tags (e.g. comments |
| 27 | and quoted strings). Fixing that requires tweaking the lexer. |
| 28 | |
| 29 | Limitations/Possible enhancements: |
| 30 | |
| 31 | - Make diff_pp immune to unlexable strings by adding a flag to the lexer. |
| 32 | *) |
| 33 | |
| 34 | (** Compute the diff between two Pp.t structures and return |
| 35 | versions of each with diffs highlighted as (old, new) *) |
| 36 | val diff_pp : ?tokenize_string:(string -> string list) -> Pp.t -> Pp.t -> Pp.t * Pp.t |
| 37 | |
| 38 | (** Compute the diff between two Pp.t structures and return |
| 39 | a highlighted Pp.t. If [show_removed] is true, show separate lines for |
| 40 | removals and additions, otherwise only show additions *) |
| 41 | val diff_pp_combined : ?tokenize_string:(string -> string list) -> ?show_removed:bool -> Pp.t -> Pp.t -> Pp.t |
| 42 | |
| 43 | (** Raised if the diff fails *) |
| 44 | exception Diff_Failure of string |
| 45 | |
| 46 | module StringDiff : |
| 47 | sig |
| 48 | type elem = String.t |
| 49 | type t = elem array |
| 50 | end |
| 51 | |
| 52 | type diff_type = |
| 53 | [ `Removed |
| 54 | | `Added |
| 55 | | `Common |
| 56 | ] |
| 57 | |
| 58 | type diff_list = StringDiff.elem Diff2.edit list |
| 59 | |
| 60 | (** Compute the difference between 2 strings in terms of tokens, using the |
| 61 | lexer to identify tokens. |
| 62 | |
| 63 | If the strings are not lexable, this routine will raise Diff_Failure. |
| 64 | (I expect to modify the lexer soon so this won't happen.) |
| 65 | |
| 66 | Therefore you should catch any exceptions. The workaround for now is for the |
| 67 | caller to tokenize the strings itself and then call diff_strs. |
| 68 | *) |
| 69 | val diff_str : ?tokenize_string:(string -> string list) -> string -> string -> StringDiff.elem Diff2.edit list |
| 70 | |
| 71 | (** Compute the differences between 2 lists of strings, treating the strings |
| 72 | in the lists as indivisible units. |
| 73 | *) |
| 74 | val diff_strs : StringDiff.t -> StringDiff.t -> StringDiff.elem Diff2.edit list |
| 75 | |
| 76 | (** Generate a new Pp that adds tags marking diffs to a Pp structure: |
| 77 | which: either `Added or `Removed, indicates which type of diffs to add |
| 78 | pp: the original structure. For `Added, must be the new pp passed to diff_pp |
| 79 | For `Removed, must be the old pp passed to diff_pp. Passing the wrong one |
| 80 | will likely raise Diff_Failure. |
| 81 | diffs: the diff list returned by diff_pp |
| 82 | |
| 83 | Diffs of single strings in the Pp are tagged with "diff.added" or "diff.removed". |
| 84 | Diffs that span multiple strings in the Pp are tagged with "start.diff.*" or |
| 85 | "end.diff.*", but only on the first and last strings of the span. |
| 86 | |
| 87 | Ppcmd_strings will be split into multiple Ppcmd_strings if a diff starts or ends |
| 88 | in the middle of the string. Whitespace just before or just after a diff will |
| 89 | not be part of the highlight. |
| 90 | |
| 91 | Preexisting tags in pp may contain only a single Ppcmd_string. Those tags will be |
| 92 | placed inside the diff tags to ensure proper nesting of tags within spans of |
| 93 | "start.diff.*" ... "end.diff.*". |
| 94 | |
| 95 | Under some "impossible" conditions, this routine may raise Diff_Failure. |
| 96 | If you want to make your call especially bulletproof, catch this |
| 97 | exception, print a user-visible message, then recall this routine with |
| 98 | the first argument set to None, which will skip the diff. |
| 99 | *) |
| 100 | val add_diff_tags : diff_type -> Pp.t -> StringDiff.elem Diff2.edit list -> Pp.t |
| 101 | |
| 102 | (** Returns a boolean pair (added, removed) for [diffs] where a true value |
| 103 | indicates that something was added/removed in the diffs. |
| 104 | *) |
| 105 | val has_changes : diff_list -> bool * bool |
| 106 | |
| 107 | val get_dinfo : StringDiff.elem Diff2.edit -> diff_type * string |
| 108 | |
| 109 | (** Returns a modified [pp] with the background highlighted with |
| 110 | "start.<diff_tag>.bg" and "end.<diff_tag>.bg" tags at the beginning |
| 111 | and end of the returned Pp.t |
| 112 | *) |
| 113 | val wrap_in_bg : string -> Pp.t -> Pp.t |
| 114 | |
| 115 | (** Displays the diffs to a printable format for debugging *) |
| 116 | val string_of_diffs : diff_list -> string |