Fix test/startCol handling

This commit is contained in:
Daniel Imms
2026-05-01 14:11:14 -07:00
parent 2a18600d18
commit 66fcdb2583
2 changed files with 14 additions and 24 deletions
+11 -21
View File
@@ -808,40 +808,30 @@ describe('BufferLine', function(): void {
assert.equal(extendedAttributes(line, 4), extendedAttributes(initial, 4)); assert.equal(extendedAttributes(line, 4), extendedAttributes(initial, 4));
}); });
it('should cache canonical trimmed string translations', () => { it('should cache canonical non-trimmed string translations', () => {
const line = new TestBufferLine(3); const line = new TestBufferLine(3);
line.setCell(0, createCellData(1, 'a', 1)); line.setCell(0, createCellData(1, 'a', 1));
line.setCell(1, createCellData(1, 'b', 1)); line.setCell(1, createCellData(1, 'b', 1));
line.setCell(2, createCellData(1, 'c', 1)); line.setCell(2, createCellData(1, 'c', 1));
const originalGetTrimmedLength = line.getTrimmedLength.bind(line); const translated = line.translateToString(false, undefined, undefined, undefined);
let trimmedLengthCalls = 0; assert.equal(translated, 'abc');
line.getTrimmedLength = (): number => { assert.equal((line as any)._cachedTrimmedString, 'abc');
trimmedLengthCalls++; (line as any)._cachedTrimmedString = 'cached';
return originalGetTrimmedLength(); assert.equal(line.translateToString(false, undefined, undefined, undefined), 'cached');
}; // Any optional translation argument should bypass cache.
assert.equal(line.translateToString(false, 0, 2, undefined), 'ab');
assert.equal(line.translateToString(true, undefined, undefined, undefined), 'abc'); assert.equal(line.translateToString(true, undefined, undefined, undefined), 'abc');
assert.equal(line.translateToString(true, undefined, undefined, undefined), 'abc');
assert.equal(trimmedLengthCalls, 1);
}); });
it('should invalidate cached trimmed strings on line mutations', () => { it('should invalidate cached trimmed strings on line mutations', () => {
const assertCacheInvalidated = (mutate: (line: TestBufferLine) => void): void => { const assertCacheInvalidated = (mutate: (line: TestBufferLine) => void): void => {
const line = new TestBufferLine(5); const line = new TestBufferLine(5);
line.fill(createCellData(1, 'a', 1)); line.fill(createCellData(1, 'a', 1));
const originalGetTrimmedLength = line.getTrimmedLength.bind(line); line.translateToString(false, undefined, undefined, undefined);
let trimmedLengthCalls = 0; assert.equal((line as any)._cachedTrimmedString, 'aaaaa');
line.getTrimmedLength = (): number => {
trimmedLengthCalls++;
return originalGetTrimmedLength();
};
assert.equal(line.translateToString(true, undefined, undefined, undefined), 'aaaaa');
assert.equal(line.translateToString(true, undefined, undefined, undefined), 'aaaaa');
assert.equal(trimmedLengthCalls, 1);
mutate(line); mutate(line);
line.translateToString(true, undefined, undefined, undefined); assert.equal((line as any)._cachedTrimmedString, undefined);
assert.equal(trimmedLengthCalls, 2);
}; };
assertCacheInvalidated(line => line.set(0, [0, 'b', 1, 'b'.charCodeAt(0)])); assertCacheInvalidated(line => line.set(0, [0, 'b', 1, 'b'.charCodeAt(0)]));
+3 -3
View File
@@ -520,8 +520,8 @@ export class BufferLine implements IBufferLine {
} }
/** /**
* Translates the buffer line to a string. Using any of the optional arguments will disable the * Translates the buffer line to a string. Caching only applies to canonical non-trimmed
* cache. * translation requests. Using `trimRight` or any optional range/mapping arguments disables cache.
* *
* @param trimRight Whether to trim any empty cells on the right. * @param trimRight Whether to trim any empty cells on the right.
* @param startCol The column to start the string (0-based inclusive). * @param startCol The column to start the string (0-based inclusive).
@@ -534,7 +534,7 @@ export class BufferLine implements IBufferLine {
* returned string, the corresponding entries in `outColumns` will have the same column number. * returned string, the corresponding entries in `outColumns` will have the same column number.
*/ */
public translateToString(trimRight?: boolean, startCol?: number, endCol?: number, outColumns?: number[]): string { public translateToString(trimRight?: boolean, startCol?: number, endCol?: number, outColumns?: number[]): string {
const isCanonicalTrimmedRequest = !trimRight && !startCol && endCol === undefined && outColumns === undefined; const isCanonicalTrimmedRequest = !trimRight && (startCol === undefined || startCol === 0) && endCol === undefined && outColumns === undefined;
if (isCanonicalTrimmedRequest && this._cachedTrimmedString !== undefined) { if (isCanonicalTrimmedRequest && this._cachedTrimmedString !== undefined) {
return this._cachedTrimmedString; return this._cachedTrimmedString;
} }