Permalink
Browse files

Fix EOFNL implementation

  • Loading branch information...
1 parent a4b5c6c commit a9bcfdbc4b2c2bf4fbb557a341fa43b72dd346a8 @kpdecker kpdecker committed Dec 24, 2011
Showing with 57 additions and 22 deletions.
  1. +15 −8 diff.js
  2. +42 −14 test/diffTest.js
View
@@ -199,6 +199,16 @@ var JsDiff = (function() {
function contextLines(lines) {
return lines.map(function(entry) { return ' ' + entry; });
}
+ function eofNL(curRange, i, current) {
+ var last = diff[diff.length-2],
+ isLast = i === diff.length-2,
+ isLastOfType = i === diff.length-3 && (current.added === !last.added || current.removed === !last.removed);
+
+ // Figure out if this is the last line for the given file and missing NL
+ if (!/\n$/.test(current.value) && (isLast || isLastOfType)) {
+ curRange.push('\\ No newline at end of file');
+ }
+ }
var oldRangeStart = 0, newRangeStart = 0, curRange = [],
oldLine = 1, newLine = 1;
@@ -220,13 +230,7 @@ var JsDiff = (function() {
}
}
curRange.push.apply(curRange, lines.map(function(entry) { return (current.added?"+":"-") + entry; }));
-
- // Figure out if this is the last line for the given file and missing NL
- if (!/\n$/.test(current.value)
- && (i === diff.length-2
- || (i === diff.length-3 && current.added === !diff[diff.length-2].added))) {
- curRange.push('\\ No newline at end of file');
- }
+ eofNL(curRange, i, current);
if (current.added) {
newLine += lines.length;
@@ -236,7 +240,7 @@ var JsDiff = (function() {
} else {
if (oldRangeStart) {
// Close out any changes that have been output (or join overlapping)
- if (lines.length <= 8 && i < diff.length-1) {
+ if (lines.length <= 8 && i < diff.length-2) {
// Overlapping
curRange.push.apply(curRange, contextLines(lines));
} else {
@@ -248,6 +252,9 @@ var JsDiff = (function() {
+ " @@");
ret.push.apply(ret, curRange);
ret.push.apply(ret, contextLines(lines.slice(0, contextSize)));
+ if (lines.length <= 4) {
+ eofNL(ret, i, current);
+ }
oldRangeStart = 0; newRangeStart = 0; curRange = [];
}
View
@@ -163,6 +163,35 @@ exports['lastLineChanged'] = function() {
+ ' line1\n'
+ ' line2\n'
+ ' line3\n'
+ + '+line44\n'
+ + '-line4\n',
+ diff.createPatch('test', 'line1\nline2\nline3\nline4\n', 'line1\nline2\nline3\nline44\n', 'header1', 'header2'));
+
+ assert.eql(
+ 'Index: test\n'
+ + '===================================================================\n'
+ + '--- test\theader1\n'
+ + '+++ test\theader2\n'
+ + '@@ -1,4 +1,5 @@\n'
+ + ' line1\n'
+ + ' line2\n'
+ + ' line3\n'
+ + '+line44\n'
+ + '+line5\n'
+ + '-line4\n',
+ diff.createPatch('test', 'line1\nline2\nline3\nline4\n', 'line1\nline2\nline3\nline44\nline5\n', 'header1', 'header2'));
+};
+
+exports['EOFNL'] = function() {
+ assert.eql(
+ 'Index: test\n'
+ + '===================================================================\n'
+ + '--- test\theader1\n'
+ + '+++ test\theader2\n'
+ + '@@ -1,4 +1,4 @@\n'
+ + ' line1\n'
+ + ' line2\n'
+ + ' line3\n'
+ '+line4\n'
+ '\\ No newline at end of file\n'
+ '-line4\n',
@@ -188,26 +217,27 @@ exports['lastLineChanged'] = function() {
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,4 +1,4 @@\n'
- + ' line1\n'
+ + '+line1\n'
+ + '-line11\n'
+ ' line2\n'
+ ' line3\n'
- + '+line44\n'
- + '-line4\n',
- diff.createPatch('test', 'line1\nline2\nline3\nline4\n', 'line1\nline2\nline3\nline44\n', 'header1', 'header2'));
+ + ' line4\n'
+ + '\\ No newline at end of file\n',
+ diff.createPatch('test', 'line11\nline2\nline3\nline4', 'line1\nline2\nline3\nline4', 'header1', 'header2'));
assert.eql(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
- + '@@ -1,4 +1,5 @@\n'
- + ' line1\n'
+ + '@@ -1,5 +1,5 @@\n'
+ + '+line1\n'
+ + '-line11\n'
+ ' line2\n'
+ ' line3\n'
- + '+line44\n'
- + '+line5\n'
- + '-line4\n',
- diff.createPatch('test', 'line1\nline2\nline3\nline4\n', 'line1\nline2\nline3\nline44\nline5\n', 'header1', 'header2'));
+ + ' line4\n'
+ + ' line4\n',
+ diff.createPatch('test', 'line11\nline2\nline3\nline4\nline4\nline4\nline4', 'line1\nline2\nline3\nline4\nline4\nline4\nline4', 'header1', 'header2'));
};
exports['Large Test'] = function() {
@@ -571,15 +601,13 @@ exports['Patch'] = function() {
diffResult = diff.createPatch("testFileName", oldFile, newFile, "Old Header", "New Header");
assert.eql(
expectedResult,
- diffResult,
- "Patch diffResult Value");
+ diffResult);
expectedResult =
"Index: testFileName\n"
+ "===================================================================\n"
+ "--- testFileName\tOld Header\n"
- + "+++ testFileName\tNew Header\n"
- + "\\ No newline at end of file\n";
+ + "+++ testFileName\tNew Header\n";
diffResult = diff.createPatch("testFileName", oldFile, oldFile, "Old Header", "New Header");
assert.eql(
expectedResult,

0 comments on commit a9bcfdb

Please sign in to comment.