Skip to content

Commit 239c68f

Browse files
committed
feat: add new differ option: fullContextIfIdentical (#79)
Signed-off-by: Jack Cherng <[email protected]>
1 parent 5050622 commit 239c68f

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
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
@@ -24,6 +24,8 @@
2424
'ignoreWhitespace' => false,
2525
// if the input sequence is too long, it will just gives up (especially for char-level diff)
2626
'lengthLimit' => 2000,
27+
// if truthy, when inputs are identical, the whole inputs will be rendered in the output
28+
'fullContextIfIdentical' => false,
2729
];
2830

2931
// options for renderer class

src/Differ.php

+20
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ final class Differ
119119
'ignoreWhitespace' => false,
120120
// if the input sequence is too long, it will just gives up (especially for char-level diff)
121121
'lengthLimit' => 2000,
122+
// if truthy, when inputs are identical, the whole inputs will be rendered in the output
123+
'fullContextIfIdentical' => false,
122124
];
123125

124126
/**
@@ -318,6 +320,15 @@ public function getGroupedOpcodes(): array
318320

319321
$old = $this->old;
320322
$new = $this->new;
323+
324+
if ($this->oldNewComparison === 0 && $this->options['fullContextIfIdentical']) {
325+
return [
326+
[
327+
[SequenceMatcher::OP_EQ, 0, \count($old), 0, \count($new)],
328+
],
329+
];
330+
}
331+
321332
$this->getGroupedOpcodesPre($old, $new);
322333

323334
$opcodes = $this->sequenceMatcher
@@ -344,6 +355,15 @@ public function getGroupedOpcodesGnu(): array
344355

345356
$old = $this->old;
346357
$new = $this->new;
358+
359+
if ($this->oldNewComparison === 0 && $this->options['fullContextIfIdentical']) {
360+
return [
361+
[
362+
[SequenceMatcher::OP_EQ, 0, \count($old), 0, \count($new)],
363+
],
364+
];
365+
}
366+
347367
$this->getGroupedOpcodesGnuPre($old, $new);
348368

349369
$opcodes = $this->sequenceMatcher

src/Renderer/AbstractRenderer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ final public function render(Differ $differ): string
183183
{
184184
$this->changesAreRaw = true;
185185
// the "no difference" situation may happen frequently
186-
return $differ->getOldNewComparison() === 0
186+
return $differ->getOldNewComparison() === 0 && !$differ->options['fullContextIfIdentical']
187187
? $this->getResultForIdenticals()
188188
: $this->renderWorker($differ);
189189
}

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)