Skip to content

Commit

Permalink
feat: add new differ option: fullContextIfIdentical
Browse files Browse the repository at this point in the history
When it's truthy and there is no difference, the diff output would be
the whole file marked as "equal". Otherwise, it just returns null.

Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Mar 4, 2024
1 parent e0da986 commit 8e29f00
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

/**
Expand Down Expand Up @@ -312,6 +314,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
Expand Down Expand Up @@ -339,6 +350,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
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Renderer/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ 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(
'@@ -1,3 +1,3 @@
the 1st line
the 2nd line
the 3rd line
\ No newline at end of file
',
$diffResult,
'Differ options: "fullContextIfIdentical" should work.',
);
}

/**
* Test the AbstractRenderer::setOptions with result for identicals.
*
Expand Down

0 comments on commit 8e29f00

Please sign in to comment.