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

Use phutil_split_lines() in Differential

Summary:
  - We currently treat "\r" as a newline, but should not because VCSes do not.
  - We get an extra empty line at the end of diffs created after D3442 because we now retain newlines.
  - Historically we've converted tab pre-cache, but do it post-cache instead so we can add prefs about it, as we should handle it better than we do (e.g., let the user set it to a different width, infer width from comments in the file, expand it to actual tab stops, or show it visually in some way).

Test Plan:
  - Verified diffs no longer have an empty line at the end.
  - Created a diff of a "\r" file and verified it displayed somewhat reasonably. All browsers treat "\r" as a real newline so it's not necessarily perfect, but we can clean that up later. Hopefully these files are exceedingly rare.
  - Created a file with tabs and verified it came out reasonably.

Reviewers: vrana, btrahan

Reviewed By: vrana

CC: aran

Maniphest Tasks: T1857

Differential Revision: https://secure.phabricator.com/D3759
This commit is contained in:
epriestley 2012-10-20 06:13:24 -07:00
parent fd87a88d71
commit f6cb51562e

View file

@ -60,7 +60,7 @@ final class DifferentialChangesetParser {
private $markupEngine; private $markupEngine;
private $highlightErrors; private $highlightErrors;
const CACHE_VERSION = 6; const CACHE_VERSION = 7;
const CACHE_MAX_SIZE = 8e6; const CACHE_MAX_SIZE = 8e6;
const ATTR_GENERATED = 'attr:generated'; const ATTR_GENERATED = 'attr:generated';
@ -132,8 +132,8 @@ final class DifferentialChangesetParser {
foreach ($changeset->getHunks() as $hunk) { foreach ($changeset->getHunks() as $hunk) {
$n_old = $hunk->getOldOffset(); $n_old = $hunk->getOldOffset();
$n_new = $hunk->getNewOffset(); $n_new = $hunk->getNewOffset();
$changes = rtrim($hunk->getChanges(), "\n"); $changes = phutil_split_lines($hunk->getChanges());
foreach (explode("\n", $changes) as $line) { foreach ($changes as $line) {
$diff_type = $line[0]; // Change type in diff of diffs. $diff_type = $line[0]; // Change type in diff of diffs.
$orig_type = $line[1]; // Change type in the original diff. $orig_type = $line[1]; // Change type in the original diff.
if ($diff_type == ' ') { if ($diff_type == ' ') {
@ -267,12 +267,7 @@ final class DifferentialChangesetParser {
public function parseHunk(DifferentialHunk $hunk) { public function parseHunk(DifferentialHunk $hunk) {
$lines = $hunk->getChanges(); $lines = $hunk->getChanges();
$lines = phutil_split_lines($lines);
$lines = str_replace(
array("\t", "\r\n", "\r"),
array(' ', "\n", "\n"),
$lines);
$lines = explode("\n", $lines);
$types = array(); $types = array();
foreach ($lines as $line_index => $line) { foreach ($lines as $line_index => $line) {
@ -573,7 +568,7 @@ final class DifferentialChangesetParser {
$old_corpus[] = $o['text']; $old_corpus[] = $o['text'];
} }
} }
$old_corpus_block = implode("\n", $old_corpus); $old_corpus_block = implode('', $old_corpus);
$new_corpus = array(); $new_corpus = array();
foreach ($this->new as $n) { foreach ($this->new as $n) {
@ -581,7 +576,7 @@ final class DifferentialChangesetParser {
$new_corpus[] = $n['text']; $new_corpus[] = $n['text'];
} }
} }
$new_corpus_block = implode("\n", $new_corpus); $new_corpus_block = implode('', $new_corpus);
$this->markGenerated($new_corpus_block); $this->markGenerated($new_corpus_block);
@ -600,6 +595,7 @@ final class DifferentialChangesetParser {
'old' => $old_corpus_block, 'old' => $old_corpus_block,
'new' => $new_corpus_block, 'new' => $new_corpus_block,
); );
$this->highlightErrors = false; $this->highlightErrors = false;
foreach (Futures($futures) as $key => $future) { foreach (Futures($futures) as $key => $future) {
try { try {
@ -820,6 +816,14 @@ final class DifferentialChangesetParser {
} }
protected function getHighlightFuture($corpus) { 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( return $this->highlightEngine->getHighlightFuture(
$this->highlightEngine->getLanguageFromFilename($this->filename), $this->highlightEngine->getLanguageFromFilename($this->filename),
$corpus); $corpus);
@ -827,7 +831,7 @@ final class DifferentialChangesetParser {
protected function processHighlightedSource($data, $result) { protected function processHighlightedSource($data, $result) {
$result_lines = explode("\n", $result); $result_lines = phutil_split_lines($result);
foreach ($data as $key => $info) { foreach ($data as $key => $info) {
if (!$info) { if (!$info) {
unset($result_lines[$key]); unset($result_lines[$key]);
@ -1774,13 +1778,20 @@ final class DifferentialChangesetParser {
$notice = $this->renderChangeTypeHeader($this->changeset, false); $notice = $this->renderChangeTypeHeader($this->changeset, false);
} }
return implode( $result = implode(
"\n", "\n",
array( array(
$notice, $notice,
$props, $props,
$table, $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) { protected function renderChangeTypeHeader($changeset, $force) {