1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
open! Core
open! Import
module Color = Ansi_text.Color
module Style = Ansi_text.Attr
module Rule = struct
module Affix = struct
type t =
{ text : string
; styles : Style.t list
}
[@@deriving compare ~localize, sexp_of]
let create ?(styles = []) text = { text; styles }
let blank = create ""
let strip_styles t = { t with styles = [] }
end
type t =
{ pre : Affix.t
; suf : Affix.t
; styles : Style.t list
}
[@@deriving compare ~localize, fields ~iterators:map, sexp_of]
let create ?(pre = Affix.blank) ?(suf = Affix.blank) styles = { pre; suf; styles }
let blank = create []
let unstyled_prefix text = { blank with pre = Affix.create text }
let strip_styles t =
let f f field = f (Field.get field t) in
Fields.map
~pre:(f Affix.strip_styles)
~suf:(f Affix.strip_styles)
~styles:(f (const []))
;;
let strip_prefix t = { t with pre = Affix.blank }
let use_prefix_text_from t ~this_prefix =
{ t with pre = { t.pre with text = this_prefix.pre.text } }
;;
end
module Rules = struct
type =
{ line_same : Rule.t
; line_prev : Rule.t
; line_next : Rule.t
; line_unified : Rule.t
; word_same_prev : Rule.t
; word_same_next : Rule.t
; word_same_unified : Rule.t
; word_same_unified_in_move : Rule.t
; word_prev : Rule.t
; word_next : Rule.t
; hunk : Rule.t
; : Rule.t
; : Rule.t
; moved_from_prev : Rule.t
; moved_to_next : Rule.t
; removed_in_move : Rule.t
; added_in_move : Rule.t
; line_unified_in_move : Rule.t
}
[@@deriving compare ~localize, fields ~iterators:map, sexp_of]
module Color_palette = struct
type t =
{ added : Color.t
; removed : Color.t
; moved_from_prev : Color.t
; moved_to_next : Color.t
}
let default =
let open Ansi_text.Color in
{ added = Standard Sgr8.Green
; removed = Standard Sgr8.Red
; moved_from_prev = Standard Sgr8.Magenta
; moved_to_next = Standard Sgr8.Cyan
}
;;
end
let inner_line_change text color =
let style = [ Ansi_text.Attr.Fg color ] in
let pre = Rule.Affix.create ~styles:[ Bold; Fg color ] text in
Rule.create ~pre style
;;
let line_unified ~is_move =
let pre =
Rule.Affix.create
~styles:[ Bold; Ansi_text.(Attr.Fg (Color.Standard Color.Sgr8.Yellow)) ]
(if is_move then ">|" else "!|")
in
Rule.create ~pre []
;;
let word_change color = Rule.create [ Fg color ]
let default_with_color_palette (palette : Color_palette.t) =
let open Rule in
{ line_same = unstyled_prefix " "
; line_prev = inner_line_change "-|" palette.removed
; line_next = inner_line_change "+|" palette.added
; line_unified = line_unified ~is_move:false
; word_same_prev = blank
; word_same_next = blank
; word_same_unified = blank
; word_same_unified_in_move = blank
; word_prev = word_change palette.removed
; word_next = word_change palette.added
; hunk = blank
; header_prev = blank
; header_next = blank
; moved_from_prev = inner_line_change "<|" palette.moved_from_prev
; moved_to_next = inner_line_change ">|" palette.moved_to_next
; removed_in_move = inner_line_change ">|" palette.removed
; added_in_move = inner_line_change ">|" palette.added
; line_unified_in_move = line_unified ~is_move:true
}
;;
let default = default_with_color_palette Color_palette.default
let strip_styles t =
let f field = Rule.strip_styles (Field.get field t) in
Fields.map
~line_same:f
~line_prev:f
~line_next:f
~line_unified:f
~word_same_prev:f
~word_same_next:f
~word_same_unified:f
~word_same_unified_in_move:f
~word_prev:f
~word_next:f
~hunk:f
~header_prev:f
~header_next:f
~moved_from_prev:f
~moved_to_next:f
~removed_in_move:f
~added_in_move:f
~line_unified_in_move:f
;;
end
module Annotation_gutter = struct
type side =
| Prev of { line : int }
| Next of { line : int }
| Same of
{ prev : int
; next : int
}
| Unified of
{ prev : int
; next : int
}
[@@deriving sexp_of]
type t =
{ width : int
; render : side -> string
}
let empty = { width = 0; render = (fun _ -> "") }
let create ~width ~render =
let render side =
render side
|> Ansi_text.parse
|> Ansi_text.truncate ~width
|> Ansi_text.pad ~width
|> Ansi_text.to_string
in
{ width; render }
;;
end
module Location_style = struct
type t =
| Diff
| Omake
| None
| Separator
[@@deriving bin_io, compare ~localize, quickcheck, enumerate, equal ~localize, sexp]
let to_string = function
| Diff -> "diff"
| Omake -> "omake"
| None -> "none"
| Separator -> "separator"
;;
let of_string = function
| "diff" -> Diff
| "omake" -> Omake
| "none" -> None
| "separator" -> Separator
| other -> failwiths "invalid location style" other [%sexp_of: string]
;;
let omake_style_error_message_start ~file ~line =
sprintf "File \"%s\", line %d, characters 0-1:" file line
;;
let sprint t (hunk : string Patience_diff.Hunk.t) ~prev_filename ~rule =
match t with
| Diff ->
rule
(sprintf
"-%i,%i +%i,%i"
hunk.prev_start
hunk.prev_size
hunk.next_start
hunk.next_size)
| Omake ->
let prev_start =
with_return (fun r ->
List.fold hunk.ranges ~init:hunk.prev_start ~f:(fun init -> function
| Same s -> init + Array.length s
| Prev _ | Next _ | Replace _ | Unified _ -> r.return init))
in
omake_style_error_message_start ~file:prev_filename ~line:prev_start
| None -> rule ""
| Separator -> rule "=== DIFF HUNK ==="
;;
end