- commit
- d4bfba05ce621eec9075eec8c408294b50d3121f
- parent
- bdefa9a6250caa2bfe911e8bcd04ebf2d263c792
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-05-09 09:42
include end offset in diff
Diffstat
M | static/context.js | 51 | +++++++++++++++++++++++++-------------------------- |
1 files changed, 25 insertions, 26 deletions
diff --git a/static/context.js b/static/context.js
@@ -33,17 +33,17 @@ var fuzzyMerge = function(s1, s2) { 33 33 return ret; 34 34 }; 35 3536 -1 export var apply = function(text, [pos, before, after], selection) {-1 36 export var apply = function(text, [start, end, before, after], selection) { 37 37 // try exact match38 -1 if (text.slice(pos).startsWith(before)) {39 -1 return _apply(text, [pos, before, after], selection);-1 38 if (text.slice(start).startsWith(before)) { -1 39 return _apply(text, [start, before, after], selection); 40 40 } 41 41 42 42 // try exact match in similar position 43 43 var best = -1; 44 44 var bestDist = Infinity; 45 45 for (var i = text.indexOf(before); i !== -1; i = text.indexOf(before, i + 1)) {46 -1 var dist = Math.abs(i - pos);-1 46 var dist = Math.abs(i - start); 47 47 if (dist < bestDist) { 48 48 best = i; 49 49 bestDist = dist; @@ -57,47 +57,46 @@ export var apply = function(text, [pos, before, after], selection) { 57 57 58 58 // fall back to fuzzy merge 59 59 var ctxLen = 3;60 -1 var ctxStart = Math.max(pos - ctxLen, 0);61 -1 var ctx = text.slice(ctxStart, pos + before.length + ctxLen);-1 60 var ctxStart = Math.max(start - ctxLen, 0); -1 61 var ctx = text.slice(ctxStart, start + before.length + ctxLen); 62 62 return _apply(text, [ctxStart, ctx, fuzzyMerge(after, ctx)], selection); 63 63 }; 64 64 65 65 export var diff = function(text1, text2, ctx) { 66 66 var start = 0;67 -1 var end1 = text1.length;68 -1 var end2 = text2.length;-1 67 var end = 0; 69 68 70 69 while (start < text1.length && start < text2.length && text1[start] === text2[start]) { 71 70 start += 1; 72 71 }73 -1 while (end1 > start && end2 > start && text1[end1 - 1] === text2[end2 - 1]) {74 -1 end1 -= 1;75 -1 end2 -= 1;-1 72 while (start + end <= text1.length && start + end <= text2.length && text1.slice(-end || Infinity) === text2.slice(-end || Infinity)) { -1 73 end += 1; 76 74 } 77 75 78 76 start = Math.max(0, start - ctx);79 -1 end1 += ctx;80 -1 end2 += ctx;-1 77 end = Math.max(0, end - ctx); 81 7882 -1 return [start, text1.slice(start, end1), text2.slice(start, end2)];-1 79 return [start, end, text1.slice(start, -end || Infinity), text2.slice(start, -end || Infinity)]; 83 80 }; 84 8185 -1 export var merge = function([pos1, before1, after1], [pos2, before2, after2]) {86 -1 // merge subsequent inserts87 -1 if (pos2 >= pos1 && after1.slice(pos2 - pos1).startsWith(before2)) {88 -1 var after = _apply(after1, [pos2 - pos1, before2, after2]);89 -1 return [[pos1, before1, after]];90 -1 }-1 82 export var merge = function([start1, end1, before1, after1], [start2, end2, before2, after2]) { -1 83 if (start1 + after1.length + end1 === start2 + before2.length + end2) { -1 84 // merge subsequent inserts -1 85 if (start2 >= start1 && after1.slice(start2 - start1).startsWith(before2)) { -1 86 var after = _apply(after1, [start2 - start1, before2, after2]); -1 87 return [[start1, end1, before1, after]]; -1 88 } 91 8992 -1 // merge subsequent deletes (inverse insert)93 -1 if (pos1 >= pos2 && before2.slice(pos1 - pos2).startsWith(after1)) {94 -1 var before = _apply(before2, [pos1 - pos2, after1, before1]);95 -1 return [[pos2, before, after2]];-1 90 // merge subsequent deletes (inverse insert) -1 91 if (start1 >= start2 && before2.slice(start1 - start2).startsWith(after1)) { -1 92 var before = _apply(before2, [start1 - start2, after1, before1]); -1 93 return [[start2, end2, before, after2]]; -1 94 } 96 95 } 97 96 98 97 return [99 -1 [pos1, before1, after1],100 -1 [pos2, before2, after2],-1 98 [start1, end1, before1, after1], -1 99 [start2, end2, before2, after2], 101 100 ]; 102 101 }; 103 102