Skip to content

Commit 9b295f3

Browse files
webzwo0irhansen
authored andcommitted
- fix handling of insertions when there is nothing left in the lines
array. - add some more test cases
1 parent 8af7834 commit 9b295f3

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/node/easysync_tests.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,28 @@ const runTests = () => {
218218
], ['banana\n', 'cabbage\n', 'duffle\n']);
219219

220220
// #2836 regressions
221-
runMutationTest(8, ['\n'], [
221+
runMutationTest(8, ['\n', 'foo\n', '\n'], [
222+
['remove', 1, 1, '\n'],
223+
['skip', 4, 1, false],
224+
['remove', 1, 1, '\n'],
225+
['insert', 'c'],
226+
], ['foo\n', 'c']);
227+
runMutationTest(9, ['\n', 'foo\n', '\n'], [
228+
['remove', 1, 1, '\n'],
229+
['skip', 3, 0, false],
230+
['remove', 2, 2, '\n\n'],
231+
['insert', 'c'],
232+
], ['fooc']);
233+
runMutationTest(10, ['\n'], [
222234
['remove', 1, 1, '\n'],
223235
['insert', 'c', 0],
224-
], ['c']);
225-
runMutationTest(9, ['\n'], [
236+
], ['c']); // TODO find out if c must have a newline because of unknown constraints
237+
runMutationTest(11, ['\n'], [
226238
['remove', 1, 1, '\n'],
227239
['insert', 'a'],
228240
['insert', 'c\n', 1],
229241
], ['ac\n']);
230-
runMutationTest(10, ['\n'], [
242+
runMutationTest(12, ['\n'], [
231243
['remove', 1, 1, '\n'],
232244
['insert', 'a\n', 1],
233245
['insert', 'c'],

src/static/js/Changeset.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,14 @@ exports.textLinesMutator = (lines) => {
758758
curSplice[1] += L - 1;
759759
const sline = curSplice.length - 1;
760760
removed = curSplice[sline].substring(curCol) + removed;
761-
curSplice[sline] = curSplice[sline].substring(0, curCol) +
762-
lines_get(curSplice[0] + curSplice[1]);
763-
curSplice[1] += 1;
761+
const line = lines_get(curSplice[0] + curSplice[1]);
762+
// if no line follows the splice
763+
if (!line) {
764+
curSplice[sline] = curSplice[sline].substring(0, curCol);
765+
} else {
766+
curSplice[sline] = curSplice[sline].substring(0, curCol) + line;
767+
curSplice[1] += 1;
768+
}
764769
}
765770
} else {
766771
removed = nextKLinesText(L);
@@ -822,14 +827,27 @@ exports.textLinesMutator = (lines) => {
822827
curLine += newLines.length;
823828
// insert the remaining chars from the "old" line (e.g. the line we were in
824829
// when we started to insert new lines)
825-
curSplice.push(theLine.substring(lineCol));
830+
// if nothing is left we don't push an empty string
831+
if (theLine.substring(lineCol)) {
832+
curSplice.push(theLine.substring(lineCol));
833+
}
826834
curCol = 0; // TODO(doc) why is this not set to the length of last line?
827835
} else {
828836
Array.prototype.push.apply(curSplice, newLines);
829837
curLine += newLines.length;
830838
}
839+
} else if (lines_get(curSplice[0] + curSplice[1]) === undefined) {
840+
// find out if there is a line in splice that is not finished processing
841+
if (isCurLineInSplice()) { // if yes, we can add our text to it
842+
const sline = curSplice.length - 1;
843+
curSplice[sline] =
844+
curSplice[sline].substring(0, curCol) + text + curSplice[sline].substring(curCol);
845+
curCol += text.length;
846+
} else { // if no, we need to add the text in a new line
847+
Array.prototype.push.apply(curSplice, [text]);
848+
curCol += text.length;
849+
}
831850
} else {
832-
// there are no additional lines
833851
// although the line is put into splice, curLine is not increased, because
834852
// there may be more chars in the line (newline is not reached)
835853
const sline = putCurLineInSplice();

0 commit comments

Comments
 (0)