Skip to content

Commit

Permalink
Added line numbers to diffs (#54)
Browse files Browse the repository at this point in the history
* modified patch to include line numbers
* fixed bug for hunk starting line
* added tests for line numbering
  • Loading branch information
Siddhi-agg authored Apr 5, 2024
1 parent 65b6e3f commit 4810e00
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 27 deletions.
2 changes: 1 addition & 1 deletion test/cram/dune
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
(./interactive_viewer_dummy.exe as dummy_terminal))))

(cram
(deps %{bin:dummy_terminal} ./example.diff))
(deps %{bin:dummy_terminal} ./example.diff ./more-examples.diff))
16 changes: 16 additions & 0 deletions test/cram/more-examples.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --git a/file.txt b/file.txt
index e03c4a4..cb71f31 100644
--- a/file.txt
+++ b/file.txt
@@ -2,1 +2,2 @@
-Hi everyone!
+Hello World!
+This is the diffcessible project.
diff --git a/file.txt b/file.txt
index 57b873d..68cebdf 100644
--- a/file.txt
+++ b/file.txt
@@ -3,1 +5,2 @@
-This file starts at line 3.
+This file starts at line 5.
+This is the second test case in this file.
86 changes: 64 additions & 22 deletions test/cram/test.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ This is a cram test for the new executable.
Operation 1 of 14, 1 hunk
1 addition, 1 removal
Modification of bin/dune
@@ -0,4 +0,4 @@
(executable
(public_name diffcessible)
(name main)
- (libraries diffcessible cmdliner))
+ (libraries diffcessible cmdliner patch))
@@ -1,4 +1,4 @@
1 1 (executable
2 2 (public_name diffcessible)
3 3 (name main)
4 - (libraries diffcessible cmdliner))
4 + (libraries diffcessible cmdliner patch))



Expand All @@ -26,17 +26,17 @@ This is a cram test for the new executable.
Operation 2 of 14, 1 hunk
3 additions, 1 removal
Modification of bin/main.ml
@@ -0,7 +0,9 @@
open Diffcessible

let main () =
- Interactive_viewer.start ()
+ let s = In_channel.input_all In_channel.stdin in
+ let patch = Patch.to_diffs s in
+ Interactive_viewer.start patch

open Cmdliner

@@ -1,7 +1,9 @@
1 1 open Diffcessible
2 2
3 3 let main () =
4 - Interactive_viewer.start ()
4 + let s = In_channel.input_all In_channel.stdin in
5 + let patch = Patch.to_diffs s in
6 + Interactive_viewer.start patch
5 7
6 8 open Cmdliner
7 9



Expand All @@ -47,11 +47,11 @@ This is a cram test for the new executable.
Operation 3 of 14, 1 hunk
1 addition, 1 removal
Modification of lib/dune
@@ -0,3 +0,3 @@
(library
(name diffcessible)
- (libraries notty nottui lwd))
+ (libraries notty nottui lwd patch))
@@ -1,3 +1,3 @@
1 1 (library
2 2 (name diffcessible)
3 - (libraries notty nottui lwd))
3 + (libraries notty nottui lwd patch))



Expand Down Expand Up @@ -85,3 +85,45 @@ This is a cram test for the new executable.


Type 'q' to exit the help panel
$ dummy_terminal more-examples.diff h q
Operation 1 of 2, 1 hunk
2 additions, 1 removal
Modification of file.txt
@@ -2,1 +2,2 @@
2 - Hi everyone!
2 + Hello World!
3 + This is the diffcessible project.












Type 'h' to go to the help panel, 'q' to quit, 'n' to go to the next operation, 'p' to go to the previous operation
$ dummy_terminal more-examples.diff n
Operation 2 of 2, 1 hunk
2 additions, 1 removal
Modification of file.txt
@@ -3,1 +5,2 @@
3 - This file starts at line 3.
5 + This file starts at line 5.
6 + This is the second test case in this file.












Type 'h' to go to the help panel, 'q' to quit, 'n' to go to the next operation, 'p' to go to the previous operation
18 changes: 14 additions & 4 deletions vendor/patch/src/patch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,28 @@ type hunk = {
}

let unified_diff hunk =
let their_line_num = ref hunk.their_start in
let mine_line_num = ref hunk.mine_start in
(* TODO *)
String.concat "\n"
(List.map
(function
| `Common line -> " " ^ line
| `Mine line -> "- " ^ line
| `Their line -> "+ " ^ line)
| `Common line ->
incr their_line_num;
incr mine_line_num;
(Printf.sprintf "%2d %2d %s" !mine_line_num !their_line_num line)
| `Their line ->
incr their_line_num;
(Printf.sprintf " %2d + %s" !their_line_num line)
| `Mine line ->
incr mine_line_num;
(Printf.sprintf "%2d - %s" !mine_line_num line))
hunk.lines)

let pp_hunk ppf hunk =
Format.fprintf ppf "@@@@ -%d,%d +%d,%d @@@@\n%s"
hunk.mine_start hunk.mine_len hunk.their_start hunk.their_len
(if hunk.mine_len = 0 then 0 else hunk.mine_start+1) hunk.mine_len
(if hunk.their_len = 0 then 0 else hunk.their_start+1) hunk.their_len
(unified_diff hunk)

let take data num =
Expand Down

0 comments on commit 4810e00

Please sign in to comment.