diff --git a/README.md b/README.md index 380c893..b742a32 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,8 @@ $differOptions = [ 'ignoreWhitespace' => false, // if the input sequence is too long, it will just gives up (especially for char-level diff) 'lengthLimit' => 2000, + // if truthy, when inputs are identical, the whole inputs will be rendered in the output + 'fullContextIfIdentical' => false, ]; // the renderer class options diff --git a/example/demo_base.php b/example/demo_base.php index 3873df9..97170d2 100644 --- a/example/demo_base.php +++ b/example/demo_base.php @@ -24,6 +24,8 @@ 'ignoreWhitespace' => false, // if the input sequence is too long, it will just gives up (especially for char-level diff) 'lengthLimit' => 2000, + // if truthy, when inputs are identical, the whole inputs will be rendered in the output + 'fullContextIfIdentical' => false, ]; // options for renderer class diff --git a/src/Differ.php b/src/Differ.php index 776d005..54fddeb 100644 --- a/src/Differ.php +++ b/src/Differ.php @@ -119,6 +119,8 @@ final class Differ 'ignoreWhitespace' => false, // if the input sequence is too long, it will just gives up (especially for char-level diff) 'lengthLimit' => 2000, + // if truthy, when inputs are identical, the whole inputs will be rendered in the output + 'fullContextIfIdentical' => false, ]; /** @@ -318,6 +320,15 @@ public function getGroupedOpcodes(): array $old = $this->old; $new = $this->new; + + if ($this->oldNewComparison === 0 && $this->options['fullContextIfIdentical']) { + return [ + [ + [SequenceMatcher::OP_EQ, 0, \count($old), 0, \count($new)], + ], + ]; + } + $this->getGroupedOpcodesPre($old, $new); $opcodes = $this->sequenceMatcher @@ -344,6 +355,15 @@ public function getGroupedOpcodesGnu(): array $old = $this->old; $new = $this->new; + + if ($this->oldNewComparison === 0 && $this->options['fullContextIfIdentical']) { + return [ + [ + [SequenceMatcher::OP_EQ, 0, \count($old), 0, \count($new)], + ], + ]; + } + $this->getGroupedOpcodesGnuPre($old, $new); $opcodes = $this->sequenceMatcher diff --git a/src/Renderer/AbstractRenderer.php b/src/Renderer/AbstractRenderer.php index 992db66..d9e3ec4 100644 --- a/src/Renderer/AbstractRenderer.php +++ b/src/Renderer/AbstractRenderer.php @@ -183,7 +183,7 @@ final public function render(Differ $differ): string { $this->changesAreRaw = true; // the "no difference" situation may happen frequently - return $differ->getOldNewComparison() === 0 + return $differ->getOldNewComparison() === 0 && !$differ->options['fullContextIfIdentical'] ? $this->getResultForIdenticals() : $this->renderWorker($differ); } diff --git a/tests/Renderer/RendererTest.php b/tests/Renderer/RendererTest.php index 86af9cf..e6f4dd6 100644 --- a/tests/Renderer/RendererTest.php +++ b/tests/Renderer/RendererTest.php @@ -47,6 +47,36 @@ public function testSetOptionsWithLanguageArray(): void ); } + /** + * Test the AbstractRenderer::setOptions with result for identicals. + * + * @covers \Jfcherng\Diff\Renderer\AbstractRenderer::setOptions + */ + public function testSetOptionsWithFullContextIfIdentical(): void + { + $diffResult = DiffHelper::calculate( + "the 1st line\nthe 2nd line\nthe 3rd line", + "the 1st line\nthe 2nd line\nthe 3rd line", + 'Unified', + ['fullContextIfIdentical' => true], + [] + ); + + self::assertSame( + <<<'DIFF' +@@ -1,3 +1,3 @@ + the 1st line + the 2nd line + the 3rd line +\ No newline at end of file + +DIFF + , + $diffResult, + 'Differ options: "fullContextIfIdentical" should work.' + ); + } + /** * Test the AbstractRenderer::setOptions with result for identicals. *