textLinesMutator: Fix insertions at the end of lines

The new insertions are directly pushed to curSplice now instead of
trying to incorporate an undefined line into the splice, which would
result in an error: TypeError: Cannot read property 'substring' of
undefined
This commit is contained in:
webzwo0i 2021-10-30 23:42:15 +02:00 committed by SamTV12345
parent fe5d43871f
commit 01b04a6f92
2 changed files with 27 additions and 2 deletions

View file

@ -983,11 +983,17 @@ class TextLinesMutator {
this._curSplice.push(...newLines);
this._curLine += newLines.length;
}
} else {
} else if (!this.hasMore()) {
// There are no additional lines. Although the line is put into splice, curLine is not
// increased because there may be more chars in the line (newline is not reached).
// increased because there may be more chars in the line (newline is not reached). We are
// inserting at the end of lines. curCol is 0 as curLine is not in splice.
this._curSplice.push(text);
this._curCol += text.length;
} else {
// insert text after curCol
const sline = this._putCurLineInSplice();
if (!this._curSplice[sline]) {
// TODO should never happen now
const err = new Error(
'curSplice[sline] not populated, actual curSplice contents is ' +
`${JSON.stringify(this._curSplice)}. Possibly related to ` +

View file

@ -160,6 +160,25 @@ describe('easysync-mutations', function () {
['insert', 'z'],
], ['fuz']);
// #2836, #5214, #3560 regressions
runMutationTest(11, ['\n'], [
['remove', 1, 1, '\n'],
['insert', 'c', 0],
], ['c']);
runMutationTest(12, ['\n'], [
['remove', 1, 1, '\n'],
['insert', 'a\n', 1],
['insert', 'c'],
], ['a\n', 'c']);
runMutationTest(13, ['\n', 'fun\n', '\n'], [
['remove', 1, 1, '\n'],
['skip', 4, 1, false],
['remove', 1, 1, '\n'],
['insert', 'c'],
], ['fun\n', 'c']);
it('mutatorHasMore', async function () {
const lines = ['1\n', '2\n', '3\n', '4\n'];
let mu;