Skip to content

Commit 4dec4ad

Browse files
committed
Add generator for ignored lines
1 parent 251ccf0 commit 4dec4ad

File tree

5 files changed

+125
-31
lines changed

5 files changed

+125
-31
lines changed

example/dark-theme.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ a, a:visited {
138138
color: #272822;
139139
}
140140

141+
.DifferencesUnified .ChangeIgnore .Left,
142+
.DifferencesUnified .ChangeIgnore .Right {
143+
background: #FBF2BF;
144+
}
145+
146+
.DifferencesUnified .ChangeIgnore .Left.Ignore {
147+
background: #4B4C57;
148+
}
149+
150+
.DifferencesUnified .ChangeIgnore .Right.Ignore {
151+
background: #4B4C57;
152+
}
153+
141154
/*
142155
* HTML Merged Diff
143156
*/
@@ -166,3 +179,7 @@ a, a:visited {
166179
.DifferencesMerged th.ChangeDelete {
167180
background-image: linear-gradient(-45deg, #AAAAAA 0%, #EE9999 100%);
168181
}
182+
183+
.DifferencesMerged th.ChangeIgnore {
184+
background-image: linear-gradient(-45deg, #CCCCCC 0%, #4B4C57 100%);
185+
}

example/styles.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ pre {
121121
background: #EE9999;
122122
}
123123

124+
.DifferencesUnified .ChangeIgnore .Left,
125+
.DifferencesUnified .ChangeIgnore .Right {
126+
background: #FBF2BF;
127+
}
128+
129+
.DifferencesUnified .ChangeIgnore .Left.Ignore {
130+
background: #F7F7F7;
131+
}
132+
133+
.DifferencesUnified .ChangeIgnore .Right.Ignore {
134+
background: #F7F7F7;
135+
}
136+
124137
/*
125138
* HTML Merged Diff
126139
*/
@@ -145,3 +158,7 @@ pre {
145158
.DifferencesMerged th.ChangeDelete {
146159
background-image: linear-gradient(-45deg, #CCCCCC 0%, #EE9999 100%);
147160
}
161+
162+
.DifferencesMerged th.ChangeIgnore {
163+
background-image: linear-gradient(-45deg, #CCCCCC 0%, #F7F7F7 100%);
164+
}

lib/jblond/Diff/Renderer/Html/Merged.php

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class Merged extends MainRenderer implements SubRendererInterface
4343
* @var string last block of lines which where removed from version 2.
4444
*/
4545
private $lastDeleted;
46+
/**
47+
* @var string
48+
*/
49+
private $headerClass = '';
4650

4751
/**
4852
* Merged constructor.
@@ -101,21 +105,17 @@ public function generateBlockHeader(array $changes): string
101105
*/
102106
public function generateSkippedLines(): string
103107
{
104-
$marker = '…';
105-
$headerClass = '';
106-
107-
if ($this->lastDeleted !== null) {
108-
$headerClass = 'ChangeDelete';
109-
}
110-
111-
$this->lastDeleted = null;
112-
113-
return <<<HTML
108+
$html = <<<HTML
114109
<tr>
115-
<th class="$headerClass" title="{$this->lastDeleted}">$marker</th>
110+
<th class="{$this->headerClass}" title="{$this->lastDeleted}">&hellip;</th>
116111
<td class="Skipped">&hellip;</td>
117112
</tr>
118113
HTML;
114+
115+
$this->headerClass = '';
116+
$this->lastDeleted = null;
117+
118+
return $html;
119119
}
120120

121121
/**
@@ -125,22 +125,20 @@ public function generateSkippedLines(): string
125125
*/
126126
public function generateLinesEqual(array $changes): string
127127
{
128-
$html = '';
129-
$headerClass = '';
128+
$html = '';
130129

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

137-
$html .= <<<HTML
133+
$html .= <<<HTML
138134
<tr>
139-
<th class="$headerClass" title="{$this->lastDeleted}">$fromLine</th>
135+
<th class="{$this->headerClass}" title="{$this->lastDeleted}">$fromLine</th>
140136
<td>$line</td>
141137
</tr>
142138
HTML;
139+
143140
$this->lastDeleted = null;
141+
$this->headerClass = '';
144142
}
145143

146144
return $html;
@@ -153,22 +151,20 @@ public function generateLinesEqual(array $changes): string
153151
*/
154152
public function generateLinesInsert(array $changes): string
155153
{
156-
$html = '';
157-
$headerClass = '';
154+
$html = '';
158155

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

166160
$html .= <<<HTML
167161
<tr>
168-
<th class="$headerClass" title="{$this->lastDeleted}">$toLine</th>
162+
<th class="{$this->headerClass}" title="{$this->lastDeleted}">$toLine</th>
169163
<td><ins>$line</ins></td>
170164
</tr>
171165
HTML;
166+
167+
$this->headerClass = '';
172168
$this->lastDeleted = null;
173169
}
174170

@@ -197,6 +193,7 @@ public function generateLinesDelete(array $changes): string
197193
}
198194

199195
$this->lastDeleted = $title;
196+
$this->headerClass = 'ChangeDelete';
200197

201198
return '';
202199
}
@@ -208,14 +205,10 @@ public function generateLinesDelete(array $changes): string
208205
*/
209206
public function generateLinesReplace(array $changes): string
210207
{
211-
$html = '';
212-
$headerClass = '';
208+
$html = '';
213209

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

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

237230
$html .= <<<HTML
238231
<tr>
239-
<th class="$headerClass" title="{$this->lastDeleted}">$fromLine</th>
232+
<th class="{$this->headerClass}" title="{$this->lastDeleted}">$fromLine</th>
240233
<td>$line</td>
241234
</tr>
242235
HTML;
236+
$this->headerClass = '';
243237
$this->lastDeleted = null;
244238
}
245239

@@ -265,4 +259,30 @@ public function generateDiffFooter(): string
265259
{
266260
return '</table>';
267261
}
262+
263+
/**
264+
* @inheritDoc
265+
*
266+
* @return string Modified text.
267+
*/
268+
public function generateLinesIgnore(array $changes): string
269+
{
270+
$baseLineCount = count($changes['base']['lines']);
271+
$changedLineCount = count($changes['changed']['lines']);
272+
273+
$this->lineOffset -= $baseLineCount;
274+
275+
$title = "Lines ignored at {$this->options['title2']}: ";
276+
$title .= $changes['changed']['offset'] + 1 . '-' . ($changes['changed']['offset'] + $changedLineCount);
277+
278+
if ($baseLineCount > $changedLineCount) {
279+
$title = "Lines ignored at {$this->options['title1']}: ";
280+
$title .= $changes['base']['offset'] + 1 . '-' . ($changes['base']['offset'] + $baseLineCount);
281+
}
282+
283+
$this->lastDeleted = $title;
284+
$this->headerClass = 'ChangeIgnore';
285+
286+
return '';
287+
}
268288
}

lib/jblond/Diff/Renderer/Html/Unified.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,44 @@ public function generateLinesReplace(array $changes): string
219219
return $html;
220220
}
221221

222+
/**
223+
* @inheritDoc
224+
*
225+
* @return string Html code representing table rows showing modified text.
226+
*/
227+
public function generateLinesIgnore(array $changes): string
228+
{
229+
$html = '';
230+
231+
foreach ($changes['base']['lines'] as $lineNo => $line) {
232+
$fromLine = $changes['base']['offset'] + $lineNo + 1;
233+
$html .= <<<HTML
234+
<tr>
235+
<th>$fromLine</th>
236+
<th></th>
237+
<td class="Left Ignore">
238+
<span>$line</span>
239+
</td>
240+
</tr>
241+
HTML;
242+
}
243+
244+
foreach ($changes['changed']['lines'] as $lineNo => $line) {
245+
$toLine = $changes['changed']['offset'] + $lineNo + 1;
246+
$html .= <<<HTML
247+
<tr>
248+
<th></th>
249+
<th>$toLine</th>
250+
<td class="Right Ignore">
251+
<span>$line</span>
252+
</td>
253+
</tr>
254+
HTML;
255+
}
256+
257+
return $html;
258+
}
259+
222260
/**
223261
* @inheritDoc
224262
*

lib/jblond/Diff/Renderer/Text/Context.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function render()
6565
// Line differences between versions or lines of version 1 are removed from version 2.
6666
// Add all operations to diff-view of version 1, except for insert.
6767
$filteredGroups = $this->filterGroups($group, 'insert');
68+
$filteredGroups = $this->filterGroups($filteredGroups, 'ignore');
6869
foreach ($filteredGroups as [$tag, $start1, $end1, $start2, $end2]) {
6970
$diff .= $this->tagMap[$tag] . ' ' .
7071
implode(
@@ -81,6 +82,7 @@ public function render()
8182
// Line differences between versions or lines are inserted into version 2.
8283
// Add all operations to diff-view of version 2, except for delete.
8384
$filteredGroups = $this->filterGroups($group, 'delete');
85+
$filteredGroups = $this->filterGroups($filteredGroups, 'ignore');
8486
foreach ($filteredGroups as [$tag, $start1, $end1, $start2, $end2]) {
8587
$diff .= $this->tagMap[$tag] . ' ' .
8688
implode(

0 commit comments

Comments
 (0)