Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit ede4d1c

Browse files
committed
Intraline colored diffs are now word-based instead of character-based
fixes #4
1 parent f5e54c5 commit ede4d1c

9 files changed

+20
-11
lines changed

src/ColoredUnifiedDiff.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ final public static function create(
110110
$next = Str\slice($next, 1);
111111
$lines = Vec\drop($lines, 1);
112112

113-
$intraline = StringDiff::characters($line, $next)->getDiff();
113+
$words_line = vec(\preg_split('/([^a-zA-Z0-9_]+)/', $line, -1, \PREG_SPLIT_DELIM_CAPTURE));
114+
$words_next = vec(\preg_split('/([^a-zA-Z0-9_]+)/', $next, -1, \PREG_SPLIT_DELIM_CAPTURE));
115+
$intraline = (new StringDiff($words_line, $words_next))->getDiff();
114116
$out[] = $intraline
115117
|> Vec\filter($$, $op ==> !$op is DiffInsertOp<_>)
116118
|> static::colorDeleteLineWithIntralineEdits($$);

tests/StringDiffTest.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ public function testCLIColoredDiff(string $name): void {
8787

8888
expect($diff)->toBeSame(
8989
\file_get_contents($base.'.clidiff.expect'),
90-
'Did not match expected contents (- %s.clidiff.expect, +%s.clidiff.out)',
91-
$name,
92-
$name,
93-
$name,
90+
'Did not match expected contents (- %s.clidiff.expect, + %s.clidiff.out)',
91+
$base,
92+
$base,
9493
);
9594
}
9695
}

tests/examples/lots_of_context_replace.clidiff.expect

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
 8
33
 9
44
 0
5-
-foo bat baz
6-
+foo bar baz
5+
-foo bat baz
6+
+foo bar baz
77
 1
88
 2
99
 3
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo Bat Baz
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo Bar Baz
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@@ -1 +1 @@
2+
-Foo Bat Baz
3+
+Foo Bar Baz
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@@ -1 +1 @@
2+
-Foo Bat Baz
3+
+Foo Bar Baz
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@@ -1 +1 @@
2-
-Framework
3-
+Facebook
2+
-Framework
3+
+Facebook
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@@ -1,2 +1 @@
22
-Foo
3-
-Bar
4-
+Baz
3+
-Bar
4+
+Baz

0 commit comments

Comments
 (0)