Skip to content

Commit 795628f

Browse files
committed
Fix all-equal hunk content should be omitted in Context output
Signed-off-by: Jack Cherng <[email protected]>
1 parent 0f2e39a commit 795628f

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Renderer/Text/Context.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ final class Context extends AbstractText
3232
SequenceMatcher::OP_REP => '!',
3333
];
3434

35+
/**
36+
* @var int the union of OPs that indicate there is a change
37+
*/
38+
const OP_BLOCK_CHANGED =
39+
SequenceMatcher::OP_INS |
40+
SequenceMatcher::OP_DEL |
41+
SequenceMatcher::OP_REP;
42+
3543
/**
3644
* {@inheritdoc}
3745
*/
@@ -42,6 +50,7 @@ protected function renderWorker(Differ $differ): string
4250
foreach ($differ->getGroupedOpcodes() as $hunk) {
4351
$lastBlockIdx = \count($hunk) - 1;
4452

53+
// note that these line number variables are 0-based
4554
$i1 = $hunk[0][1];
4655
$i2 = $hunk[$lastBlockIdx][2];
4756
$j1 = $hunk[0][3];
@@ -82,16 +91,21 @@ protected function renderHunkHeader(string $delimiter, int $a1, int $a2): string
8291
protected function renderHunkOld(Differ $differ, array $hunk): string
8392
{
8493
$ret = '';
94+
$hasChangeInHunk = false;
8595

8696
foreach ($hunk as [$op, $i1, $i2, $j1, $j2]) {
8797
if ($op === SequenceMatcher::OP_INS) {
8898
continue;
8999
}
90100

101+
if ($op & self::OP_BLOCK_CHANGED) {
102+
$hasChangeInHunk = true;
103+
}
104+
91105
$ret .= $this->renderContext(self::TAG_MAP[$op], $differ->getOld($i1, $i2));
92106
}
93107

94-
return $ret;
108+
return $hasChangeInHunk ? $ret : '';
95109
}
96110

97111
/**
@@ -103,16 +117,21 @@ protected function renderHunkOld(Differ $differ, array $hunk): string
103117
protected function renderHunkNew(Differ $differ, array $hunk): string
104118
{
105119
$ret = '';
120+
$hasChangeInHunk = false;
106121

107122
foreach ($hunk as [$op, $i1, $i2, $j1, $j2]) {
108123
if ($op === SequenceMatcher::OP_DEL) {
109124
continue;
110125
}
111126

127+
if ($op & self::OP_BLOCK_CHANGED) {
128+
$hasChangeInHunk = true;
129+
}
130+
112131
$ret .= $this->renderContext(self::TAG_MAP[$op], $differ->getNew($j1, $j2));
113132
}
114133

115-
return $ret;
134+
return $hasChangeInHunk ? $ret : '';
116135
}
117136

118137
/**

0 commit comments

Comments
 (0)