[bugzilla-gnome-org-extensions] Fix line number interpretation



commit 3945c9755621a86a765ef490eeb2c226e6bbc2b8
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Wed Sep 9 00:02:09 2009 -0400

    Fix line number interpretation
    
    Make consistent that we increment the line count after the line,
    not before. So:
    
     1 1 2
       *
    
    Not:
    
     1 2 2
       *
    
    This gives a simple interpretation that oldStart,newStart for the hunk
    are the line numbers of the first line of the hunk.

 js/patch.js      |    6 +++---
 js/review.js     |   22 ++++++++++++++++++++--
 tests/review.jst |    4 ++--
 3 files changed, 25 insertions(+), 7 deletions(-)
---
diff --git a/js/patch.js b/js/patch.js
index 4614747..ee81ee7 100644
--- a/js/patch.js
+++ b/js/patch.js
@@ -158,15 +158,15 @@ Hunk.prototype = {
     },
 
     iterate : function(cb) {
-        var oldLine = this.oldStart - 1;
-        var newLine = this.newStart - 1;
+        var oldLine = this.oldStart;
+        var newLine = this.newStart;
         for (var i = 0; i < this.lines.length; i++) {
             var line = this.lines[i];
+            cb(this.location + i, oldLine, line[0], newLine, line[1], line[2], line);
             if (line[0] != null)
                 oldLine++;
             if (line[1] != null)
                 newLine++;
-            cb(this.location + i, oldLine, line[0], newLine, line[1], line[2], line);
         }
     }
 };
diff --git a/js/review.js b/js/review.js
index 80743d6..39a0689 100644
--- a/js/review.js
+++ b/js/review.js
@@ -223,8 +223,26 @@ Review.prototype = {
 
                 var hunk = new Patch.Hunk(oldStart, oldCount, newStart, newCount, m2[5], true);
 
-                var location = file.patchFile.getLocation(hunk.oldStart + hunk.oldCount - 1,
-                                                          hunk.newStart + hunk.newCount - 1);
+                // Numbering of old/new line numbers is
+                //
+                //     A             B
+                // 1 1 2 3 3 4 5 5 5 6 7
+                //   * *   * *     * * *
+                //
+                // Where the * represent lines actually in that version of the file.
+                // So, the difference in line numbers between A and B is the number of
+                // *'s from A to B, not including B.
+
+                var oldLine = hunk.oldStart + hunk.oldCount;
+                var newLine = hunk.newStart + hunk.newCount;
+
+                var lastLine = hunk.lines[hunk.lines.length - 1];
+                if (lastLine[0] != null)
+                    oldLine--;
+                if (lastLine[1] != null)
+                    newLine--;
+
+                var location = file.patchFile.getLocation(oldLine, newLine);
                 file.addComment(location, Utils.strip(hunk.comment));
             }
 
diff --git a/tests/review.jst b/tests/review.jst
index b662688..3bc3edf 100644
--- a/tests/review.jst
+++ b/tests/review.jst
@@ -52,7 +52,7 @@ I like this patch
, r.toString());
 
 let argC = r.getFile('gi/arg.c');
-let loc = argC.patchFile.getLocation(216,214);
+let loc = argC.patchFile.getLocation(216,215);
 r.getFile('gi/arg.c').addComment(loc, 'Should you have removed elem?');
 assertEquals(<<<
 I like this patch
@@ -122,7 +122,7 @@ let r2 = new Review.Review(p);
 r2.parse(r.toString());
 assertEquals(r.toString(), r2.toString());
 
-loc = argC.patchFile.getLocation(216,214);
+loc = argC.patchFile.getLocation(216,215);
 let comment = argC.getComment(loc);
 assertEquals(loc, comment.location);
 comment.remove();


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