Skip to content

Commit a8f09ab

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 77e4cde commit a8f09ab

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/static/js/Changeset.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,14 @@ exports.textLinesMutator = (lines) => {
711711
curSplice[1] += L - 1;
712712
const sline = curSplice.length - 1;
713713
removed = curSplice[sline].substring(curCol) + removed;
714-
curSplice[sline] = curSplice[sline].substring(0, curCol) +
715-
linesGet(curSplice[0] + curSplice[1]);
716-
curSplice[1] += 1;
714+
const line = linesGet(curSplice[0] + curSplice[1]);
715+
// if no line follows the splice
716+
if (!line) {
717+
curSplice[sline] = curSplice[sline].substring(0, curCol);
718+
} else {
719+
curSplice[sline] = curSplice[sline].substring(0, curCol) + line;
720+
curSplice[1] += 1;
721+
}
717722
}
718723
} else {
719724
removed = nextKLinesText(L);
@@ -775,14 +780,27 @@ exports.textLinesMutator = (lines) => {
775780
curLine += newLines.length;
776781
// insert the remaining chars from the "old" line (e.g. the line we were in
777782
// when we started to insert new lines)
778-
curSplice.push(theLine.substring(lineCol));
783+
// if nothing is left we don't push an empty string
784+
if (theLine.substring(lineCol)) {
785+
curSplice.push(theLine.substring(lineCol));
786+
}
779787
curCol = 0; // TODO(doc) why is this not set to the length of last line?
780788
} else {
781789
Array.prototype.push.apply(curSplice, newLines);
782790
curLine += newLines.length;
783791
}
792+
} else if (lines_get(curSplice[0] + curSplice[1]) === undefined) {
793+
// find out if there is a line in splice that is not finished processing
794+
if (isCurLineInSplice()) { // if yes, we can add our text to it
795+
const sline = curSplice.length - 1;
796+
curSplice[sline] =
797+
curSplice[sline].substring(0, curCol) + text + curSplice[sline].substring(curCol);
798+
curCol += text.length;
799+
} else { // if no, we need to add the text in a new line
800+
Array.prototype.push.apply(curSplice, [text]);
801+
curCol += text.length;
802+
}
784803
} else {
785-
// there are no additional lines
786804
// although the line is put into splice, curLine is not increased, because
787805
// there may be more chars in the line (newline is not reached)
788806
const sline = putCurLineInSplice();

src/tests/frontend/specs/easysync.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,28 @@ describe('easysync', function () {
179179
], ['banana\n', 'cabbage\n', 'duffle\n']);
180180

181181
// #2836 regressions
182-
runMutationTest(8, ['\n'], [
182+
runMutationTest(8, ['\n', 'foo\n', '\n'], [
183+
['remove', 1, 1, '\n'],
184+
['skip', 4, 1, false],
185+
['remove', 1, 1, '\n'],
186+
['insert', 'c'],
187+
], ['foo\n', 'c']);
188+
runMutationTest(9, ['\n', 'foo\n', '\n'], [
189+
['remove', 1, 1, '\n'],
190+
['skip', 3, 0, false],
191+
['remove', 2, 2, '\n\n'],
192+
['insert', 'c'],
193+
], ['fooc']);
194+
runMutationTest(10, ['\n'], [
183195
['remove', 1, 1, '\n'],
184196
['insert', 'c', 0],
185-
], ['c']);
186-
runMutationTest(9, ['\n'], [
197+
], ['c']); // TODO find out if c must have a newline because of unknown constraints
198+
runMutationTest(11, ['\n'], [
187199
['remove', 1, 1, '\n'],
188200
['insert', 'a'],
189201
['insert', 'c\n', 1],
190202
], ['ac\n']);
191-
runMutationTest(10, ['\n'], [
203+
runMutationTest(12, ['\n'], [
192204
['remove', 1, 1, '\n'],
193205
['insert', 'a\n', 1],
194206
['insert', 'c'],

0 commit comments

Comments
 (0)