From 7e2707d9798646a637df5e4770dcadcf7e63e73d Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 25 Nov 2008 16:14:58 +0100 Subject: [PATCH] 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. --- html/css/diff.css | 8 +-- html/lib/diffHighlighter.js | 118 ++++++++++++++++------------------ html/views/commit/commit.js | 6 +- html/views/history/history.js | 18 ++---- html/views/history/index.html | 3 +- 5 files changed, 71 insertions(+), 82 deletions(-) diff --git a/html/css/diff.css b/html/css/diff.css index 8da1211..2b6ad3e 100644 --- a/html/css/diff.css +++ b/html/css/diff.css @@ -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; } diff --git a/html/lib/diffHighlighter.js b/html/lib/diffHighlighter.js index 66c8806..f2cc6d0 100644 --- a/html/lib/diffHighlighter.js +++ b/html/lib/diffHighlighter.js @@ -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 += '
' + file_index + ' ' + match[1] + '
'; + if (header) { + if (firstChar == "+" || firstChar == "-") continue; - } - - if (firstChar == "+") { - // Highlight trailing whitespace - if (m = l.match(/\s+$/)) - l = l.replace(/\s+$/, "" + m + ""); - - line1 += "\n"; - line2 += ++hunk_start_line_2 + "\n"; - diffContent += "
" + l + "
"; - } else if (firstChar == "-") { - line1 += ++hunk_start_line_1 + "\n"; - line2 += "\n"; - diffContent += "
" + l + "
"; - } 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 += "
" + l + "
"; - } 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 += '
' + file_index + ' ' + match[1] + '
'; + continue; } - // This takes about 7ms - diff.innerHTML = "" + - "" + - "
" + line1 + "
" + line2 + "
" + diffContent + "
"; + if (firstChar == "+") { + // Highlight trailing whitespace + if (m = l.match(/\s+$/)) + l = l.replace(/\s+$/, "" + m + ""); + + line1 += "\n"; + line2 += ++hunk_start_line_2 + "\n"; + diffContent += "
" + l + "
"; + } else if (firstChar == "-") { + line1 += ++hunk_start_line_1 + "\n"; + line2 += "\n"; + diffContent += "
" + l + "
"; + } 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 += "
" + l + "
"; + } else if (firstChar == " ") { + line1 += ++hunk_start_line_1 + "\n"; + line2 += ++hunk_start_line_2 + "\n"; + diffContent += l + "\n"; + } } + // This takes about 7ms + element.innerHTML = "" + + "" + + "
" + line1 + "
" + line2 + "
" + diffContent + "
"; + // TODO: Replace this with a performance pref call if (false) Controller.log_("Total time:" + (new Date().getTime() - start)); diff --git a/html/views/commit/commit.js b/html/views/commit/commit.js index 60516f5..99e8d47 100644 --- a/html/views/commit/commit.js +++ b/html/views/commit/commit.js @@ -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) { diff --git a/html/views/history/history.js b/html/views/history/history.js index f0da2d2..1f4003c 100644 --- a/html/views/history/history.js +++ b/html/views/history/history.js @@ -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,"
"); - 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("
"); + } if (commit.diff.length < 200000) { - showDiffs(); + highlightDiff(commit.diff, $("diff")); } else { - $("details").innerHTML = "This is a large commit. Click here or press 'v' to view."; + $("diff").innerHTML = "This is a large commit. Click here or press 'v' to view."; } + hideNotification(); setGravatar(commit.author_email, $("gravatar")); } \ No newline at end of file diff --git a/html/views/history/index.html b/html/views/history/index.html index 9bc31c1..2078ec3 100644 --- a/html/views/history/index.html +++ b/html/views/history/index.html @@ -51,5 +51,6 @@
-
+ +