- commit
- 0e5a89bf14522d6a80768a9d011ef5c8abc8230e
- parent
- b7e6a8da035d65ad79ffd8bfd914c7ab4f20da6b
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-05-08 20:38
refactor applyExact
Diffstat
M | static/context.js | 42 | +++++++++++++++++++----------------------- |
1 files changed, 19 insertions, 23 deletions
diff --git a/static/context.js b/static/context.js
@@ -1,17 +1,13 @@1 -1 var applyExact = function(text, [pos, before, after], selection) {2 -1 if (pos >= 0 && text.slice(pos).startsWith(before)) {3 -1 if (selection) {4 -1 if (pos < selection[0]) {5 -1 selection[0] += after.length - before.length;6 -1 }7 -1 if (pos < selection[1]) {8 -1 selection[1] += after.length - before.length;9 -1 }-1 1 var _apply = function(text, [pos, before, after], selection) { -1 2 if (selection) { -1 3 if (pos < selection[0]) { -1 4 selection[0] += after.length - before.length; -1 5 } -1 6 if (pos < selection[1]) { -1 7 selection[1] += after.length - before.length; 10 8 }11 -1 return text.slice(0, pos) + after + text.slice(pos + before.length);12 -1 } else {13 -1 throw 'no match';14 9 } -1 10 return text.slice(0, pos) + after + text.slice(pos + before.length); 15 11 }; 16 12 17 13 var fuzzyMerge = function(s1, s2) { @@ -38,9 +34,9 @@ var fuzzyMerge = function(s1, s2) { 38 34 39 35 export var apply = function(text, [pos, before, after], selection) { 40 36 // try exact match41 -1 try {42 -1 return applyExact(text, [pos, before, after], selection);43 -1 } catch (e) {}-1 37 if (text.slice(pos).startsWith(before)) { -1 38 return _apply(text, [pos, before, after], selection); -1 39 } 44 40 45 41 // try exact match in similar position 46 42 var best = -1; @@ -55,14 +51,14 @@ export var apply = function(text, [pos, before, after], selection) { 55 51 } 56 52 } 57 53 if (best !== -1) {58 -1 return applyExact(text, [best, before, after], selection);-1 54 return _apply(text, [best, before, after], selection); 59 55 } 60 56 61 57 // fall back to fuzzy merge 62 58 var ctxLen = 3; 63 59 var ctxStart = Math.max(pos - ctxLen, 0); 64 60 var ctx = text.slice(ctxStart, pos + before.length + ctxLen);65 -1 return applyExact(text, [ctxStart, ctx, fuzzyMerge(after, ctx)], selection);-1 61 return _apply(text, [ctxStart, ctx, fuzzyMerge(after, ctx)], selection); 66 62 }; 67 63 68 64 export var diff = function(text1, text2, ctx) { @@ -87,16 +83,16 @@ export var diff = function(text1, text2, ctx) { 87 83 88 84 export var merge = function([pos1, before1, after1], [pos2, before2, after2]) { 89 85 // merge subsequent inserts90 -1 try {91 -1 var after = applyExact(after1, [pos2 - pos1, before2, after2]);-1 86 if (pos2 >= pos1 && after1.slice(pos2 - pos1).startsWith(before2)) { -1 87 var after = _apply(after1, [pos2 - pos1, before2, after2]); 92 88 return [[pos1, before1, after]];93 -1 } catch (e) {}-1 89 } 94 90 95 91 // merge subsequent deletes (inverse insert)96 -1 try {97 -1 var before = applyExact(before2, [pos1 - pos2, after1, before1]);-1 92 if (pos1 >= pos2 && before2.slice(pos1 - pos2).startsWith(after1)) { -1 93 var before = _apply(before2, [pos1 - pos2, after1, before1]); 98 94 return [[pos2, before, after2]];99 -1 } catch (e) {}-1 95 } 100 96 101 97 return [ 102 98 [pos1, before1, after1],