From 239c68f5ea2a094b6ab2eeb613999b2eb3d7bb49 Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Tue, 5 Mar 2024 16:30:48 +0800 Subject: [PATCH] feat: add new differ option: fullContextIfIdentical (#79) Signed-off-by: Jack Cherng --- README.md | 2 ++ example/demo_base.php | 2 ++ src/Differ.php | 20 ++++++++++++++++++++ src/Renderer/AbstractRenderer.php | 2 +- tests/Renderer/RendererTest.php | 30 ++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 380c8936..b742a320 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 3873df95..97170d22 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 776d005f..54fddeba 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 992db662..d9e3ec42 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 86af9cf3..e6f4dd6b 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. *