Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yokurang committed Aug 16, 2024
1 parent 97bda80 commit 76d8520
Showing 1 changed file with 97 additions and 86 deletions.
183 changes: 97 additions & 86 deletions lib/HunkView.ml
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
open Nottui
module W = Nottui_widgets

(* Constants and Formatting Functions *)
let added_marker (content : Ui.t) : Ui.t = Ui.hcat [ W.string "+"; content ]
let removed_marker (content : Ui.t) : Ui.t = Ui.hcat [ W.string "-"; content ]
let unchanged_marker (content : Ui.t) : Ui.t = Ui.hcat [ W.string ""; content ]

(* Types *)

type line = Change of string | Common of string | Empty
type rendering_mode = Color | TextMarkers

(* Utility Functions *)
let line_marker (sign : string option) (content : Ui.t) : Ui.t =
match sign with
| Some s -> Ui.hcat [ W.string s; Ui.space 1 0; content ]
| None -> content

let split_and_align_hunk hunks : line list * line list =
let rec process_hunk mine_acc their_acc = function
Expand Down Expand Up @@ -56,10 +54,10 @@ let style_word (word : string)
match (mode, change_type) with
| Color, `Added -> W.string ~attr:Notty.A.(fg green) word
| Color, `Removed -> W.string ~attr:Notty.A.(fg red) word
| Color, `Unchanged -> W.string word
| TextMarkers, `Added -> added_marker (W.string word)
| TextMarkers, `Removed -> removed_marker (W.string word)
| TextMarkers, `Unchanged -> unchanged_marker (W.string word)
| Color, `Unchanged -> W.string ~attr:Notty.A.(fg lightcyan) word
| TextMarkers, `Added -> W.string ("+" ^ word)
| TextMarkers, `Removed -> W.string ("-" ^ word)
| TextMarkers, `Unchanged -> W.string word
in
Ui.hcat [ styled_word; W.string " " ]

Expand Down Expand Up @@ -94,33 +92,30 @@ let render_hunk_summary (hunk : string Patch.hunk) (mode : rendering_mode) :
let render_line_number (mine_num : int) (their_num : int)
(diff_type : [ `Added | `Removed | `Unchanged ]) (mode : rendering_mode) :
Ui.t =
match mode with
| TextMarkers -> (
match diff_type with
| `Added -> W.string (Printf.sprintf " %2d + " (their_num + 1))
| `Removed -> W.string (Printf.sprintf "%2d - " (mine_num + 1))
| `Unchanged ->
W.string (Printf.sprintf "%2d %2d " (mine_num + 1) (their_num + 1)))
| Color -> (
match diff_type with
| `Added ->
W.string
~attr:Notty.A.(fg green)
(Printf.sprintf " %2d + " (their_num + 1))
| `Removed ->
W.string
~attr:Notty.A.(fg red)
(Printf.sprintf "%2d - " (mine_num + 1))
| `Unchanged ->
Ui.hcat
[
W.string
~attr:Notty.A.(fg cyan)
(Printf.sprintf "%2d " (mine_num + 1));
W.string
~attr:Notty.A.(fg cyan)
(Printf.sprintf "%2d " (their_num + 1));
])
let attr_option =
match mode with
| TextMarkers -> None
| Color -> (
match diff_type with
| `Added -> Some Notty.A.(fg green)
| `Removed -> Some Notty.A.(fg red)
| `Unchanged -> Some Notty.A.(fg lightcyan))
in
match diff_type with
| `Added ->
W.string ?attr:attr_option (Printf.sprintf " %2d + " (their_num + 1))
| `Removed ->
W.string ?attr:attr_option (Printf.sprintf "%2d - " (mine_num + 1))
| `Unchanged ->
if mode = Color then
Ui.hcat
[
W.string ?attr:attr_option (Printf.sprintf "%2d " (mine_num + 1));
W.string ?attr:attr_option (Printf.sprintf "%2d " (their_num + 1));
]
else
W.string ?attr:attr_option
(Printf.sprintf "%2d %2d " (mine_num + 1) (their_num + 1))

let render_word_diff (words : WordDiff.word list)
(diff_type : [ `Added | `Removed | `Unchanged ]) (mode : rendering_mode) :
Expand Down Expand Up @@ -174,39 +169,55 @@ let render_hunk (hunk : string Patch.hunk) (mode : rendering_mode) : Ui.t =

(* Helper functions for side-by-side view *)

