forked from llvm/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhutilProseDiff.php
292 lines (246 loc) · 6.42 KB
/
PhutilProseDiff.php
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
final class PhutilProseDiff extends Phobject {
private $parts = array();
public function addPart($type, $text) {
$this->parts[] = array(
'type' => $type,
'text' => $text,
);
return $this;
}
public function getParts() {
return $this->parts;
}
/**
* Get diff parts, but replace large blocks of unchanged text with "."
* parts representing missing context.
*/
public function getSummaryParts() {
$parts = $this->getParts();
$head_key = head_key($parts);
$last_key = last_key($parts);
$results = array();
foreach ($parts as $key => $part) {
$is_head = ($key == $head_key);
$is_last = ($key == $last_key);
switch ($part['type']) {
case '=':
$pieces = $this->splitTextForSummary($part['text']);
if ($is_head || $is_last) {
$need = 2;
} else {
$need = 3;
}
// We don't have enough pieces to omit anything, so just continue.
if (count($pieces) < $need) {
$results[] = $part;
break;
}
if (!$is_head) {
$results[] = array(
'type' => '=',
'text' => head($pieces),
);
}
$results[] = array(
'type' => '.',
'text' => null,
);
if (!$is_last) {
$results[] = array(
'type' => '=',
'text' => last($pieces),
);
}
break;
default:
$results[] = $part;
break;
}
}
return $results;
}
public function reorderParts() {
// Reorder sequences of removed and added sections to put all the "-"
// parts together first, then all the "+" parts together. This produces
// a more human-readable result than intermingling them.
$o_run = array();
$n_run = array();
$result = array();
foreach ($this->parts as $part) {
$type = $part['type'];
switch ($type) {
case '-':
$o_run[] = $part;
break;
case '+':
$n_run[] = $part;
break;
default:
if ($o_run || $n_run) {
foreach ($this->combineRuns($o_run, $n_run) as $merged_part) {
$result[] = $merged_part;
}
$o_run = array();
$n_run = array();
}
$result[] = $part;
break;
}
}
if ($o_run || $n_run) {
foreach ($this->combineRuns($o_run, $n_run) as $part) {
$result[] = $part;
}
}
// Now, combine consecuitive runs of the same type of change (like a
// series of "-" parts) into a single run.
$combined = array();
$last = null;
$last_text = null;
foreach ($result as $part) {
$type = $part['type'];
if ($last !== $type) {
if ($last !== null) {
$combined[] = array(
'type' => $last,
'text' => $last_text,
);
}
$last_text = null;
$last = $type;
}
$last_text .= $part['text'];
}
if ($last_text !== null) {
$combined[] = array(
'type' => $last,
'text' => $last_text,
);
}
$this->parts = $combined;
return $this;
}
private function combineRuns($o_run, $n_run) {
$o_merge = $this->mergeParts($o_run);
$n_merge = $this->mergeParts($n_run);
// When removed and added blocks share a prefix or suffix, we sometimes
// want to count it as unchanged (for example, if it is whitespace) but
// sometimes want to count it as changed (for example, if it is a word
// suffix like "ing"). Find common prefixes and suffixes of these layout
// characters and emit them as "=" (unchanged) blocks.
$layout_characters = array(
' ' => true,
"\n" => true,
'.' => true,
'!' => true,
',' => true,
'?' => true,
']' => true,
'[' => true,
'(' => true,
')' => true,
'<' => true,
'>' => true,
);
$o_text = $o_merge['text'];
$n_text = $n_merge['text'];
$o_len = strlen($o_text);
$n_len = strlen($n_text);
$min_len = min($o_len, $n_len);
$prefix_len = 0;
for ($pos = 0; $pos < $min_len; $pos++) {
$o = $o_text[$pos];
$n = $n_text[$pos];
if ($o !== $n) {
break;
}
if (empty($layout_characters[$o])) {
break;
}
$prefix_len++;
}
$suffix_len = 0;
for ($pos = 0; $pos < ($min_len - $prefix_len); $pos++) {
$o = $o_text[$o_len - ($pos + 1)];
$n = $n_text[$n_len - ($pos + 1)];
if ($o !== $n) {
break;
}
if (empty($layout_characters[$o])) {
break;
}
$suffix_len++;
}
$results = array();
if ($prefix_len) {
$results[] = array(
'type' => '=',
'text' => substr($o_text, 0, $prefix_len),
);
}
if ($prefix_len < $o_len) {
$results[] = array(
'type' => '-',
'text' => substr(
$o_text,
$prefix_len,
$o_len - $prefix_len - $suffix_len),
);
}
if ($prefix_len < $n_len) {
$results[] = array(
'type' => '+',
'text' => substr(
$n_text,
$prefix_len,
$n_len - $prefix_len - $suffix_len),
);
}
if ($suffix_len) {
$results[] = array(
'type' => '=',
'text' => substr($o_text, -$suffix_len),
);
}
return $results;
}
private function mergeParts(array $parts) {
$text = '';
$type = null;
foreach ($parts as $part) {
$part_type = $part['type'];
if ($type === null) {
$type = $part_type;
}
if ($type !== $part_type) {
throw new Exception(pht('Can not merge parts of dissimilar types!'));
}
$text .= $part['text'];
}
return array(
'type' => $type,
'text' => $text,
);
}
private function splitTextForSummary($text) {
$matches = null;
$ok = preg_match('/^(\n*[^\n]+)\n/', $text, $matches);
if (!$ok) {
return array($text);
}
$head = $matches[1];
$text = substr($text, strlen($head));
$ok = preg_match('/\n([^\n]+\n*)\z/', $text, $matches);
if (!$ok) {
return array($text);
}
$last = $matches[1];
$text = substr($text, 0, -strlen($last));
if (!strlen(trim($text))) {
return array($head, $last);
} else {
return array($head, $text, $last);
}
}
}