Debian Codegraph @

vendor/jfcherng/php-diff/src/Renderer/Text/Context.php

raw
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 * Context diff generator.
12 *
13 * @see https://en.wikipedia.org/wiki/Diff#Context_format
14 */
15 final class Context extends AbstractText
16 {
17 /**
18 * {@inheritdoc}
19 */
20 public const INFO = [
21 'desc' => 'Context',
22 'type' => 'Text',
23 ];
24
25 /**
26 * @var int the union of OPs that indicate there is a change
27 */
28 public const OP_BLOCK_CHANGED =
29 SequenceMatcher::OP_DEL |
30 SequenceMatcher::OP_INS |
31 SequenceMatcher::OP_REP;
32
33 protected function renderWorker(Differ $differ): string
34 {
35 $ret = '';
36
37 foreach ($differ->getGroupedOpcodesGnu() as $hunk) {
38 $lastBlockIdx = \count($hunk) - 1;
39
40 // note that these line number variables are 0-based
41 $i1 = $hunk[0][1];
42 $i2 = $hunk[$lastBlockIdx][2];
43 $j1 = $hunk[0][3];
44 $j2 = $hunk[$lastBlockIdx][4];
45
46 $ret .=
47 $this->cliColoredString("***************\n", '@') .
48 $this->renderHunkHeader('*', $i1, $i2) .
49 $this->renderHunkOld($differ, $hunk) .
50 $this->renderHunkHeader('-', $j1, $j2) .
51 $this->renderHunkNew($differ, $hunk);
52 }
53
54 return $ret;
55 }
56
57 /**
58 * Render the hunk header.
59 *
60 * @param string $symbol the symbol
61 * @param int $a1 the begin index
62 * @param int $a2 the end index
63 */
64 protected function renderHunkHeader(string $symbol, int $a1, int $a2): string
65 {
66 $a1x = $a1 + 1; // 1-based begin line number
67
68 return $this->cliColoredString(
69 "{$symbol}{$symbol}{$symbol} " .
70 ($a1x < $a2 ? "{$a1x},{$a2}" : $a2) .
71 " {$symbol}{$symbol}{$symbol}{$symbol}\n",
72 '@', // symbol
73 );
74 }
75
76 /**
77 * Render the old hunk.
78 *
79 * @param Differ $differ the differ object
80 * @param int[][] $hunk the hunk
81 */
82 protected function renderHunkOld(Differ $differ, array $hunk): string
83 {
84 $ret = '';
85 $hunkOps = 0;
86 $noEolAtEofIdx = $differ->getOldNoEolAtEofIdx();
87
88 foreach ($hunk as [$op, $i1, $i2, $j1, $j2]) {
89 // OP_INS does not belongs to an old hunk
90 if ($op === SequenceMatcher::OP_INS) {
91 continue;
92 }
93
94 $hunkOps |= $op;
95
96 $ret .= $this->renderContext(
97 self::SYMBOL_MAP[$op],
98 $differ->getOld($i1, $i2),
99 $i2 === $noEolAtEofIdx,
100 );
101 }
102
103 // if there is no content changed, the hunk context should be omitted
104 return $hunkOps & self::OP_BLOCK_CHANGED ? $ret : '';
105 }
106
107 /**
108 * Render the new hunk.
109 *
110 * @param Differ $differ the differ object
111 * @param int[][] $hunk the hunk
112 */
113 protected function renderHunkNew(Differ $differ, array $hunk): string
114 {
115 $ret = '';
116 $hunkOps = 0;
117 $noEolAtEofIdx = $differ->getNewNoEolAtEofIdx();
118
119 foreach ($hunk as [$op, $i1, $i2, $j1, $j2]) {
120 // OP_DEL does not belongs to a new hunk
121 if ($op === SequenceMatcher::OP_DEL) {
122 continue;
123 }
124
125 $hunkOps |= $op;
126
127 $ret .= $this->renderContext(
128 self::SYMBOL_MAP[$op],
129 $differ->getNew($j1, $j2),
130 $j2 === $noEolAtEofIdx,
131 );
132 }
133
134 // if there is no content changed, the hunk context should be omitted
135 return $hunkOps & self::OP_BLOCK_CHANGED ? $ret : '';
136 }
137
138 /**
139 * Render the context array with the symbol.
140 *
141 * @param string $symbol the symbol
142 * @param string[] $context the context
143 * @param bool $noEolAtEof there is no EOL at EOF in this block
144 */
145 protected function renderContext(string $symbol, array $context, bool $noEolAtEof = false): string
146 {
147 if (empty($context)) {
148 return '';
149 }
150
151 $ret = "{$symbol} " . implode("\n{$symbol} ", $context) . "\n";
152 $ret = $this->cliColoredString($ret, $symbol);
153
154 if ($noEolAtEof) {
155 $ret .= self::GNU_OUTPUT_NO_EOL_AT_EOF . "\n";
156 }
157
158 return $ret;
159 }
160 }