| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jfcherng\Diff\Renderer\Text; |
| 6 |
|
| 7 |
use Jfcherng\Diff\Differ; |
| 8 |
use Jfcherng\Diff\SequenceMatcher; |
| 9 |
|
| 10 |
/** |
| 11 |
* Unified diff generator. |
| 12 |
* |
| 13 |
* @see https://en.wikipedia.org/wiki/Diff#Unified_format |
| 14 |
*/ |
| 15 |
final class Unified extends AbstractText |
| 16 |
{ |
| 17 |
/** |
| 18 |
* {@inheritdoc} |
| 19 |
*/ |
| 20 |
public const INFO = [ |
| 21 |
'desc' => 'Unified', |
| 22 |
'type' => 'Text', |
| 23 |
]; |
| 24 |
|
| 25 |
protected function renderWorker(Differ $differ): string |
| 26 |
{ |
| 27 |
$ret = ''; |
| 28 |
|
| 29 |
foreach ($differ->getGroupedOpcodesGnu() as $hunk) { |
| 30 |
$ret .= $this->renderHunkHeader($differ, $hunk); |
| 31 |
$ret .= $this->renderHunkBlocks($differ, $hunk); |
| 32 |
} |
| 33 |
|
| 34 |
return $ret; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Render the hunk header. |
| 39 |
* |
| 40 |
* @param Differ $differ the differ |
| 41 |
* @param int[][] $hunk the hunk |
| 42 |
*/ |
| 43 |
protected function renderHunkHeader(Differ $differ, array $hunk): string |
| 44 |
{ |
| 45 |
$lastBlockIdx = \count($hunk) - 1; |
| 46 |
|
| 47 |
// note that these line number variables are 0-based |
| 48 |
$i1 = $hunk[0][1]; |
| 49 |
$i2 = $hunk[$lastBlockIdx][2]; |
| 50 |
$j1 = $hunk[0][3]; |
| 51 |
$j2 = $hunk[$lastBlockIdx][4]; |
| 52 |
|
| 53 |
$oldLinesCount = $i2 - $i1; |
| 54 |
$newLinesCount = $j2 - $j1; |
| 55 |
|
| 56 |
return $this->cliColoredString( |
| 57 |
'@@' . |
| 58 |
' -' . |
| 59 |
// the line number in GNU diff is 1-based, so we add 1 |
| 60 |
// a special case is when a hunk has only changed blocks, |
| 61 |
// i.e., context is set to 0, we do not need the adding |
| 62 |
($i1 === $i2 ? $i1 : $i1 + 1) . |
| 63 |
// if the line counts is 1, it can (and mostly) be omitted |
| 64 |
($oldLinesCount === 1 ? '' : ",{$oldLinesCount}") . |
| 65 |
' +' . |
| 66 |
($j1 === $j2 ? $j1 : $j1 + 1) . |
| 67 |
($newLinesCount === 1 ? '' : ",{$newLinesCount}") . |
| 68 |
" @@\n", |
| 69 |
'@', // symbol |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Render the hunk content. |
| 75 |
* |
| 76 |
* @param Differ $differ the differ |
| 77 |
* @param int[][] $hunk the hunk |
| 78 |
*/ |
| 79 |
protected function renderHunkBlocks(Differ $differ, array $hunk): string |
| 80 |
{ |
| 81 |
$ret = ''; |
| 82 |
|
| 83 |
$oldNoEolAtEofIdx = $differ->getOldNoEolAtEofIdx(); |
| 84 |
$newNoEolAtEofIdx = $differ->getNewNoEolAtEofIdx(); |
| 85 |
|
| 86 |
foreach ($hunk as [$op, $i1, $i2, $j1, $j2]) { |
| 87 |
// note that although we are in a OP_EQ situation, |
| 88 |
// the old and the new may not be exactly the same |
| 89 |
// because of ignoreCase, ignoreWhitespace, etc |
| 90 |
if ($op === SequenceMatcher::OP_EQ) { |
| 91 |
// we could only pick either the old or the new to show |
| 92 |
// note that the GNU diff will use the old one because it creates a patch |
| 93 |
$ret .= $this->renderContext( |
| 94 |
' ', |
| 95 |
$differ->getOld($i1, $i2), |
| 96 |
$i2 === $oldNoEolAtEofIdx, |
| 97 |
); |
| 98 |
|
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if ($op & (SequenceMatcher::OP_REP | SequenceMatcher::OP_DEL)) { |
| 103 |
$ret .= $this->renderContext( |
| 104 |
'-', |
| 105 |
$differ->getOld($i1, $i2), |
| 106 |
$i2 === $oldNoEolAtEofIdx, |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
if ($op & (SequenceMatcher::OP_REP | SequenceMatcher::OP_INS)) { |
| 111 |
$ret .= $this->renderContext( |
| 112 |
'+', |
| 113 |
$differ->getNew($j1, $j2), |
| 114 |
$j2 === $newNoEolAtEofIdx, |
| 115 |
); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return $ret; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Render the context array with the symbol. |
| 124 |
* |
| 125 |
* @param string $symbol the symbol |
| 126 |
* @param string[] $context the context |
| 127 |
* @param bool $noEolAtEof there is no EOL at EOF in this block |
| 128 |
*/ |
| 129 |
protected function renderContext(string $symbol, array $context, bool $noEolAtEof = false): string |
| 130 |
{ |
| 131 |
if (empty($context)) { |
| 132 |
return ''; |
| 133 |
} |
| 134 |
|
| 135 |
$ret = $symbol . implode("\n{$symbol}", $context) . "\n"; |
| 136 |
$ret = $this->cliColoredString($ret, $symbol); |
| 137 |
|
| 138 |
if ($noEolAtEof) { |
| 139 |
$ret .= self::GNU_OUTPUT_NO_EOL_AT_EOF . "\n"; |
| 140 |
} |
| 141 |
|
| 142 |
return $ret; |
| 143 |
} |
| 144 |
} |