Skip to content

Commit 662ef59

Browse files
committed
- fix handling of insertions when there is nothing left in the lines
array. - add some more test cases
1 parent 8186874 commit 662ef59

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

src/node/easysync_tests.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,28 @@ function runTests() {
223223
], ["banana\n", "cabbage\n", "duffle\n"]);
224224

225225
// #2836 regressions
226-
runMutationTest(8, ["\n"], [
226+
runMutationTest(8, ["\n","foo\n","\n"], [
227+
['remove', 1, 1, "\n"],
228+
['skip', 4, 1, false],
229+
['remove', 1, 1, "\n"],
230+
['insert',"c"]
231+
], ["foo\n","c"]);
232+
runMutationTest(9, ["\n","foo\n","\n"], [
233+
['remove', 1, 1, "\n"],
234+
['skip', 3, 0, false],
235+
['remove', 2, 2, "\n\n"],
236+
['insert',"c"]
237+
], ["fooc"]);
238+
runMutationTest(10, ["\n"], [
227239
['remove', 1, 1, "\n"],
228240
['insert',"c", 0]
229-
], ["c"]);
230-
runMutationTest(9, ["\n"], [
241+
], ["c"]); //TODO find out if c must have a newline because of unknown constraints
242+
runMutationTest(11, ["\n"], [
231243
['remove', 1, 1, "\n"],
232244
['insert', "a"],
233245
['insert',"c\n", 1]
234246
], ["ac\n"]);
235-
runMutationTest(10, ["\n"], [
247+
runMutationTest(12, ["\n"], [
236248
['remove', 1, 1, "\n"],
237249
['insert', "a\n", 1],
238250
['insert',"c"]

src/static/js/Changeset.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,14 @@ exports.textLinesMutator = function (lines) {
802802
curSplice[1] += L - 1;
803803
var sline = curSplice.length - 1;
804804
removed = curSplice[sline].substring(curCol) + removed;
805-
curSplice[sline] = curSplice[sline].substring(0, curCol) + lines_get(curSplice[0] + curSplice[1]);
806-
curSplice[1] += 1;
805+
var line = lines_get(curSplice[0] + curSplice[1]);
806+
// if no line follows the splice
807+
if (!line) {
808+
curSplice[sline] = curSplice[sline].substring(0, curCol);
809+
} else {
810+
curSplice[sline] = curSplice[sline].substring(0, curCol) + line;
811+
curSplice[1] += 1;
812+
}
807813
}
808814
} else {
809815
removed = nextKLinesText(L);
@@ -854,6 +860,7 @@ exports.textLinesMutator = function (lines) {
854860
if (L) {
855861
var newLines = exports.splitTextLines(text);
856862
if (isCurLineInSplice()) {
863+
//TODO(doc) is this relevant?
857864
//if (curCol == 0) {
858865
//curSplice.length--;
859866
//curSplice[1]--;
@@ -864,22 +871,47 @@ exports.textLinesMutator = function (lines) {
864871
var sline = curSplice.length - 1;
865872
var theLine = curSplice[sline];
866873
var lineCol = curCol;
874+
// insert the first new line
867875
curSplice[sline] = theLine.substring(0, lineCol) + newLines[0];
868876
curLine++;
869877
newLines.splice(0, 1);
878+
// insert the remaining new lines
870879
Array.prototype.push.apply(curSplice, newLines);
871880
curLine += newLines.length;
872-
curSplice.push(theLine.substring(lineCol));
873-
curCol = 0;
881+
// insert the remaining chars from the "old" line (e.g. the line we were in
882+
// when we started to insert new lines)
883+
// if nothing is left we don't push an empty string
884+
if (theLine.substring(lineCol)) {
885+
curSplice.push(theLine.substring(lineCol));
886+
}
887+
curCol = 0; // TODO(doc) why is this not set to the length of last line?
874888
//}
875889
} else {
876890
Array.prototype.push.apply(curSplice, newLines);
877891
curLine += newLines.length;
878892
}
879893
} else {
880-
var sline = putCurLineInSplice();
881-
curSplice[sline] = curSplice[sline].substring(0, curCol) + text + curSplice[sline].substring(curCol);
882-
curCol += text.length;
894+
// there are no additional lines
895+
if (lines_get(curSplice[0] + curSplice[1]) === undefined) {
896+
// find out if there is a line in splice that is not finished processing
897+
// if yes, we can add our text to it
898+
if (isCurLineInSplice()) {
899+
var sline = curSplice.length - 1;
900+
curSplice[sline] = curSplice[sline].substring(0, curCol) + text + curSplice[sline].substring(curCol);
901+
curCol += text.length;
902+
}
903+
// if no, we need to add the text in a new line
904+
else {
905+
Array.prototype.push.apply(curSplice, [text]);
906+
curCol += text.length;
907+
}
908+
} else {
909+
// although the line is put into splice, curLine is not increased, because
910+
// there may be more chars in the line (newline is not reached)
911+
var sline = putCurLineInSplice();
912+
curSplice[sline] = curSplice[sline].substring(0, curCol) + text + curSplice[sline].substring(curCol);
913+
curCol += text.length;
914+
}
883915
}
884916
//debugPrint("insert");
885917
}

0 commit comments

Comments
 (0)