Skip to content

Commit 2dc8344

Browse files
authored
Merge pull request #111 from JBlond/php-diff-109
Keep "Out Of Context" lines in opCodes
2 parents e74c993 + 40f418f commit 2dc8344

17 files changed

+125
-114
lines changed

lib/jblond/Diff/ConstantsInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface ConstantsInterface
2727
*/
2828
public const DIFF_IGNORE_LINE_EMPTY = 1;
2929
/**
30-
* Flag to ignore blank lines. (Lines which contain no or only non printable characters.)
30+
* Flag to ignore blank lines. (Lines which contain no or only non-printable characters.)
3131
*/
3232
public const DIFF_IGNORE_LINE_BLANK = 2;
3333
}

lib/jblond/Diff/Renderer/Html/Merged.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public function generateBlockHeader(array $changes): string
9797
/**
9898
* @inheritDoc
9999
*
100-
* @return string Representation of skipped lines.
100+
* @return string HTML code representing table rows showing text which is 'Out Of Context'
101101
*/
102-
public function generateSkippedLines(): string
102+
public function generateLinesOutOfContext($change): string
103103
{
104104
$marker = '…';
105105
$headerClass = '';

lib/jblond/Diff/Renderer/Html/SideBySide.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public function generateDiffHeader(): string
8686
/**
8787
* @inheritDoc
8888
*
89-
* @return string HTML code representation of a table's header.
89+
* @return string HTML code representing table rows showing text which is "Out Of Context"
9090
*/
91-
public function generateSkippedLines(): string
91+
public function generateLinesOutOfContext($change): string
9292
{
9393
return <<<HTML
9494
<tr>

lib/jblond/Diff/Renderer/Html/Unified.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ public function generateDiffHeader(): string
8787
/**
8888
* @inheritDoc
8989
*
90-
* @return string HTML code representation of skipped lines.
90+
* @return string HTML code representing table rows showing text which is 'Out Of Context'
9191
*/
92-
public function generateSkippedLines(): string
92+
public function generateLinesOutOfContext($change): string
9393
{
9494
return <<<HTML
9595
<tr>
@@ -222,7 +222,7 @@ public function generateLinesReplace(array $changes): string
222222
/**
223223
* @inheritDoc
224224
*
225-
* @return string Html code representing table rows showing modified text.
225+
* @return string Html code representing table rows showing ignored text.
226226
*/
227227
public function generateLinesIgnore(array $changes): string
228228
{

lib/jblond/Diff/Renderer/MainRenderer.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ class MainRenderer extends MainRendererAbstract
3737
*
3838
* This method is called by the renderers which extends this class.
3939
*
40-
* @param array $changes Contains the op-codes about the differences between version1 and version2.
41-
* @param object $subRenderer Renderer which is subClass of this class.
40+
* @param array $changes Contains the op-codes about the differences between version1 and
41+
* version2.
42+
* @param SubRendererInterface $subRenderer Renderer which is child class of this class.
4243
*
4344
* @return string|false String representation of the differences or false when versions are identical.
4445
*/
45-
public function renderOutput(array $changes, object $subRenderer)
46+
public function renderOutput(array $changes, SubRendererInterface $subRenderer)
4647
{
4748
if (!$changes) {
4849
//No changes between version1 and version2
@@ -51,13 +52,7 @@ public function renderOutput(array $changes, object $subRenderer)
5152

5253
$output = $subRenderer->generateDiffHeader();
5354

54-
foreach ($changes as $iterator => $blocks) {
55-
if ($iterator > 0) {
56-
// If this is a separate block, we're condensing code to indicate a significant portion of the code
57-
// has been collapsed as it did not change.
58-
$output .= $subRenderer->generateSkippedLines();
59-
}
60-
55+
foreach ($changes as $blocks) {
6156
$this->maxLineMarkerWidth = max(
6257
strlen($this->options['insertMarkers'][0]),
6358
strlen($this->options['deleteMarkers'][0]),
@@ -100,6 +95,10 @@ public function renderOutput(array $changes, object $subRenderer)
10095
// TODO: Keep backward compatible with renderers?
10196
$output .= $subRenderer->generateLinesIgnore($change);
10297
break;
98+
case 'outOfContext':
99+
// TODO: Keep backward compatible with renderers?
100+
$output .= $subRenderer->generateLinesOutOfContext($change);
101+
break;
103102
}
104103

105104
$output .= $subRenderer->generateBlockFooter($change);
@@ -335,17 +334,15 @@ private function markOuterChange(array &$oldText, array &$newText, int $startOld
335334
// Changes between the lines exist.
336335
// Add markers around the changed character sequence in the old string.
337336
$sequenceEnd = mb_strlen($oldString) + $end;
338-
$oldString
339-
= mb_substr($oldString, 0, $start) . "\0" .
340-
mb_substr($oldString, $start, $sequenceEnd - $start) . "\1" .
341-
mb_substr($oldString, $sequenceEnd);
337+
$oldString = mb_substr($oldString, 0, $start) . "\0" .
338+
mb_substr($oldString, $start, $sequenceEnd - $start) . "\1" .
339+
mb_substr($oldString, $sequenceEnd);
342340

343341
// Add markers around the changed character sequence in the new string.
344342
$sequenceEnd = mb_strlen($newString) + $end;
345-
$newString
346-
= mb_substr($newString, 0, $start) . "\0" .
347-
mb_substr($newString, $start, $sequenceEnd - $start) . "\1" .
348-
mb_substr($newString, $sequenceEnd);
343+
$newString = mb_substr($newString, 0, $start) . "\0" .
344+
mb_substr($newString, $start, $sequenceEnd - $start) . "\1" .
345+
mb_substr($newString, $sequenceEnd);
349346

350347
// Overwrite the strings in the old and new text so the changed lines include the markers.
351348
$oldText[$startOld + $iterator] = $oldString;
@@ -397,7 +394,7 @@ private function getOuterChange(string $oldString, string $newString): array
397394

398395
/**
399396
* Helper function that will fill the changes-array for the renderer with default values.
400-
* Every time an operation changes (specified by $tag) , a new element will be appended to this array.
397+
* Every time an operation changes (specified by $tag), a new element will be appended to this array.
401398
*
402399
* The index of the last element of the array is always returned.
403400
*
@@ -432,7 +429,7 @@ private function appendChangesArray(array &$blocks, string $tag, int $lineInOld,
432429
}
433430

434431
/**
435-
* Format a series of strings which are suitable for output in a HTML rendered diff.
432+
* Format a series of strings which are suitable for output in an HTML rendered diff.
436433
*
437434
* This involves replacing tab characters with spaces, making the HTML safe for output by ensuring that double
438435
* spaces are replaced with &nbsp; etc.

lib/jblond/Diff/Renderer/SubRendererInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public function generateLinesEqual(array $changes): string;
6363
public function generateLinesInsert(array $changes): string;
6464

6565
/**
66-
* Generate a string representation of lines that are skipped in the diff view.
66+
* Generate a string representation of lines that are "Out Of Context" for the diff view.
6767
*
68-
* @return string Representation of skipped lines.
68+
* @return string Representation of 'Out Of Context' lines.
6969
*/
70-
public function generateSkippedLines(): string;
70+
public function generateLinesOutOfContext($change): string;
7171

7272
/**
7373
* Generate a string representation of lines with ignored differences between both versions.

lib/jblond/Diff/Renderer/Text/Context.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ public function render()
4444
$diff = false;
4545
$opCodes = $this->diff->getGroupedOpCodes();
4646

47-
foreach ($opCodes as $group) {
47+
foreach ($opCodes as $key => $group) {
48+
if ($key % 2) {
49+
// Skip lines which are Out Of Context.
50+
continue;
51+
}
4852
$diff .= "***************\n";
49-
$lastItem = count($group) - 1;
50-
$start1 = $group['0']['1'];
51-
$end1 = $group[$lastItem]['2'];
52-
$start2 = $group['0']['3'];
53-
$end2 = $group[$lastItem]['4'];
53+
$lastItem = array_key_last($group);
54+
$start1 = $group[0][1];
55+
$end1 = $group[$lastItem][2];
56+
$start2 = $group[0][3];
57+
$end2 = $group[$lastItem][4];
5458

5559
// Line to line header for version 1.
5660
$diffStart = $end1 - $start1 >= 2 ? $start1 + 1 . ',' : '';

lib/jblond/Diff/Renderer/Text/InlineCli.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public function generateBlockHeader(array $changes): string
7777
/**
7878
* @inheritDoc
7979
*
80-
* @return string Representation of skipped lines.
80+
* @return string HTML code representing table rows showing text which is 'Out Of Context'
8181
*/
82-
public function generateSkippedLines(): string
82+
public function generateLinesOutOfContext($change): string
8383
{
8484
return "...\n";
8585
}

lib/jblond/Diff/Renderer/Text/Unified.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ public function render()
3434
{
3535
$diff = false;
3636
$opCodes = $this->diff->getGroupedOpCodes();
37-
foreach ($opCodes as $group) {
38-
$lastItem = count($group) - 1;
39-
$i1 = $group['0']['1'];
40-
$i2 = $group[$lastItem]['2'];
41-
$j1 = $group['0']['3'];
42-
$j2 = $group[$lastItem]['4'];
37+
foreach ($opCodes as $key => $group) {
38+
if ($key % 2) {
39+
// Skip lines which are Out Of Context.
40+
continue;
41+
}
42+
$lastItem = array_key_last($group);
43+
$i1 = $group[0][1];
44+
$i2 = $group[$lastItem][2];
45+
$j1 = $group[0][3];
46+
$j2 = $group[$lastItem][4];
4347

4448
if ($i1 == 0 && $i2 == 0) {
4549
$i1 = -1;

lib/jblond/Diff/Renderer/Text/UnifiedCli.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,16 @@ private function output(): string
6666
{
6767
$diff = '';
6868
$opCodes = $this->diff->getGroupedOpCodes();
69-
foreach ($opCodes as $group) {
70-
$lastItem = count($group) - 1;
71-
$i1 = $group['0']['1'];
72-
$i2 = $group[$lastItem]['2'];
73-
$j1 = $group['0']['3'];
74-
$j2 = $group[$lastItem]['4'];
69+
foreach ($opCodes as $key => $group) {
70+
if ($key % 2) {
71+
// Skip lines which are Out Of Context.
72+
continue;
73+
}
74+
$lastItem = array_key_last($group);
75+
$i1 = $group[0][1];
76+
$i2 = $group[$lastItem][2];
77+
$j1 = $group[0][3];
78+
$j2 = $group[$lastItem][4];
7579

7680
if ($i1 == 0 && $i2 == 0) {
7781
$i1 = -1;
@@ -82,6 +86,7 @@ private function output(): string
8286
'@@ -' . ($i1 + 1) . ',' . ($i2 - $i1) . ' +' . ($j1 + 1) . ',' . ($j2 - $j1) . " @@\n",
8387
'purple'
8488
);
89+
8590
foreach ($group as [$tag, $i1, $i2, $j1, $j2]) {
8691
if ($tag == 'equal') {
8792
$string = implode(
@@ -112,8 +117,8 @@ private function output(): string
112117
}
113118

114119
/**
115-
* @param string $string
116-
* @param string $color
120+
* @param string $string
121+
* @param string $color
117122
*
118123
* @return string
119124
*/

0 commit comments

Comments
 (0)