let lines_with_numbers (lines : line list) (attr_change : Notty.attr)
(marker : Ui.t -> Ui.t) (mode : rendering_mode) : Ui.t list =
let lines_with_numbers (lines : line list) (attr_change : Notty.attr option)
(change_type : [ `Add | `Remove | `Unchanged ]) (mode : rendering_mode) :
Ui.t list =
let rec process_lines line_num acc = function
| [] -> List.rev acc
| line :: rest ->
let content, attr, next_num =
let line_ui, next_num =
match line with
| Common s ->
let content = Printf.sprintf "%3d %s" line_num s in
(content, Notty.A.empty, line_num + 1)
let attr =
match mode with
| Color -> Some Notty.A.(fg lightcyan)
| TextMarkers -> None
in
(W.string ?attr content, line_num + 1)
| Change s ->
let content = Printf.sprintf "%3d %s" line_num s in
(content, attr_change, line_num + 1)
| Empty ->
let content = Printf.sprintf " " in
(content, Notty.A.empty, line_num)
in
let line_ui =
match mode with
| Color -> W.string ~attr content
| TextMarkers -> marker (W.string content)
let marker =
match change_type with
| `Add -> "+"
| `Remove -> "-"
| `Unchanged -> " "
in
let line_number = Printf.sprintf "%3d " line_num in
let content = s in
( (match mode with
| Color ->
W.string ?attr:attr_change
(line_number ^ marker ^ " " ^ content)
| TextMarkers ->
Ui.hcat
[
W.string line_number;
line_marker (Some marker) (W.string content);
]),
line_num + 1 )
| Empty -> (W.string " ", line_num)
in
process_lines next_num (line_ui :: acc) rest
in
process_lines 1 [] lines

let create_summary (start_line_num : int) (hunk_length : int)
(attr : Notty.attr) (change_type : [ `Add | `Remove ]) : Ui.t =
(attr : Notty.attr option) (change_type : [ `Add | `Remove ]) : Ui.t =
let sign = match change_type with `Add -> "+" | `Remove -> "-" in
let summary =
Printf.sprintf "@@ %s%d,%d @@" sign start_line_num hunk_length
in
W.string ~attr summary
W.string ?attr summary

(* Main View Functions *)

Expand Down Expand Up @@ -236,39 +247,39 @@ let current_hunks (z_patches : string Patch.t Zipper.t) (mode : rendering_mode)
let hunks = List.map (fun hunk -> ui_unified_diff hunk mode) p.Patch.hunks in
Ui.vcat hunks

let render_side_by_side (hunk : string Patch.hunk) (mode : rendering_mode) :
Ui.t =
let mine_lines, their_lines = split_and_align_hunk hunk.Patch.lines in
let render_lines lines change_type attr =
let attr_option =
match mode with Color -> Some attr | TextMarkers -> None
in
lines_with_numbers lines attr_option change_type mode
in
let content_mine = render_lines mine_lines `Remove Notty.A.(fg red) in
let content_their = render_lines their_lines `Add Notty.A.(fg green) in
let summary_mine =
create_summary
(hunk.Patch.mine_start + 1)
hunk.Patch.mine_len
(match mode with Color -> Some Notty.A.(fg red) | TextMarkers -> None)
`Remove
in
let summary_their =
create_summary
(hunk.Patch.their_start + 1)
hunk.Patch.their_len
(match mode with Color -> Some Notty.A.(fg green) | TextMarkers -> None)
`Add
in
Ui.hcat
[
Ui.resize ~w:0 ~sw:2 (Ui.vcat (summary_mine :: content_mine));
Ui.space 1 0;
Ui.resize ~w:0 ~sw:2 (Ui.vcat (summary_their :: content_their));
]

let current_hunks_side_by_side (z_patches : string Patch.t Zipper.t)
(mode : rendering_mode) : Ui.t =
let p = Zipper.get_focus z_patches in
let render_side_by_side hunk =
let mine_lines, their_lines = split_and_align_hunk hunk.Patch.lines in
let render_lines lines marker attr =
lines_with_numbers lines attr marker mode
in
let content_mine =
render_lines mine_lines removed_marker Notty.A.(fg red)
in
let content_their =
render_lines their_lines added_marker Notty.A.(fg green)
in
let summary_mine =
create_summary
(hunk.Patch.mine_start + 1)
hunk.Patch.mine_len
Notty.A.(fg red)
`Remove
in
let summary_their =
create_summary
(hunk.Patch.their_start + 1)
hunk.Patch.their_len
Notty.A.(fg green)
`Add
in
Ui.hcat
[
Ui.resize ~w:0 ~sw:2 (Ui.vcat (summary_mine :: content_mine));
Ui.space 1 0;
Ui.resize ~w:0 ~sw:2 (Ui.vcat (summary_their :: content_their));
]
in
Ui.vcat (List.map render_side_by_side p.Patch.hunks)
Ui.vcat (List.map (fun hunk -> render_side_by_side hunk mode) p.Patch.hunks)

0 comments on commit 76d8520

Please sign in to comment.