function DiffFormatter::format in Diff 6
Same name and namespace in other branches
- 5.2 DiffEngine.php \DiffFormatter::format()
- 5 DiffEngine.php \DiffFormatter::format()
- 6.2 DiffEngine.php \DiffFormatter::format()
- 7.3 DiffEngine.php \DiffFormatter::format()
- 7.2 DiffEngine.php \DiffFormatter::format()
* Format a diff. * *
Parameters
$diff object A Diff object.: * @return string The formatted output.
File
- ./
DiffEngine.php, line 766
Class
- DiffFormatter
- A class to format Diffs
Code
function format($diff) {
$xi = $yi = 1;
$block = false;
$context = array();
$nlead = $this->leading_context_lines;
$ntrail = $this->trailing_context_lines;
$this
->_start_diff();
foreach ($diff->edits as $edit) {
if ($edit->type == 'copy') {
if (is_array($block)) {
if (sizeof($edit->orig) <= $nlead + $ntrail) {
$block[] = $edit;
}
else {
if ($ntrail) {
$context = array_slice($edit->orig, 0, $ntrail);
$block[] = new _DiffOp_Copy($context);
}
$this
->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
$block = false;
}
}
$context = $edit->orig;
}
else {
if (!is_array($block)) {
$context = array_slice($context, sizeof($context) - $nlead);
$x0 = $xi - sizeof($context);
$y0 = $yi - sizeof($context);
$block = array();
if ($context) {
$block[] = new _DiffOp_Copy($context);
}
}
$block[] = $edit;
}
if ($edit->orig) {
$xi += sizeof($edit->orig);
}
if ($edit->closing) {
$yi += sizeof($edit->closing);
}
}
if (is_array($block)) {
$this
->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
}
$end = $this
->_end_diff();
return $end;
}