[gitg/wip/techlivezh/diff-view-clean-up: 19/21] Extract to 'diff_hunk()' function



commit 7e9d60fa39053603dd93726030415980f6de8565
Author: Techlive Zheng <techlivezheng gmail com>
Date:   Thu Dec 19 19:31:49 2013 +0800

    Extract to 'diff_hunk()' function
    
    https://bugzilla.gnome.org/show_bug.cgi?id=720886

 libgitg/resources/diff-view-html-builder.js |  179 ++++++++++++++-------------
 1 files changed, 92 insertions(+), 87 deletions(-)
---
diff --git a/libgitg/resources/diff-view-html-builder.js b/libgitg/resources/diff-view-html-builder.js
index 9e00224..2fa0a55 100644
--- a/libgitg/resources/diff-view-html-builder.js
+++ b/libgitg/resources/diff-view-html-builder.js
@@ -26,8 +26,8 @@ function exec_template(template, replacements) {
 
 function diff_file(file, lnstate, data)
 {
-       var added = 0;
-       var removed = 0;
+       lnstate.added = 0;
+       lnstate.removed = 0;
 
        /**
         * file_body
@@ -36,88 +36,7 @@ function diff_file(file, lnstate, data)
 
        for (var i = 0; i < file.hunks.length; ++i)
        {
-               var h = file.hunks[i];
-
-               var cold = h.range.old.start;
-               var cnew = h.range.new.start;
-
-               var hunk_stats = '<span class="hunk_stats">@@ -' + h.range.old.start + ',' + 
h.range.old.lines + ' +' + h.range.new.start + ',' + h.range.new.lines + ' @@</span>';
-
-               hunk_stats = lnstate.stagebutton + hunk_stats;
-
-               file_body += '<tr class="hunk_header">\
-                       <td class="gutter old">' + lnstate.gutterdots + '</td> \
-                       <td class="gutter new">' + lnstate.gutterdots + '</td> \
-                       <td class="hunk_header">' + hunk_stats + '</td> \
-               </tr>';
-
-               for (var j = 0; j < h.lines.length; ++j)
-               {
-                       var l = h.lines[j];
-
-                       var row = '<tr class="';
-
-                       switch (String.fromCharCode(l.type))
-                       {
-                               case ' ':
-                                       row += 'context"> \
-                                               <td class="gutter old">' + cold + '</td> \
-                                               <td class="gutter new">' + cnew + '</td>';
-
-                                       cold++;
-                                       cnew++;
-                                       break;
-                               case '+':
-                                       row += 'added"> \
-                                               <td class="gutter old"></td> \
-                                               <td class="gutter new">' + cnew + '</td>';
-
-                                       cnew++;
-                                       added++;
-                                       break;
-                               case '-':
-                                       row += 'removed"> \
-                                               <td class="gutter old">' + cold + '</td> \
-                                               <td class="gutter new"></td>';
-
-                                       cold++;
-                                       removed++;
-                                       break;
-                               case '=':
-                               case '>':
-                               case '<':
-                                       row += 'context"> \
-                                               <td class="gutter old"></td> \
-                                               <td class="gutter new"></td>';
-                                               l.content = l.content.substr(1, l.content.length);
-                                       break;
-                               default:
-                                       row += '">';
-                                       break;
-                       }
-
-                       l.content = html_escape(l.content).replace(/\t/g, '<span class="tab" style="width: ' 
+ data.settings.tab_width + 'ex">\t</span>');
-
-                       row += '<td class="code">' + l.content + '</td>';
-
-                       row += '</tr>';
-
-                       file_body += row;
-
-                       lnstate.processed++;
-
-                       proc = lnstate.processed / lnstate.lines;
-
-                       if (proc >= lnstate.nexttick)
-                       {
-                               self.postMessage({tick: proc});
-
-                               while (proc >= lnstate.nexttick)
-                               {
-                                       lnstate.nexttick += lnstate.tickfreq;
-                               }
-                       }
-               }
+               file_body += diff_hunk(file.hunks[i], lnstate, data);
        }
 
        /**
@@ -137,11 +56,11 @@ function diff_file(file, lnstate, data)
        /**
         * file_stats
         */
-       var total = added + removed;
-       var addedp = Math.floor(added / total * 100);
+       var total = lnstate.added + lnstate.removed;
+       var addedp = Math.floor(lnstate.added / total * 100);
        var removedp = 100 - addedp;
 
-       var file_stats = '<span class="file_stats"><span class="number">' + (added + removed)  + 
'</span><span class="bar"><span class="added" style="width: ' + addedp + '%;"></span><span class="removed" 
style="width: ' + removedp + '%;"></span></span></span>';
+       var file_stats = '<span class="file_stats"><span class="number">' + (lnstate.added + lnstate.removed) 
 + '</span><span class="bar"><span class="added" style="width: ' + addedp + '%;"></span><span class="removed" 
style="width: ' + removedp + '%;"></span></span></span>';
 
        file_stats = lnstate.stagebutton + file_stats;
 
@@ -155,6 +74,92 @@ function diff_file(file, lnstate, data)
        });
 }
 
