Skip to content

Commit 5d094e4

Browse files
authored
feat: add new differ option: fullContextIfIdentical (#79)
1 parent bf2ddb8 commit 5d094e4

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ $differOptions = [
8484
'ignoreWhitespace' => false,
8585
// if the input sequence is too long, it will just gives up (especially for char-level diff)
8686
'lengthLimit' => 2000,
87+
// if truthy, when inputs are identical, the whole inputs will be rendered in the output
88+
'fullContextIfIdentical' => false,
8789
];
8890

8991
// the renderer class options

example/demo_base.php

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
'ignoreWhitespace' => false,
2727
// if the input sequence is too long, it will just gives up (especially for char-level diff)
2828
'lengthLimit' => 2000,
29+
// if truthy, when inputs are identical, the whole inputs will be rendered in the output
30+
'fullContextIfIdentical' => false,
2931
];
3032

3133
// options for renderer class

src/Differ.php

+28-4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ final class Differ
113113
'ignoreWhitespace' => false,
114114
// if the input sequence is too long, it will just gives up (especially for char-level diff)
115115
'lengthLimit' => 2000,
116+
// if truthy, when inputs are identical, the whole inputs will be rendered in the output
117+
'fullContextIfIdentical' => false,
116118
];
117119

118120
/**
@@ -310,10 +312,21 @@ public function getGroupedOpcodes(): array
310312
return $this->groupedOpcodes;
311313
}
312314

313-
$this->getGroupedOpcodesPre($this->old, $this->new);
315+
$old = $this->old;
316+
$new = $this->new;
317+
318+
if ($this->oldNewComparison === 0 && $this->options['fullContextIfIdentical']) {
319+
return [
320+
[
321+
[SequenceMatcher::OP_EQ, 0, \count($old), 0, \count($new)],
322+
],
323+
];
324+
}
325+
326+
$this->getGroupedOpcodesPre($old, $new);
314327

315328
$opcodes = $this->sequenceMatcher
316-
->setSequences($this->old, $this->new)
329+
->setSequences($old, $new)
317330
->getGroupedOpcodes($this->options['context'])
318331
;
319332

@@ -335,10 +348,21 @@ public function getGroupedOpcodesGnu(): array
335348
return $this->groupedOpcodesGnu;
336349
}
337350

338-
$this->getGroupedOpcodesGnuPre($this->old, $this->new);
351+
$old = $this->old;
352+
$new = $this->new;
353+
354+
if ($this->oldNewComparison === 0 && $this->options['fullContextIfIdentical']) {
355+
return [
356+
[
357+
[SequenceMatcher::OP_EQ, 0, \count($old), 0, \count($new)],
358+
],
359+
];
360+
}
361+
362+
$this->getGroupedOpcodesGnuPre($old, $new);
339363

340364
$opcodes = $this->sequenceMatcher
341-
->setSequences($this->old, $this->new)
365+
->setSequences($old, $new)
342366
->getGroupedOpcodes($this->options['context'])
343367
;
344368

src/Renderer/AbstractRenderer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ final public function render(Differ $differ): string
181181
$this->changesAreRaw = true;
182182

183183
// the "no difference" situation may happen frequently
184-
return $differ->getOldNewComparison() === 0
184+
return $differ->getOldNewComparison() === 0 && !$differ->options['fullContextIfIdentical']
185185
? $this->getResultForIdenticals()
186186
: $this->renderWorker($differ);
187187
}

tests/Renderer/RendererTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,36 @@ public function testSetOptionsWithLanguageArray(): void
4747
);
4848
}
4949

50+
/**
51+
* Test the AbstractRenderer::setOptions with result for identicals.
52+
*
53+
* @covers \Jfcherng\Diff\Renderer\AbstractRenderer::setOptions
54+
*/
55+
public function testSetOptionsWithFullContextIfIdentical(): void
56+
{
57+
$diffResult = DiffHelper::calculate(
58+
"the 1st line\nthe 2nd line\nthe 3rd line",
59+
"the 1st line\nthe 2nd line\nthe 3rd line",
60+
'Unified',
61+
['fullContextIfIdentical' => true],
62+
[],
63+
);
64+
65+
self::assertSame(
66+
<<<'DIFF'
67+
@@ -1,3 +1,3 @@
68+
the 1st line
69+
the 2nd line
70+
the 3rd line
71+
\ No newline at end of file
72+
73+
DIFF
74+
,
75+
$diffResult,
76+
'Differ options: "fullContextIfIdentical" should work.',
77+
);
78+
}
79+
5080
/**
5181
* Test the AbstractRenderer::setOptions with result for identicals.
5282
*

0 commit comments

Comments
 (0)