Skip to content

Commit

Permalink
Add generator for ignored lines
Browse files Browse the repository at this point in the history
  • Loading branch information
DigiLive authored and DigiLive committed Dec 18, 2020
1 parent 251ccf0 commit 4dec4ad
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 31 deletions.
17 changes: 17 additions & 0 deletions example/dark-theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ a, a:visited {
color: #272822;
}

.DifferencesUnified .ChangeIgnore .Left,
.DifferencesUnified .ChangeIgnore .Right {
background: #FBF2BF;
}

.DifferencesUnified .ChangeIgnore .Left.Ignore {
background: #4B4C57;
}

.DifferencesUnified .ChangeIgnore .Right.Ignore {
background: #4B4C57;
}

/*
* HTML Merged Diff
*/
Expand Down Expand Up @@ -166,3 +179,7 @@ a, a:visited {
.DifferencesMerged th.ChangeDelete {
background-image: linear-gradient(-45deg, #AAAAAA 0%, #EE9999 100%);
}

.DifferencesMerged th.ChangeIgnore {
background-image: linear-gradient(-45deg, #CCCCCC 0%, #4B4C57 100%);
}
17 changes: 17 additions & 0 deletions example/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ pre {
background: #EE9999;
}

.DifferencesUnified .ChangeIgnore .Left,
.DifferencesUnified .ChangeIgnore .Right {
background: #FBF2BF;
}

.DifferencesUnified .ChangeIgnore .Left.Ignore {
background: #F7F7F7;
}

.DifferencesUnified .ChangeIgnore .Right.Ignore {
background: #F7F7F7;
}

/*
* HTML Merged Diff
*/
Expand All @@ -145,3 +158,7 @@ pre {
.DifferencesMerged th.ChangeDelete {
background-image: linear-gradient(-45deg, #CCCCCC 0%, #EE9999 100%);
}

.DifferencesMerged th.ChangeIgnore {
background-image: linear-gradient(-45deg, #CCCCCC 0%, #F7F7F7 100%);
}
82 changes: 51 additions & 31 deletions lib/jblond/Diff/Renderer/Html/Merged.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class Merged extends MainRenderer implements SubRendererInterface
* @var string last block of lines which where removed from version 2.
*/
private $lastDeleted;
/**
* @var string
*/
private $headerClass = '';

/**
* Merged constructor.
Expand Down Expand Up @@ -101,21 +105,17 @@ public function generateBlockHeader(array $changes): string
*/
public function generateSkippedLines(): string
{
$marker = '…';
$headerClass = '';

if ($this->lastDeleted !== null) {
$headerClass = 'ChangeDelete';
}

$this->lastDeleted = null;

return <<<HTML
$html = <<<HTML
<tr>
<th class="$headerClass" title="{$this->lastDeleted}">$marker</th>
<th class="{$this->headerClass}" title="{$this->lastDeleted}">&hellip;</th>
<td class="Skipped">&hellip;</td>
</tr>
HTML;

$this->headerClass = '';
$this->lastDeleted = null;

return $html;
}

/**
Expand All @@ -125,22 +125,20 @@ public function generateSkippedLines(): string
*/
public function generateLinesEqual(array $changes): string
{
$html = '';
$headerClass = '';
$html = '';

foreach ($changes['base']['lines'] as $lineNo => $line) {
$fromLine = $changes['base']['offset'] + $lineNo + 1 + $this->lineOffset;
if (!$lineNo && $this->lastDeleted !== null) {
$headerClass = 'ChangeDelete';
}

$html .= <<<HTML
$html .= <<<HTML
<tr>
<th class="$headerClass" title="{$this->lastDeleted}">$fromLine</th>
<th class="{$this->headerClass}" title="{$this->lastDeleted}">$fromLine</th>
<td>$line</td>
</tr>
HTML;

$this->lastDeleted = null;
$this->headerClass = '';
}

return $html;
Expand All @@ -153,22 +151,20 @@ public function generateLinesEqual(array $changes): string
*/
public function generateLinesInsert(array $changes): string
{
$html = '';
$headerClass = '';
$html = '';

foreach ($changes['changed']['lines'] as $lineNo => $line) {
foreach ($changes['changed']['lines'] as $line) {
$this->lineOffset++;
$toLine = $changes['base']['offset'] + $this->lineOffset;
if (!$lineNo && $this->lastDeleted !== null) {
$headerClass = 'ChangeDelete';
}

$html .= <<<HTML
<tr>
<th class="$headerClass" title="{$this->lastDeleted}">$toLine</th>
<th class="{$this->headerClass}" title="{$this->lastDeleted}">$toLine</th>
<td><ins>$line</ins></td>
</tr>
HTML;

$this->headerClass = '';
$this->lastDeleted = null;
}

Expand Down Expand Up @@ -197,6 +193,7 @@ public function generateLinesDelete(array $changes): string
}

$this->lastDeleted = $title;
$this->headerClass = 'ChangeDelete';

return '';
}
Expand All @@ -208,14 +205,10 @@ public function generateLinesDelete(array $changes): string
*/
public function generateLinesReplace(array $changes): string
{
$html = '';
$headerClass = '';
$html = '';

foreach ($changes['base']['lines'] as $lineNo => $line) {
$fromLine = $changes['base']['offset'] + $lineNo + 1 + $this->lineOffset;
if (!$lineNo && $this->lastDeleted !== null) {
$headerClass = 'ChangeDelete';
}

// Capture added parts.
$addedParts = [];
Expand All @@ -236,10 +229,11 @@ function ($removedParts) use ($addedParts) {

$html .= <<<HTML
<tr>
<th class="$headerClass" title="{$this->lastDeleted}">$fromLine</th>
<th class="{$this->headerClass}" title="{$this->lastDeleted}">$fromLine</th>
<td>$line</td>
</tr>
HTML;
$this->headerClass = '';
$this->lastDeleted = null;
}

Expand All @@ -265,4 +259,30 @@ public function generateDiffFooter(): string
{
return '</table>';
}

/**
* @inheritDoc
*
* @return string Modified text.
*/
public function generateLinesIgnore(array $changes): string
{
$baseLineCount = count($changes['base']['lines']);
$changedLineCount = count($changes['changed']['lines']);

$this->lineOffset -= $baseLineCount;

$title = "Lines ignored at {$this->options['title2']}: ";
$title .= $changes['changed']['offset'] + 1 . '-' . ($changes['changed']['offset'] + $changedLineCount);

if ($baseLineCount > $changedLineCount) {
$title = "Lines ignored at {$this->options['title1']}: ";
$title .= $changes['base']['offset'] + 1 . '-' . ($changes['base']['offset'] + $baseLineCount);
}

$this->lastDeleted = $title;
$this->headerClass = 'ChangeIgnore';

return '';
}
}
38 changes: 38 additions & 0 deletions lib/jblond/Diff/Renderer/Html/Unified.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,44 @@ public function generateLinesReplace(array $changes): string
return $html;
}

/**
* @inheritDoc
*
* @return string Html code representing table rows showing modified text.
*/
public function generateLinesIgnore(array $changes): string
{
$html = '';

foreach ($changes['base']['lines'] as $lineNo => $line) {
$fromLine = $changes['base']['offset'] + $lineNo + 1;
$html .= <<<HTML
<tr>
<th>$fromLine</th>
<th></th>
<td class="Left Ignore">
<span>$line</span>
</td>
</tr>
HTML;
}

foreach ($changes['changed']['lines'] as $lineNo => $line) {
$toLine = $changes['changed']['offset'] + $lineNo + 1;
$html .= <<<HTML
<tr>
<th></th>
<th>$toLine</th>
<td class="Right Ignore">
<span>$line</span>
</td>
</tr>
HTML;
}

return $html;
}

/**
* @inheritDoc
*
Expand Down
2 changes: 2 additions & 0 deletions lib/jblond/Diff/Renderer/Text/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function render()
// Line differences between versions or lines of version 1 are removed from version 2.
// Add all operations to diff-view of version 1, except for insert.
$filteredGroups = $this->filterGroups($group, 'insert');
$filteredGroups = $this->filterGroups($filteredGroups, 'ignore');
foreach ($filteredGroups as [$tag, $start1, $end1, $start2, $end2]) {
$diff .= $this->tagMap[$tag] . ' ' .
implode(
Expand All @@ -81,6 +82,7 @@ public function render()
// Line differences between versions or lines are inserted into version 2.
// Add all operations to diff-view of version 2, except for delete.
$filteredGroups = $this->filterGroups($group, 'delete');
$filteredGroups = $this->filterGroups($filteredGroups, 'ignore');
foreach ($filteredGroups as [$tag, $start1, $end1, $start2, $end2]) {
$diff .= $this->tagMap[$tag] . ' ' .
implode(
Expand Down

0 comments on commit 4dec4ad

Please sign in to comment.