Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new differ option: fullContextIfIdentical #79

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading