From 1612cec3e093861f6f46da4101876d32c635affa Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 09:54:19 -0800 Subject: [PATCH] Fix reflow larger bug, add regression test --- src/Buffer.test.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/Buffer.ts | 15 +++++++-------- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index bece8c22..faed1808 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -334,6 +334,49 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(3).translateToString(), ' '); assert.equal(buffer.lines.get(4).translateToString(), ' '); }); + it('should remove the correct amount of rows when reflowing larger', () => { + // This is a regression test to ensure that successive wrapped lines that are getting + // 3+ lines removed on a reflow actually remove the right lines + buffer.fillViewportRows(); + buffer.resize(10, 10); + const firstLine = buffer.lines.get(0); + const secondLine = buffer.lines.get(1); + for (let i = 0; i < 10; i++) { + const code = 'a'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + firstLine.set(i, [null, char, 1, code]); + } + for (let i = 0; i < 10; i++) { + const code = '0'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + secondLine.set(i, [null, char, 1, code]); + } + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); + assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); + for (let i = 2; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + buffer.resize(2, 10); + assert.equal(buffer.ybase, 0); + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'ab'); + assert.equal(buffer.lines.get(1).translateToString(), 'cd'); + assert.equal(buffer.lines.get(2).translateToString(), 'ef'); + assert.equal(buffer.lines.get(3).translateToString(), 'gh'); + assert.equal(buffer.lines.get(4).translateToString(), 'ij'); + assert.equal(buffer.lines.get(5).translateToString(), '01'); + assert.equal(buffer.lines.get(6).translateToString(), '23'); + assert.equal(buffer.lines.get(7).translateToString(), '45'); + assert.equal(buffer.lines.get(8).translateToString(), '67'); + assert.equal(buffer.lines.get(9).translateToString(), '89'); + buffer.resize(10, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); + assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); + for (let i = 2; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + }); }); }); diff --git a/src/Buffer.ts b/src/Buffer.ts index fc74f679..27662708 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -280,7 +280,7 @@ export class Buffer implements IBuffer { // Check how many lines it's wrapped for const wrappedLines: BufferLine[] = [this.lines.get(y) as BufferLine]; - while (nextLine.isWrapped && i < this.lines.length) { + while (i < this.lines.length && nextLine.isWrapped) { wrappedLines.push(nextLine); nextLine = this.lines.get(++i) as BufferLine; } @@ -325,7 +325,7 @@ export class Buffer implements IBuffer { toRemove.push(countToRemove); } - y += wrappedLines.length - countToRemove - 1; + y += wrappedLines.length - 1; } if (toRemove.length > 0) { @@ -346,16 +346,15 @@ export class Buffer implements IBuffer { } } - // TODO: THis and the next loop could be improved, only gather the new layout lines, not the original lines // Record original lines so they don't get overridden when we rearrange the list - const originalLines: BufferLine[] = []; - for (let i = 0; i < this.lines.length; i++) { - originalLines.push(this.lines.get(i) as BufferLine); + const newLayoutLines: BufferLine[] = []; + for (let i = 0; i < newLayout.length; i++) { + newLayoutLines.push(this.lines.get(newLayout[i]) as BufferLine); } // Rearrange the list - for (let i = 0; i < newLayout.length; i++) { - this.lines.set(i, originalLines[newLayout[i]]); + for (let i = 0; i < newLayoutLines.length; i++) { + this.lines.set(i, newLayoutLines[i]); } this.lines.length = newLayout.length;