[extensions-web] review: Add an "index" field for the JS to use



commit b94437b05ad64cef6f87845c8f2110f5cd1ca09c
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Jun 26 13:12:11 2012 -0400

    review: Add an "index" field for the JS to use
    
    So we don't get tripped up on these off-by-one errors.

 sweettooth/review/diffutils.py |   13 +++++++++----
 sweettooth/static/js/diff.js   |    8 +++-----
 2 files changed, 12 insertions(+), 9 deletions(-)
---
diff --git a/sweettooth/review/diffutils.py b/sweettooth/review/diffutils.py
index c716222..9d4a8fe 100644
--- a/sweettooth/review/diffutils.py
+++ b/sweettooth/review/diffutils.py
@@ -757,17 +757,24 @@ def new_chunk(lines, collapsable=False, tag='equal', meta=None):
 def get_fake_chunk(numlines, tag):
     linenums = xrange(numlines)
     lines = [dict(oldlinenum=n+1, newlinenum=n+1,
+                  oldindex=n, newindex=n,
                   oldregion=[], newregion=[]) for n in linenums]
     return new_chunk(lines, tag=tag)
 
 def get_chunks(a, b):
-    def diff_line(oldlinenum, newlinenum, oldline, newline):
+    def diff_line(old, new):
+        oldindex, oldline = old
+        newindex, newline = new
+        oldlinenum = oldindex + 1
+        newlinenum = newindex + 1
+
         if oldline and newline and oldline != newline:
             oldregion, newregion = get_line_changed_regions(oldline, newline)
         else:
             oldregion = newregion = []
 
         result = dict(oldlinenum=oldlinenum, newlinenum=newlinenum,
+                      oldindex=oldindex, newindex=newindex,
                       oldregion=oldregion, newregion=newregion)
 
         return result
@@ -794,9 +801,7 @@ def get_chunks(a, b):
     for tag, i1, i2, j1, j2, meta in opcodes_with_metadata(differ):
         numlines = max(i2-i1, j2-j1)
 
-        lines = map(diff_line,
-                    xrange(i1 + 1, i2 + 1), xrange(j1 + 1, j2 + 1),
-                    a[i1:i2], b[j1:j2])
+        lines = [diff_line(old, new) for old, new in zip(zip(xrange(i1, i2), a), zip(xrange(j1, j2), b))]
 
         if tag == 'equal' and numlines > collapse_threshold:
             last_range_start = numlines - context_num_lines
diff --git a/sweettooth/static/js/diff.js b/sweettooth/static/js/diff.js
index 1cce33e..ba637a1 100644
--- a/sweettooth/static/js/diff.js
+++ b/sweettooth/static/js/diff.js
@@ -45,7 +45,7 @@ define(['jquery'], function($) {
         var collapsed = false;
 
         var $elems = $.map(chunk.lines, function(line, i) {
-            var contents = oldContents[line.oldlinenum - 1];
+            var contents = oldContents[line.oldindex];
 
             var $row = $('<tr>', {'class': 'diff-line equal'}).
                 append($('<td>', {'class': 'old linum'}).text(line.oldlinenum)).
@@ -126,12 +126,11 @@ define(['jquery'], function($) {
     }
 
     function buildInsertLine(line, contents) {
-        var content = contents[line.newlinenum - 1];
         return $('<tr>', {'class': 'diff-line inserted'}).
             append($('<td>', {'class': 'linum'})).
             append($('<td>', {'class': 'new linum'}).text(line.newlinenum)).
             append($('<td>', {'class': 'new contents'}).
-                     append(flatten(buildReplaceRegions(line.newregion, content))));
+                     append(flatten(buildReplaceRegions(line.newregion, contents[line.newindex]))));
     }
 
     function buildInsertChunk(chunk, oldContents, newContents) {
@@ -139,12 +138,11 @@ define(['jquery'], function($) {
     }
 
     function buildDeleteLine(line, contents) {
-        var content = contents[line.oldlinenum];
         return $('<tr>', {'class': 'diff-line deleted'}).
             append($('<td>', {'class': 'old linum'}).text(line.oldlinenum)).
             append($('<td>', {'class': 'linum'})).
             append($('<td>', {'class': 'old contents'}).
-                   append(flatten(buildReplaceRegions(line.oldregion, content))));
+                   append(flatten(buildReplaceRegions(line.oldregion, contents[line.oldindex]))));
     }
 
     function buildDeleteChunk(chunk, oldContents, newContents) {



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