1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-26 08:42:41 +01:00

Restore phutil_split_lines() to Differential

Summary:
Same as D3759 with a fix:

Previously, we would insert `null` to indicate a line that doesn't exist on one side of a diff, and then implode on "\n", so we'd get "\n" as a result.

In D3759, we'd insert `null` but implode on empty string, and get nothing as a result.

When a placeholder null is present, explicitly insert a newline.

Test Plan: D3759 plus examined a diff with removed lines on the left side prior to new lines on the right side.

Reviewers: vrana

Reviewed By: vrana

CC: aran

Differential Revision: https://secure.phabricator.com/D3765
This commit is contained in:
epriestley 2012-10-20 07:11:19 -07:00
parent 468aeaef0d
commit 67498f3334

View file

@ -60,7 +60,7 @@ final class DifferentialChangesetParser {
private $markupEngine;
private $highlightErrors;
const CACHE_VERSION = 6;
const CACHE_VERSION = 8;
const CACHE_MAX_SIZE = 8e6;
const ATTR_GENERATED = 'attr:generated';
@ -132,8 +132,8 @@ final class DifferentialChangesetParser {
foreach ($changeset->getHunks() as $hunk) {
$n_old = $hunk->getOldOffset();
$n_new = $hunk->getNewOffset();
$changes = rtrim($hunk->getChanges(), "\n");
foreach (explode("\n", $changes) as $line) {
$changes = phutil_split_lines($hunk->getChanges());
foreach ($changes as $line) {
$diff_type = $line[0]; // Change type in diff of diffs.
$orig_type = $line[1]; // Change type in the original diff.
if ($diff_type == ' ') {
@ -267,12 +267,7 @@ final class DifferentialChangesetParser {
public function parseHunk(DifferentialHunk $hunk) {
$lines = $hunk->getChanges();
$lines = str_replace(
array("\t", "\r\n", "\r"),
array(' ', "\n", "\n"),
$lines);
$lines = explode("\n", $lines);
$lines = phutil_split_lines($lines);
$types = array();
foreach ($lines as $line_index => $line) {
@ -570,18 +565,28 @@ final class DifferentialChangesetParser {
$old_corpus = array();
foreach ($this->old as $o) {
if ($o['type'] != '\\') {
$old_corpus[] = $o['text'];
if ($o['text'] === null) {
// There's no text on this side of the diff, but insert a placeholder
// newline so the highlighted line numbers match up.
$old_corpus[] = "\n";
} else {
$old_corpus[] = $o['text'];
}
}
}
$old_corpus_block = implode("\n", $old_corpus);
$old_corpus_block = implode('', $old_corpus);
$new_corpus = array();
foreach ($this->new as $n) {
if ($n['type'] != '\\') {
$new_corpus[] = $n['text'];
if ($n['text'] === null) {
$new_corpus[] = "\n";
} else {
$new_corpus[] = $n['text'];
}
}
}
$new_corpus_block = implode("\n", $new_corpus);
$new_corpus_block = implode('', $new_corpus);
$this->markGenerated($new_corpus_block);
@ -600,6 +605,7 @@ final class DifferentialChangesetParser {
'old' => $old_corpus_block,
'new' => $new_corpus_block,
);
$this->highlightErrors = false;
foreach (Futures($futures) as $key => $future) {
try {
@ -820,6 +826,14 @@ final class DifferentialChangesetParser {
}
protected function getHighlightFuture($corpus) {
if (preg_match('/\r(?!\n)/', $corpus)) {
// TODO: Pygments converts "\r" newlines into "\n" newlines, so we can't
// use it on files with "\r" newlines. If we have "\r" not followed by
// "\n" in the file, skip highlighting.
$result = phutil_escape_html($corpus);
return new ImmediateFuture($result);
}
return $this->highlightEngine->getHighlightFuture(
$this->highlightEngine->getLanguageFromFilename($this->filename),
$corpus);
@ -827,7 +841,7 @@ final class DifferentialChangesetParser {
protected function processHighlightedSource($data, $result) {
$result_lines = explode("\n", $result);
$result_lines = phutil_split_lines($result);
foreach ($data as $key => $info) {
if (!$info) {
unset($result_lines[$key]);
@ -1774,13 +1788,20 @@ final class DifferentialChangesetParser {
$notice = $this->renderChangeTypeHeader($this->changeset, false);
}
return implode(
$result = implode(
"\n",
array(
$notice,
$props,
$table,
));
// TODO: Let the user customize their tab width / display style.
$result = str_replace("\t", ' ', $result);
// TODO: We should possibly post-process "\r" as well.
return $result;
}
protected function renderChangeTypeHeader($changeset, $force) {