in src/ColoredUnifiedDiff.php [69:143]
final public static function create(
string $a,
string $b,
int $context = 3,
): TOut {
$diff = StringDiff::lines($a, $b)->getUnifiedDiff($context);
$out = vec[];
$lines = Str\split($diff, "\n");
while (!C\is_empty($lines)) {
$line = C\firstx($lines);
$lines = Vec\drop($lines, 1);
if ($line === '') {
invariant(C\is_empty($lines), 'Blank line was not last line');
break;
}
if ($line[0] === '@') {
$out[] = static::colorHeaderLine($line);
continue;
}
if ($line[0] === ' ') {
$out[] = static::colorKeepLine($line);
continue;
}
if ($line[0] === '+') {
$out[] = static::colorInsertLine($line);
continue;
}
if ($line[0] === '-') {
$next = C\first($lines);
if (
$next !== null && $next !== '' && $next[0] === '+'
// Levenshtein function throws for strings with length > 255
// Don't need intra word diffs for long lines
&& Str\length($line) < 256 && Str\length($next) < 256
// -2 to deal with the prefix
/* HH_FIXME[4107] using directly because this is open source */
/* HH_FIXME[2049] using directly because this is open source */
&& \levenshtein($line, $next) - 2 <= (0.5 * (Str\length($line) - 2))
) {
// Drop the prefix
$line = Str\slice($line, 1);
$next = Str\slice($next, 1);
$lines = Vec\drop($lines, 1);
$words_line = vec(\preg_split(
'/([^a-zA-Z0-9_]+)/',
$line,
-1,
\PREG_SPLIT_DELIM_CAPTURE,
));
$words_next = vec(\preg_split(
'/([^a-zA-Z0-9_]+)/',
$next,
-1,
\PREG_SPLIT_DELIM_CAPTURE,
));
$intraline = (new StringDiff($words_line, $words_next))->getDiff();
$out[] = $intraline
|> Vec\filter($$, $op ==> !$op->isInsertOp())
|> static::colorDeleteLineWithIntralineEdits($$);
$out[] = $intraline
|> Vec\filter($$, $op ==> !$op->isDeleteOp())
|> static::colorInsertLineWithIntralineEdits($$);
continue;
}
$out[] = static::colorDeleteLine($line);
continue;
}
invariant_violation('unhandled line prefix: %s', $line[0]);
}
return static::join($out);
}