Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14089003
D16820.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D16820.diff
View Options
diff --git a/src/utils/PhutilProseDiff.php b/src/utils/PhutilProseDiff.php
--- a/src/utils/PhutilProseDiff.php
+++ b/src/utils/PhutilProseDiff.php
@@ -186,9 +186,9 @@
}
$suffix_len = 0;
- for ($pos = 1; $pos <= $min_len; $pos++) {
- $o = $o_text[$o_len - $pos];
- $n = $n_text[$n_len - $pos];
+ 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;
}
@@ -210,14 +210,20 @@
if ($prefix_len < $o_len) {
$results[] = array(
'type' => '-',
- 'text' => substr($o_text, $prefix_len, $o_len - $suffix_len),
+ '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 - $suffix_len),
+ 'text' => substr(
+ $n_text,
+ $prefix_len,
+ $n_len - $prefix_len - $suffix_len),
);
}
diff --git a/src/utils/PhutilProseDifferenceEngine.php b/src/utils/PhutilProseDifferenceEngine.php
--- a/src/utils/PhutilProseDifferenceEngine.php
+++ b/src/utils/PhutilProseDifferenceEngine.php
@@ -79,10 +79,10 @@
}
$pieces = preg_split($expr, $corpus, -1, PREG_SPLIT_DELIM_CAPTURE);
- return $this->stitchPieces($pieces);
+ return $this->stitchPieces($pieces, $level);
}
- private function stitchPieces(array $pieces) {
+ private function stitchPieces(array $pieces, $level) {
$results = array();
$count = count($pieces);
for ($ii = 0; $ii < $count; $ii += 2) {
@@ -90,7 +90,28 @@
if ($ii + 1 < $count) {
$result .= $pieces[$ii + 1];
}
- $results[] = $result;
+
+ if ($level == 1) {
+ // Split pieces into separate text and whitespace sections: make one
+ // piece out of all the whitespace at the beginning, one piece out of
+ // all the actual text in the middle, and one piece out of all the
+ // whitespace at the end.
+
+ $matches = null;
+ preg_match('/^(\s*)(.*?)(\s*)\z/', $result, $matches);
+
+ if (strlen($matches[1])) {
+ $results[] = $matches[1];
+ }
+ if (strlen($matches[2])) {
+ $results[] = $matches[2];
+ }
+ if (strlen($matches[3])) {
+ $results[] = $matches[3];
+ }
+ } else {
+ $results[] = $result;
+ }
}
// If the input ended with a delimiter, we can get an empty final piece.
diff --git a/src/utils/__tests__/PhutilProseDiffTestCase.php b/src/utils/__tests__/PhutilProseDiffTestCase.php
--- a/src/utils/__tests__/PhutilProseDiffTestCase.php
+++ b/src/utils/__tests__/PhutilProseDiffTestCase.php
@@ -122,6 +122,14 @@
),
pht('Summary diff with last change.'));
+ $this->assertProseParts(
+ 'aaa aaa aaa aaa, bbb bbb bbb bbb.',
+ "aaa aaa aaa aaa, bbb bbb bbb bbb.\n\n- ccc ccc ccc",
+ array(
+ '= aaa aaa aaa aaa, bbb bbb bbb bbb.',
+ "+ \n\n- ccc ccc ccc",
+ ),
+ pht('Diff with new trailing content.'));
}
private function assertProseParts($old, $new, array $expect_parts, $label) {
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 25, 7:24 AM (13 h, 51 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6785319
Default Alt Text
D16820.diff (3 KB)
Attached To
Mode
D16820: Slightly improve some prose diffs
Attached
Detach File
Event Timeline
Log In to Comment