Skip to content

Commit

Permalink
feat: add new differ option: fullContextIfIdentical (#79)
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Mar 5, 2024
1 parent 5050622 commit 239c68f
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 @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

/**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
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 239c68f

Please sign in to comment.