Skip to content

Commit ae347be

Browse files
committed
Merge pull request #80 from rtfpessoa/improve-paths
Improve paths
2 parents 146d951 + ce90977 commit ae347be

File tree

3 files changed

+95
-11
lines changed

3 files changed

+95
-11
lines changed

sample/index.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
Author: rtfpessoa
1010
-->
1111

12-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/styles/github.min.css">
12+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/styles/github.min.css">
1313

1414
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js"></script>
1515

16-
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/highlight.min.js"></script>
17-
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/languages/scala.min.js"></script>
16+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/highlight.min.js"></script>
17+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/languages/scala.min.js"></script>
1818

1919
<!-- diff2html -->
2020
<link rel="stylesheet" type="text/css" href="../dist/diff2html.css">
@@ -24,6 +24,12 @@
2424

2525
<script>
2626
var lineDiffExample =
27+
"--- a/src/my/really/big/path/sample.js\n" +
28+
"+++ b/src/my/small/path/sample.js\n" +
29+
"@@ -1 +1,2 @@\n" +
30+
"-test\n" +
31+
"+test1r\n" +
32+
"+test2r\n" +
2733
'diff --git a/src/core/init.java b/src/core/init.java\n' +
2834
'index e49196a..50f310c 100644\n' +
2935
'--- a/src/core/init.java\n' +

src/printer-utils.js

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
var utils = require('./utils.js').Utils;
1212
var Rematch = require('./rematch.js').Rematch;
1313

14+
var separator = '/';
15+
1416
function PrinterUtils() {
1517
}
1618

@@ -32,18 +34,65 @@
3234
};
3335

3436
PrinterUtils.prototype.getDiffName = function(file) {
35-
var oldFilename = file.oldName;
36-
var newFilename = file.newName;
37+
var oldFilename = unifyPath(file.oldName);
38+
var newFilename = unifyPath(file.newName);
3739

3840
if (oldFilename && newFilename && oldFilename !== newFilename && !isDevNullName(oldFilename) && !isDevNullName(newFilename)) {
39-
return oldFilename + ' -> ' + newFilename;
41+
var prefixPaths = [];
42+
var suffixPaths = [];
43+
44+
var oldFilenameParts = oldFilename.split(separator);
45+
var newFilenameParts = newFilename.split(separator);
46+
47+
var oldFilenamePartsSize = oldFilenameParts.length;
48+
var newFilenamePartsSize = newFilenameParts.length;
49+
50+
var i = 0;
51+
var j = oldFilenamePartsSize - 1;
52+
var k = newFilenamePartsSize - 1;
53+
54+
while (i < j && i < k) {
55+
if (oldFilenameParts[i] === newFilenameParts[i]) {
56+
prefixPaths.push(newFilenameParts[i]);
57+
i += 1;
58+
} else {
59+
break;
60+
}
61+
}
62+
63+
while (j > i && k > i) {
64+
if (oldFilenameParts[j] === newFilenameParts[k]) {
65+
suffixPaths.unshift(newFilenameParts[k]);
66+
j -= 1;
67+
k -= 1;
68+
} else {
69+
break;
70+
}
71+
}
72+
73+
var finalPrefix = prefixPaths.join(separator);
74+
var finalSuffix = suffixPaths.join(separator);
75+
76+
var oldRemainingPath = oldFilenameParts.slice(i, j + 1).join(separator);
77+
var newRemainingPath = newFilenameParts.slice(i, k + 1).join(separator);
78+
79+
if (finalPrefix.length && finalSuffix.length) {
80+
return finalPrefix + separator + '{' + oldRemainingPath + ' → ' + newRemainingPath + '}' + separator + finalSuffix;
81+
} else if (finalPrefix.length) {
82+
return finalPrefix + separator + '{' + oldRemainingPath + ' → ' + newRemainingPath + '}';
83+
} else if (finalSuffix.length) {
84+
return '{' + oldRemainingPath + ' → ' + newRemainingPath + '}' + separator + finalSuffix;
85+
}
86+
87+
return oldFilename + ' → ' + newFilename;
88+
4089
} else if (newFilename && !isDevNullName(newFilename)) {
4190
return newFilename;
4291
} else if (oldFilename) {
4392
return oldFilename;
4493
}
4594

46-
return 'Unknown filename';
95+
return 'unknown/file/path';
4796
};
4897

4998
PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) {
@@ -128,6 +177,14 @@
128177
};
129178
};
130179

180+
function unifyPath(path) {
181+
if (path) {
182+
return path.replace('\\', '/');
183+
}
184+
185+
return path;
186+
}
187+
131188
function isDevNullName(name) {
132189
return name.indexOf('dev/null') !== -1;
133190
}

test/printer-utils-tests.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,33 @@ describe('Utils', function() {
2828
});
2929
assert.equal('sample.js', result);
3030
});
31-
it('should generate the file name for a changed file and renamed', function() {
31+
it('should generate the file name for a changed file and full rename', function() {
3232
var result = PrinterUtils.getDiffName({
3333
oldName: 'sample1.js',
3434
newName: 'sample2.js'
3535
});
36-
assert.equal('sample1.js -> sample2.js', result);
36+
assert.equal('sample1.js → sample2.js', result);
37+
});
38+
it('should generate the file name for a changed file and prefix rename', function() {
39+
var result = PrinterUtils.getDiffName({
40+
oldName: 'src/path/sample.js',
41+
newName: 'source/path/sample.js'
42+
});
43+
assert.equal('{src → source}/path/sample.js', result);
44+
});
45+
it('should generate the file name for a changed file and suffix rename', function() {
46+
var result = PrinterUtils.getDiffName({
47+
oldName: 'src/path/sample1.js',
48+
newName: 'src/path/sample2.js'
49+
});
50+
assert.equal('src/path/{sample1.js → sample2.js}', result);
51+
});
52+
it('should generate the file name for a changed file and middle rename', function() {
53+
var result = PrinterUtils.getDiffName({
54+
oldName: 'src/really/big/path/sample.js',
55+
newName: 'src/small/path/sample.js'
56+
});
57+
assert.equal('src/{really/big → small}/path/sample.js', result);
3758
});
3859
it('should generate the file name for a deleted file', function() {
3960
var result = PrinterUtils.getDiffName({
@@ -49,9 +70,9 @@ describe('Utils', function() {
4970
});
5071
assert.equal('src/my/file.js', result);
5172
});
52-
it('should generate handle undefined filenames', function() {
73+
it('should generate handle undefined filename', function() {
5374
var result = PrinterUtils.getDiffName({});
54-
assert.equal('Unknown filename', result);
75+
assert.equal('unknown/file/path', result);
5576
});
5677
});
5778

0 commit comments

Comments
 (0)