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 314e67a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions example/demo_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
30 changes: 30 additions & 0 deletions tests/Renderer/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 314e67a

Please sign in to comment.