Skip to content

Commit

Permalink
Add a command flag for word diff textual diff output
Browse files Browse the repository at this point in the history
  • Loading branch information
azzsal committed Feb 21, 2025
1 parent 09955ea commit eea7bb6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
18 changes: 15 additions & 3 deletions bin/api_diff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ let mode ~reference ~current ~main_module ~unwrapped =
"Either --main-module or --unwrapped must be provided when diffing \
entire libraries."

let run (`Main_module main_module) (`Unwrapped_library unwrapped)
let run (`Word_diff word_diff) (`Main_module main_module)
(`Unwrapped_library unwrapped)
(`Ref_cmi reference) (`Current_cmi current) =
let open CCResult.Infix in
let* reference_map, current_map =
Expand Down Expand Up @@ -75,12 +76,23 @@ let run (`Main_module main_module) (`Unwrapped_library unwrapped)
List.iter
(fun diff ->
let text_diff = Api_watch.Text_diff.from_diff diff in
Api_watch.Text_diff.With_colors.pp Format.std_formatter text_diff)
if word_diff
then Api_watch.Text_diff.With_colors.pp Format.std_formatter text_diff
else Api_watch.Text_diff.Word.pp Format.std_formatter text_diff)
diff_map;
if has_changes then Ok 1 else Ok 0

let named f = Cmdliner.Term.(app (const f))

let word_diff =
let doc =
"Show changes in a signature item inline, where a removed part of an item is wrapped in
$(b,[-removed-]) and an added one is wrapped in $(b,{+added+})"
in
named
(fun x -> `Word_diff x)
Cmdliner.Arg.(value & flag & info ~doc [ "word-diff" ])

let main_module =
let docv = "MAIN_MODULE_NAME" in
let doc =
Expand Down Expand Up @@ -128,7 +140,7 @@ let info =

let term =
Cmdliner.Term.(
const run $ main_module $ unwrapped_library $ ref_cmi $ current_cmi)
const run $ word_diff $ main_module $ unwrapped_library $ ref_cmi $ current_cmi)

let () =
Fmt_tty.setup_std_outputs ();
Expand Down
4 changes: 4 additions & 0 deletions lib/text_diff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,7 @@ module With_colors = struct
let pp_diff fmt diff = pp_ printer fmt diff
let pp fmt t = gen_pp pp_diff fmt t
end

module Word = struct
let pp _fmt _t = assert false
end
4 changes: 4 additions & 0 deletions lib/text_diff.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ module With_colors : sig
(** Same as regular [pp] but prints added lines in green and removed lines in
red. *)
end

module Word : sig
val pp : Format.formatter -> t -> unit
end

0 comments on commit eea7bb6

Please sign in to comment.