-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,47 @@ | ||
type 'a t = Common of 'a | Changed of { mine : 'a list; their : 'a list } | ||
|
||
(* Helper function implementations *) | ||
let rec group_lines acc current_mine current_their = function | ||
| [] -> | ||
if current_mine <> [] || current_their <> [] then | ||
Changed { mine = current_mine; their = current_their } :: acc | ||
else acc | ||
| `Common line :: rest -> | ||
let acc' = | ||
let of_hunk (hunk : 'a Patch.hunk) : 'a t list = | ||
let rec process_hunk acc current_mine current_their = function | ||
| [] -> | ||
if current_mine <> [] || current_their <> [] then | ||
Changed { mine = current_mine; their = current_their } :: acc | ||
Changed | ||
{ mine = List.rev current_mine; their = List.rev current_their } | ||
:: acc | ||
else acc | ||
in | ||
group_lines (Common line :: acc') [] [] rest | ||
| `Mine line :: rest -> | ||
group_lines acc (line :: current_mine) current_their rest | ||
| `Their line :: rest -> | ||
group_lines acc current_mine (line :: current_their) rest | ||
| `Common line :: rest -> | ||
let acc' = | ||
if current_mine <> [] || current_their <> [] then | ||
Changed | ||
{ mine = List.rev current_mine; their = List.rev current_their } | ||
:: acc | ||
else acc | ||
in | ||
process_hunk (Common line :: acc') [] [] rest | ||
| `Mine line :: rest -> | ||
process_hunk acc (line :: current_mine) current_their rest | ||
| `Their line :: rest -> | ||
process_hunk acc current_mine (line :: current_their) rest | ||
in | ||
List.rev (process_hunk [] [] [] hunk.lines) | ||
|
||
let rec process_blocks lines mine_len their_len = function | ||
| [] -> (lines, mine_len, their_len) | ||
| Common line :: rest -> | ||
process_blocks (`Common line :: lines) (mine_len + 1) (their_len + 1) rest | ||
| Changed { mine; their } :: rest -> | ||
let mine_lines = List.rev_map (fun line -> `Mine line) mine in | ||
let their_lines = List.rev_map (fun line -> `Their line) their in | ||
process_blocks | ||
(List.rev_append mine_lines (List.rev_append their_lines lines)) | ||
(mine_len + List.length mine) | ||
(their_len + List.length their) | ||
rest | ||
|
||
(* End of helper function implementations *) | ||
|
||
(* Main block function implementations *) | ||
let of_hunk (_hunk : 'a Patch.hunk) : 'a t list = | ||
List.rev (group_lines [] [] [] _hunk.lines) | ||
|
||
let to_hunk (_blocks : 'a t list) : 'a Patch.hunk = | ||
let lines, mine_len, their_len = process_blocks [] 0 0 _blocks in | ||
let to_hunk (blocks : 'a t list) : 'a Patch.hunk = | ||
let lines, mine_len, their_len = | ||
List.fold_left | ||
(fun (lines, mine_len, their_len) block -> | ||
match block with | ||
| Common line -> (`Common line :: lines, mine_len + 1, their_len + 1) | ||
| Changed { mine; their } -> | ||
let mine_lines = List.map (fun line -> `Mine line) mine in | ||
let their_lines = List.map (fun line -> `Their line) their in | ||
( mine_lines @ their_lines @ lines, | ||
mine_len + List.length mine, | ||
their_len + List.length their )) | ||
([], 0, 0) blocks | ||
in | ||
{ | ||
mine_start = 0; | ||
mine_len; | ||
their_start = 0; | ||
their_len; | ||
lines = List.rev lines; | ||
} | ||
|
||
(* End of main block function implementations *) |