Skip to content

Commit

Permalink
feat: add new differ option: fullContextIfIdentical (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcherng authored Mar 5, 2024
1 parent bf2ddb8 commit 5d094e4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
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
32 changes: 28 additions & 4 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 @@ -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'])
;

Expand All @@ -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'])
;

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 5d094e4

Please sign in to comment.