+function diff_hunk(hunk, lnstate, data)
+{
+       var cold = hunk.range.old.start;
+       var cnew = hunk.range.new.start;
+
+       var hunk_stats = '<span class="hunk_stats">@@ -' + hunk.range.old.start + ',' + hunk.range.old.lines 
+ ' +' + hunk.range.new.start + ',' + hunk.range.new.lines + ' @@</span>';
+
+       hunk_stats = lnstate.stagebutton + hunk_stats;
+
+       var hunk_body = '<tr class="hunk_header">\
+               <td class="gutter old">' + lnstate.gutterdots + '</td> \
+               <td class="gutter new">' + lnstate.gutterdots + '</td> \
+               <td class="hunk_header">' + hunk_stats + '</td> \
+       </tr>';
+
+       for (var i = 0; i < hunk.lines.length; ++i)
+       {
+               var l = hunk.lines[i];
+
+               var row = '<tr class="';
+
+               switch (String.fromCharCode(l.type))
+               {
+                       case ' ':
+                               row += 'context"> \
+                                       <td class="gutter old">' + cold + '</td> \
+                                       <td class="gutter new">' + cnew + '</td>';
+
+                               cold++;
+                               cnew++;
+                               break;
+                       case '+':
+                               row += 'added"> \
+                                       <td class="gutter old"></td> \
+                                       <td class="gutter new">' + cnew + '</td>';
+
+                               cnew++;
+                               lnstate.added++;
+                               break;
+                       case '-':
+                               row += 'removed"> \
+                                       <td class="gutter old">' + cold + '</td> \
+                                       <td class="gutter new"></td>';
+
+                               cold++;
+                               lnstate.removed++;
+                               break;
+                       case '=':
+                       case '>':
+                       case '<':
+                               row += 'context"> \
+                                       <td class="gutter old"></td> \
+                                       <td class="gutter new"></td>';
+                                       l.content = l.content.substr(1, l.content.length);
+                               break;
+                       default:
+                               row += '">';
+                               break;
+               }
+
+               l.content = html_escape(l.content).replace(/\t/g, '<span class="tab" style="width: ' + 
data.settings.tab_width + 'ex">\t</span>');
+
+               row += '<td class="code">' + l.content + '</td>';
+
+               row += '</tr>';
+
+               hunk_body += row;
+
+               lnstate.processed++;
+
+               proc = lnstate.processed / lnstate.lines;
+
+               if (proc >= lnstate.nexttick)
+               {
+                       self.postMessage({tick: proc});
+
+                       while (proc >= lnstate.nexttick)
+                       {
+                               lnstate.nexttick += lnstate.tickfreq;
+                       }
+               }
+       }
+
+       return hunk_body;
+}
+
 function diff_files(files, lines, maxlines, data)
 {
        var lnstate = {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]