DiffHilighter: Specific the div in which to hilight

Previously, the diff hilighter was a bit odd in that it would
just search for elements with a diff and highlight those. While
useful in theory, this wasn't used at all. We change it to
receive the diff to hilight, which makes the code somewhat simpler.
This commit is contained in:
Pieter de Bie
2008-11-25 16:14:58 +01:00
parent f05d0188fc
commit 7e2707d979
5 changed files with 71 additions and 82 deletions
+4 -4
View File
@@ -11,20 +11,20 @@ body {
border-top:0.1em solid #999999;
}
code .delline, code .oldfile {
.delline, .oldfile {
background-color: #FEE;
color: #B00;
}
code .addline, code .newfile {
.addline, .newfile {
background-color: #DFD;
color: #080;
}
code .fileline {
.fileline {
font-weight: bold;
}
code .hunkheader {
.hunkheader {
background-color: #f7f7f7;
color: #bbb;
}
+57 -61
View File
@@ -6,79 +6,75 @@ if (typeof Controller == 'undefined') {
Controller.log_ = console.log;
}
var highlightDiffs = function() {
var highlightDiff = function(diff, element) {
var start = new Date().getTime();
var diffs = document.getElementsByClassName("diffcode");
for (var diffn = 0; diffn < diffs.length; diffn++) {
var diff = diffs[diffn];
var content = diff.escapeHTML().replace(/\t/g, " ");;
var file_index = 0;
var content = diff.innerHTML.replace(/\t/g, " ");;
var file_index = 0;
var line1 = "";
var line2 = "";
var diffContent = "";
var lines = content.split('\n');
var line1 = "";
var line2 = "";
var diffContent = "";
var lines = content.split('\n');
var hunk_start_line_1 = -1;
var hunk_start_line_2 = -1;
var hunk_start_line_1 = -1;
var hunk_start_line_2 = -1;
var header = false;
var header = false;
for (var lineno = 0; lineno < lines.length; lineno++) {
var l = lines[lineno];
for (var lineno = 0; lineno < lines.length; lineno++) {
var l = lines[lineno];
var firstChar = l.charAt(0);
var firstChar = l.charAt(0);
if (header) {
if (firstChar == "+" || firstChar == "-")
continue;
} else if (firstChar == "d") {
++file_index;
header = true;
line1 += '\n';
line2 += '\n';
var match = l.match(/diff --git a\/(\S*)/);
diffContent += '</div><div class="fileHeader" id="file_index_' + file_index + '">' + file_index + ' <span class="fileline">' + match[1] + '</span></div>';
if (header) {
if (firstChar == "+" || firstChar == "-")
continue;
}
if (firstChar == "+") {
// Highlight trailing whitespace
if (m = l.match(/\s+$/))
l = l.replace(/\s+$/, "<span class='whitespace'>" + m + "</span>");
line1 += "\n";
line2 += ++hunk_start_line_2 + "\n";
diffContent += "<div class='addline'>" + l + "</div>";
} else if (firstChar == "-") {
line1 += ++hunk_start_line_1 + "\n";
line2 += "\n";
diffContent += "<div class='delline'>" + l + "</div>";
} else if (firstChar == "@") {
header = false;
if (m = l.match(/@@ \-([0-9]+),\d+ \+(\d+),\d+ @@/))
{
hunk_start_line_1 = parseInt(m[1]) - 1;
hunk_start_line_2 = parseInt(m[2]) - 1;
}
line1 += "...\n";
line2 += "...\n";
diffContent += "<div class='hunkheader'>" + l + "</div>";
} else if (firstChar == " ") {
line1 += ++hunk_start_line_1 + "\n";
line2 += ++hunk_start_line_2 + "\n";
diffContent += l + "\n";
}
} else if (firstChar == "d") {
++file_index;
header = true;
line1 += '\n';
line2 += '\n';
var match = l.match(/diff --git a\/(\S*)/);
diffContent += '</div><div class="fileHeader" id="file_index_' + file_index + '">' + file_index + ' <span class="fileline">' + match[1] + '</span></div>';
continue;
}
// This takes about 7ms
diff.innerHTML = "<table class='diff'><tr><td class='lineno'l><pre>" + line1 + "</pre></td>" +
"<td class='lineno'l><pre>" + line2 + "</pre></td>" +
"<td width='100%'><pre width='100%'>" + diffContent + "</pre></td></tr></table>";
if (firstChar == "+") {
// Highlight trailing whitespace
if (m = l.match(/\s+$/))
l = l.replace(/\s+$/, "<span class='whitespace'>" + m + "</span>");
line1 += "\n";
line2 += ++hunk_start_line_2 + "\n";
diffContent += "<div class='addline'>" + l + "</div>";
} else if (firstChar == "-") {
line1 += ++hunk_start_line_1 + "\n";
line2 += "\n";
diffContent += "<div class='delline'>" + l + "</div>";
} else if (firstChar == "@") {
header = false;
if (m = l.match(/@@ \-([0-9]+),\d+ \+(\d+),\d+ @@/))
{
hunk_start_line_1 = parseInt(m[1]) - 1;
hunk_start_line_2 = parseInt(m[2]) - 1;
}
line1 += "...\n";
line2 += "...\n";
diffContent += "<div class='hunkheader'>" + l + "</div>";
} else if (firstChar == " ") {
line1 += ++hunk_start_line_1 + "\n";
line2 += ++hunk_start_line_2 + "\n";
diffContent += l + "\n";
}
}
// This takes about 7ms
element.innerHTML = "<table class='diff'><tr><td class='lineno'l><pre>" + line1 + "</pre></td>" +
"<td class='lineno'l><pre>" + line2 + "</pre></td>" +
"<td width='100%'><pre width='100%'>" + diffContent + "</pre></td></tr></table>";
// TODO: Replace this with a performance pref call
if (false)
Controller.log_("Total time:" + (new Date().getTime() - start));
+2 -4
View File
@@ -26,7 +26,6 @@ var showFileChanges = function(file, cached) {
if (!file)
return;
$("diff").style.display = 'none';
hideNotification();
hideState();
@@ -49,7 +48,6 @@ var showFileChanges = function(file, cached) {
}
displayDiff(changes, cached);
$("diff").style.display = '';
}
var diffHeader;
@@ -60,8 +58,8 @@ var displayDiff = function(diff, cached)
diffHeader = diff.split("\n").slice(0,4).join("\n");
originalDiff = diff;
$("diff").innerHTML = diff.escapeHTML();
highlightDiffs();
$("diff").style.display = "";
highlightDiff(diff, $("diff"));
hunkHeaders = $("diff").getElementsByClassName("hunkheader");
for (i = 0; i < hunkHeaders.length; ++i) {
+6 -12
View File
@@ -118,14 +118,6 @@ var selectCommit = function(a) {
Controller.selectCommit_(a);
}
var showDiffs = function() {
var details = $("details");
details.style.display = "none";
details.innerHTML = commit.diff.escapeHTML();
highlightDiffs();
details.style.display = "";
}
var reload = function() {
$("notification").style.display = "none";
commit.reloadRefs();
@@ -158,7 +150,7 @@ var loadCommit = function(commitObject, currentRef) {
$("commitID").innerHTML = commit.sha;
$("authorID").innerHTML = commit.author_name;
$("subjectID").innerHTML = commit.subject.escapeHTML();
$("details").innerHTML = ""
$("diff").innerHTML = ""
$("message").innerHTML = ""
$("files").innerHTML = ""
$("date").innerHTML = ""
@@ -189,7 +181,7 @@ var loadExtendedCommit = function(commit)
$("date").innerHTML = commit.author_date;
$("message").innerHTML = commit.message.replace(/\n/g,"<br>");
if (commit.files)
if (commit.files) {
var commit_file_links = commit.files;
for (var i=0; i < commit_file_links.length; i++) {
index = i+1;
@@ -197,12 +189,14 @@ var loadExtendedCommit = function(commit)
}
$("files").innerHTML = commit_file_links.join("<br>");
}
if (commit.diff.length < 200000) {
showDiffs();
highlightDiff(commit.diff, $("diff"));
} else {
$("details").innerHTML = "<a class='showdiff' href='' onclick='showDiffs(); return false;'>This is a large commit. Click here or press 'v' to view.</a>";
$("diff").innerHTML = "<a class='showdiff' href='' onclick='showDiffs(); return false;'>This is a large commit. Click here or press 'v' to view.</a>";
}
hideNotification();
setGravatar(commit.author_email, $("gravatar"));
}
+2 -1
View File
@@ -51,5 +51,6 @@
<div id="message"></div>
<div id="files"></div>
</div>
<pre><code class="diffcode" id='details'></code></pre>
<div id="diff"></div>
</body>