[gitg/wip/techlivezh/bug/720886: 4/7] Extract to 'diff_hunk()' function



commit 13a0bdeec6fc107817aa465e8d5fde535115157e
Author: Techlive Zheng <techlivezheng gmail com>
Date:   Fri Dec 20 11:42:35 2013 +0800

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

 libgitg/resources/diff-view-html-builder.js |  143 ++++++++++++++-------------
 1 files changed, 75 insertions(+), 68 deletions(-)
---
diff --git a/libgitg/resources/diff-view-html-builder.js b/libgitg/resources/diff-view-html-builder.js
index dc7915a..af1dad9 100644
--- a/libgitg/resources/diff-view-html-builder.js
+++ b/libgitg/resources/diff-view-html-builder.js
@@ -3,99 +3,106 @@ function html_escape(s)
        return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
 }
 
-function diff_file(file, lnstate, data)
+function diff_hunk(hunk, lnstate, data)
 {
-       var added = 0;
-       var removed = 0;
+       var hunk_body = '';
 
-       var file_body = '';
+       var hunk_header = '<span class="hunk_stats">@@ -' + hunk.range.old.start + ',' + hunk.range.old.lines 
+ ' +' + hunk.range.new.start + ',' + hunk.range.new.lines + ' @@</span>';
 
-       for (var i = 0; i < file.hunks.length; ++i)
-       {
-               var h = file.hunks[i];
+       hunk_header = lnstate.stagebutton + hunk_header;
 
-               var hunk_header = '<span class="hunk_stats">@@ -' + h.range.old.start + ',' + 
h.range.old.lines + ' +' + h.range.new.start + ',' + h.range.new.lines + ' @@</span>';
+       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_header + '</td> \
+       </tr>';
 
-               hunk_header = lnstate.stagebutton + hunk_header;
+       var row, line, proc;
+       var cold = hunk.range.old.start;
+       var cnew = hunk.range.new.start;
+       for (var i = 0; i < hunk.lines.length; ++i)
+       {
+               row = '<tr class="';
 
-               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_header + '</td> \
-               </tr>';
+               line = hunk.lines[i];
 
-               var l, row, proc;
-               var cold = h.range.old.start;
-               var cnew = h.range.new.start;
-               for (var j = 0; j < h.lines.length; ++j)
+               switch (String.fromCharCode(line.type))
                {
-                       l = h.lines[j];
-
-                       row = '<tr class="';
+                       case ' ':
+                               row += 'context"> \
+                                       <td class="gutter old">' + cold + '</td> \
+                                       <td class="gutter new">' + cnew + '</td>';
 
-                       switch (String.fromCharCode(l.type))
-                       {
-                               case ' ':
-                                       row += 'context"> \
-                                               <td class="gutter old">' + cold + '</td> \
-                                               <td class="gutter new">' + cnew + '</td>';
-
-                                       cold++;
-                                       cnew++;
+                               cold++;
+                               cnew++;
                                break;
-                               case '+':
-                                       row += 'added"> \
-                                               <td class="gutter old"></td> \
-                                               <td class="gutter new">' + cnew + '</td>';
+                       case '+':
+                               row += 'added"> \
+                                       <td class="gutter old"></td> \
+                                       <td class="gutter new">' + cnew + '</td>';
 
-                                       cnew++;
-                                       added++;
+                               cnew++;
+                               lnstate.added++;
                                break;
-                               case '-':
-                                       row += 'removed"> \
-                                               <td class="gutter old">' + cold + '</td> \
-                                               <td class="gutter new"></td>';
+                       case '-':
+                               row += 'removed"> \
+                                       <td class="gutter old">' + cold + '</td> \
+                                       <td class="gutter new"></td>';
 
-                                       cold++;
-                                       removed++;
+                               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);
+                       case '=':
+                       case '>':
+                       case '<':
+                               row += 'context"> \
+                                       <td class="gutter old"></td> \
+                                       <td class="gutter new"></td>';
+                                       line.content = line.content.substr(1, line.content.length);
                                break;
-                               default:
-                                       row += '">';
+                       default:
+                               row += '">';
                                break;
-                       }
+               }
 
-                       l.content = html_escape(l.content).replace(/\t/g, '<span class="tab" style="width: ' 
+ data.settings.tab_width + 'ex">\t</span>');
+               line.content = html_escape(line.content).replace(/\t/g, '<span class="tab" style="width: ' + 
data.settings.tab_width + 'ex">\t</span>');
 
-                       row += '<td class="code">' + l.content + '</td>';
+               row += '<td class="code">' + line.content + '</td>';
 
-                       row += '</tr>';
+               row += '</tr>';
 
-                       file_body += row;
+               hunk_body += row;
 
-                       lnstate.processed++;
+               lnstate.processed++;
 
-                       proc = lnstate.processed / lnstate.lines;
+               proc = lnstate.processed / lnstate.lines;
 
-                       if (proc >= lnstate.nexttick)
-                       {
-                               self.postMessage({tick: proc});
+               if (proc >= lnstate.nexttick)
+               {
+                       self.postMessage({tick: proc});
 
-                               while (proc >= lnstate.nexttick)
-                               {
-                                       lnstate.nexttick += lnstate.tickfreq;
-                               }
+                       while (proc >= lnstate.nexttick)
+                       {
+                               lnstate.nexttick += lnstate.tickfreq;
                        }
                }
        }
 
+       return hunk_body;
+}
+
+function diff_file(file, lnstate, data)
+{
+       lnstate.added = 0;
+       lnstate.removed = 0;
+
+       var file_body = '';
+
+       for (var i = 0; i < file.hunks.length; ++i)
+       {
+               file_body += diff_hunk(file.hunks[i], lnstate, data);
+       }
+
        var file_path;
 
        if (file.file.new.path)
@@ -107,11 +114,11 @@ function diff_file(file, lnstate, data)
                file_path = file.file.old.path;
        }
 
-       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;
 


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