Skip to content

Commit 08f3cf7

Browse files
committed
Remove extra empty lines when setting context to 0
Signed-off-by: Jack Cherng <[email protected]>
1 parent cfc46e3 commit 08f3cf7

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

src/Renderer/Html/AbstractHtml.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ public function getChanges(): array
7777
$lastTag = $tag;
7878

7979
if ($tag === SequenceMatcher::OPCODE_EQUAL) {
80-
$lines = \array_slice($a, $i1, ($i2 - $i1));
81-
$blocks[$lastBlock]['base']['lines'] += $this->formatLines($lines);
82-
$lines = \array_slice($b, $j1, ($j2 - $j1));
83-
$blocks[$lastBlock]['changed']['lines'] += $this->formatLines($lines);
80+
if (!empty($lines = \array_slice($a, $i1, ($i2 - $i1)))) {
81+
$formattedLines = $this->formatLines($lines);
82+
83+
$blocks[$lastBlock]['base']['lines'] += $formattedLines;
84+
$blocks[$lastBlock]['changed']['lines'] += $formattedLines;
85+
}
8486
} else {
8587
if (
8688
$tag === SequenceMatcher::OPCODE_REPLACE ||

src/Renderer/Text/Context.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ protected function renderBlockFrom(array $opcodes): string
8080
continue;
8181
}
8282

83-
$ret .= self::TAG_MAP[$tag] . ' ' . \implode("\n" . self::TAG_MAP[$tag] . ' ', $this->diff->getA($i1, $i2)) . "\n";
83+
$ret .= $this->renderContext(
84+
self::TAG_MAP[$tag],
85+
$this->diff->getA($i1, $i2)
86+
);
8487
}
8588

8689
return $ret;
@@ -102,9 +105,27 @@ protected function renderBlockTo(array $opcodes): string
102105
continue;
103106
}
104107

105-
$ret .= self::TAG_MAP[$tag] . ' ' . \implode("\n" . self::TAG_MAP[$tag] . ' ', $this->diff->getB($j1, $j2)) . "\n";
108+
$ret .= $this->renderContext(
109+
self::TAG_MAP[$tag],
110+
$this->diff->getB($j1, $j2)
111+
);
106112
}
107113

108114
return $ret;
109115
}
116+
117+
/**
118+
* Render the context array with the symbol.
119+
*
120+
* @param string $symbol the symbol
121+
* @param array $context the context
122+
*
123+
* @return string
124+
*/
125+
protected function renderContext(string $symbol, array $context): string
126+
{
127+
return empty($context)
128+
? ''
129+
: "{$symbol} " . \implode("\n{$symbol} ", $context) . "\n";
130+
}
110131
}

src/Renderer/Text/Unified.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function render(): string
2525
{
2626
$ret = '';
2727

28+
// var_dump($this->diff->getGroupedOpcodes());
2829
foreach ($this->diff->getGroupedOpcodes() as $opcodes) {
2930
$lastItem = \count($opcodes) - 1;
3031

@@ -41,7 +42,7 @@ public function render(): string
4142

4243
foreach ($opcodes as [$tag, $i1, $i2, $j1, $j2]) {
4344
if ($tag === SequenceMatcher::OPCODE_EQUAL) {
44-
$ret .= ' ' . \implode("\n ", $this->diff->getA($i1, $i2)) . "\n";
45+
$ret .= $this->renderContext(' ', $this->diff->getA($i1, $i2));
4546

4647
continue;
4748
}
@@ -50,18 +51,33 @@ public function render(): string
5051
$tag === SequenceMatcher::OPCODE_REPLACE ||
5152
$tag === SequenceMatcher::OPCODE_DELETE
5253
) {
53-
$ret .= '-' . \implode("\n-", $this->diff->getA($i1, $i2)) . "\n";
54+
$ret .= $this->renderContext('-', $this->diff->getA($i1, $i2));
5455
}
5556

5657
if (
5758
$tag === SequenceMatcher::OPCODE_REPLACE ||
5859
$tag === SequenceMatcher::OPCODE_INSERT
5960
) {
60-
$ret .= '+' . \implode("\n+", $this->diff->getB($j1, $j2)) . "\n";
61+
$ret .= $this->renderContext('+', $this->diff->getB($j1, $j2));
6162
}
6263
}
6364
}
6465

6566
return $ret;
6667
}
68+
69+
/**
70+
* Render the context array with the symbol.
71+
*
72+
* @param string $symbol the symbol
73+
* @param array $context the context
74+
*
75+
* @return string
76+
*/
77+
protected function renderContext(string $symbol, array $context): string
78+
{
79+
return empty($context)
80+
? ''
81+
: $symbol . \implode("\n{$symbol}", $context) . "\n";
82+
}
6783
}

0 commit comments

Comments
 (0)