diff --git a/README.md b/README.md index ecdf675..b328c48 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 00b463a..c108221 100644 --- a/example/demo_base.php +++ b/example/demo_base.php @@ -26,6 +26,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 162c689..7130fbf 100644 --- a/src/Differ.php +++ b/src/Differ.php @@ -113,6 +113,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, ]; /** @@ -310,10 +312,21 @@ public function getGroupedOpcodes(): array return $this->groupedOpcodes; } - $this->getGroupedOpcodesPre($this->old, $this->new); + $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 - ->setSequences($this->old, $this->new) + ->setSequences($old, $new) ->getGroupedOpcodes($this->options['context']) ; @@ -335,10 +348,21 @@ public function getGroupedOpcodesGnu(): array return $this->groupedOpcodesGnu; } - $this->getGroupedOpcodesGnuPre($this->old, $this->new); + $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 - ->setSequences($this->old, $this->new) + ->setSequences($old, $new) ->getGroupedOpcodes($this->options['context']) ; diff --git a/src/Renderer/AbstractRenderer.php b/src/Renderer/AbstractRenderer.php index 47a5c52..2d7d55c 100644 --- a/src/Renderer/AbstractRenderer.php +++ b/src/Renderer/AbstractRenderer.php @@ -181,7 +181,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 94ada7e..daa3977 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. *