Skip to content

Commit 2e27ef6

Browse files
committed
Pretty printer: Fix no_nl support
1 parent 87f98e7 commit 2e27ef6

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/patch.ml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@ type hunk = {
5252
their : string list ;
5353
}
5454

55-
let unified_diff hunk =
55+
let unified_diff ~mine_no_nl ~their_no_nl hunk =
56+
let no_nl_str = ["\\ No newline at end of file"] in
5657
(* TODO *)
5758
String.concat "\n" (List.map (fun line -> "-" ^ line) hunk.mine @
58-
List.map (fun line -> "+" ^ line) hunk.their)
59+
(if mine_no_nl then no_nl_str else []) @
60+
List.map (fun line -> "+" ^ line) hunk.their @
61+
(if their_no_nl then no_nl_str else []))
5962

60-
let pp_hunk ppf hunk =
63+
let pp_hunk ~mine_no_nl ~their_no_nl ppf hunk =
6164
Format.fprintf ppf "@@@@ -%d,%d +%d,%d @@@@\n%s"
6265
hunk.mine_start hunk.mine_len hunk.their_start hunk.their_len
63-
(unified_diff hunk)
66+
(unified_diff ~mine_no_nl ~their_no_nl hunk)
6467

6568
let take data num =
6669
let rec take0 num data acc =
@@ -215,9 +218,16 @@ type t = {
215218
their_no_nl : bool ;
216219
}
217220

218-
let pp ~git ppf t =
219-
pp_operation ~git ppf t.operation ;
220-
List.iter (pp_hunk ppf) t.hunks
221+
let pp ~git ppf {operation; hunks; mine_no_nl; their_no_nl} =
222+
pp_operation ~git ppf operation;
223+
let rec aux = function
224+
| [] -> ()
225+
| [x] -> pp_hunk ~mine_no_nl ~their_no_nl ppf x
226+
| x::xs ->
227+
pp_hunk ~mine_no_nl:false ~their_no_nl:false ppf x;
228+
aux xs
229+
in
230+
aux hunks
221231

222232
let operation_of_strings git mine their =
223233
let get_filename_opt n =

src/patch.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type hunk = {
1111
(** A hunk contains some difference between two files: each with a start line
1212
and length, and then the content as lists of string. *)
1313

14-
val pp_hunk : Format.formatter -> hunk -> unit
14+
val pp_hunk : mine_no_nl:bool -> their_no_nl:bool -> Format.formatter -> hunk -> unit
1515
(** [pp_hunk ppf hunk] pretty-prints the [hunk] on [ppf], the printing is in the
1616
same format as [diff] does. *)
1717

test/test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let hunk_eq a b =
1010
List.for_all (fun x -> List.mem x b.mine) a.mine &&
1111
List.for_all (fun x -> List.mem x b.their) a.their
1212

13-
let test_hunk = Alcotest.testable Patch.pp_hunk hunk_eq
13+
let test_hunk = Alcotest.testable (Patch.pp_hunk ~mine_no_nl:false ~their_no_nl:false) hunk_eq
1414

1515
let patch_eq a b =
1616
let open Patch in

0 commit comments

Comments
 (0)