Skip to content

Commit 3512671

Browse files
committed
translateToString() adds one more entry to outColumns
Previously, for string 'ab好', outColumns=[0, 1, 2]. Now it becomes [0, 1, 2, 4]. This allows the user to know where does the last character ends.
1 parent 836da87 commit 3512671

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

src/browser/AccessibilityManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export class AccessibilityManager extends Disposable {
206206
if (element) {
207207
if (lineData.length === 0) {
208208
element.innerText = '\u00a0';
209-
this._rowColumns.set(element, [0]);
209+
this._rowColumns.set(element, [0, 1]);
210210
} else {
211211
element.textContent = lineData;
212212
this._rowColumns.set(element, columns);

src/common/buffer/BufferLine.test.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ describe('BufferLine', function(): void {
333333
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]), false);
334334
const columns: number[] = [];
335335
assert.equal(line.translateToString(false, undefined, undefined, columns), ' ');
336-
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
336+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
337337
assert.equal(line.translateToString(true, undefined, undefined, columns), '');
338-
assert.deepEqual(columns, []);
338+
assert.deepEqual(columns, [0]);
339339
});
340340
it('ASCII', function(): void {
341341
const columns: number[] = [];
@@ -345,16 +345,16 @@ describe('BufferLine', function(): void {
345345
line.setCell(4, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
346346
line.setCell(5, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
347347
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a a aa ');
348-
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
348+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
349349
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a a aa');
350-
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5]);
350+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6]);
351351
for (const trimRight of [true, false]) {
352352
assert.equal(line.translateToString(trimRight, 0, 5, columns), 'a a a');
353-
assert.deepEqual(columns, [0, 1, 2, 3, 4]);
353+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5]);
354354
assert.equal(line.translateToString(trimRight, 0, 4, columns), 'a a ');
355-
assert.deepEqual(columns, [0, 1, 2, 3]);
355+
assert.deepEqual(columns, [0, 1, 2, 3, 4]);
356356
assert.equal(line.translateToString(trimRight, 0, 3, columns), 'a a');
357-
assert.deepEqual(columns, [0, 1, 2]);
357+
assert.deepEqual(columns, [0, 1, 2, 3]);
358358
}
359359

360360
});
@@ -366,16 +366,16 @@ describe('BufferLine', function(): void {
366366
line.setCell(4, CellData.fromCharData([1, '𝄞', 1, '𝄞'.charCodeAt(0)]));
367367
line.setCell(5, CellData.fromCharData([1, '𝄞', 1, '𝄞'.charCodeAt(0)]));
368368
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a 𝄞 𝄞𝄞 ');
369-
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8, 9]);
369+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10]);
370370
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a 𝄞 𝄞𝄞');
371-
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5]);
371+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5, 6]);
372372
for (const trimRight of [true, false]) {
373373
assert.equal(line.translateToString(trimRight, 0, 5, columns), 'a 𝄞 𝄞');
374-
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4]);
374+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5]);
375375
assert.equal(line.translateToString(trimRight, 0, 4, columns), 'a 𝄞 ');
376-
assert.deepEqual(columns, [0, 1, 2, 2, 3]);
376+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4]);
377377
assert.equal(line.translateToString(trimRight, 0, 3, columns), 'a 𝄞');
378-
assert.deepEqual(columns, [0, 1, 2, 2]);
378+
assert.deepEqual(columns, [0, 1, 2, 2, 3]);
379379
}
380380
});
381381
it('combining', function(): void {
@@ -386,16 +386,16 @@ describe('BufferLine', function(): void {
386386
line.setCell(4, CellData.fromCharData([1, 'e\u0301', 1, '\u0301'.charCodeAt(0)]));
387387
line.setCell(5, CellData.fromCharData([1, 'e\u0301', 1, '\u0301'.charCodeAt(0)]));
388388
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a e\u0301 e\u0301e\u0301 ');
389-
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8, 9]);
389+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10]);
390390
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a e\u0301 e\u0301e\u0301');
391-
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5]);
391+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5, 6]);
392392
for (const trimRight of [true, false]) {
393393
assert.equal(line.translateToString(trimRight, 0, 5, columns), 'a e\u0301 e\u0301');
394-
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4]);
394+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5]);
395395
assert.equal(line.translateToString(trimRight, 0, 4, columns), 'a e\u0301 ');
396-
assert.deepEqual(columns, [0, 1, 2, 2, 3]);
396+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4]);
397397
assert.equal(line.translateToString(trimRight, 0, 3, columns), 'a e\u0301');
398-
assert.deepEqual(columns, [0, 1, 2, 2]);
398+
assert.deepEqual(columns, [0, 1, 2, 2, 3]);
399399
}
400400
});
401401
it('fullwidth', function(): void {
@@ -409,22 +409,22 @@ describe('BufferLine', function(): void {
409409
line.setCell(7, CellData.fromCharData([1, '1', 2, '1'.charCodeAt(0)]));
410410
line.setCell(8, CellData.fromCharData([0, '', 0, 0]));
411411
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a 1 11 ');
412-
assert.deepEqual(columns, [0, 1, 2, 4, 5, 7, 9]);
412+
assert.deepEqual(columns, [0, 1, 2, 4, 5, 7, 9, 10]);
413413
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a 1 11');
414-
assert.deepEqual(columns, [0, 1, 2, 4, 5, 7]);
414+
assert.deepEqual(columns, [0, 1, 2, 4, 5, 7, 9]);
415415
for (const trimRight of [true, false]) {
416416
assert.equal(line.translateToString(trimRight, 0, 7, columns), 'a 1 1');
417-
assert.deepEqual(columns, [0, 1, 2, 4, 5]);
417+
assert.deepEqual(columns, [0, 1, 2, 4, 5, 7]);
418418
assert.equal(line.translateToString(trimRight, 0, 6, columns), 'a 1 1');
419-
assert.deepEqual(columns, [0, 1, 2, 4, 5]);
419+
assert.deepEqual(columns, [0, 1, 2, 4, 5, 7]);
420420
assert.equal(line.translateToString(trimRight, 0, 5, columns), 'a 1 ');
421-
assert.deepEqual(columns, [0, 1, 2, 4]);
421+
assert.deepEqual(columns, [0, 1, 2, 4, 5]);
422422
assert.equal(line.translateToString(trimRight, 0, 4, columns), 'a 1');
423-
assert.deepEqual(columns, [0, 1, 2]);
423+
assert.deepEqual(columns, [0, 1, 2, 4]);
424424
assert.equal(line.translateToString(trimRight, 0, 3, columns), 'a 1');
425-
assert.deepEqual(columns, [0, 1, 2]);
425+
assert.deepEqual(columns, [0, 1, 2, 4]);
426426
assert.equal(line.translateToString(trimRight, 0, 2, columns), 'a ');
427-
assert.deepEqual(columns, [0, 1]);
427+
assert.deepEqual(columns, [0, 1, 2]);
428428
}
429429
});
430430
it('space at end', function(): void {
@@ -436,9 +436,9 @@ describe('BufferLine', function(): void {
436436
line.setCell(5, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
437437
line.setCell(6, CellData.fromCharData([1, ' ', 1, ' '.charCodeAt(0)]));
438438
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a a aa ');
439-
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
439+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
440440
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a a aa ');
441-
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6]);
441+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7]);
442442
});
443443
it('should always return some sane value', function(): void {
444444
const columns: number[] = [];
@@ -447,16 +447,16 @@ describe('BufferLine', function(): void {
447447
// fullwidth pairs --> needs to be fixed after settling BufferLine impl
448448
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, 0, NULL_CELL_CODE]), false);
449449
assert.equal(line.translateToString(false, undefined, undefined, columns), ' ');
450-
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
450+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
451451
assert.equal(line.translateToString(true, undefined, undefined, columns), '');
452-
assert.deepEqual(columns, []);
452+
assert.deepEqual(columns, [0]);
453453
});
454454
it('should work with endCol=0', () => {
455455
const columns: number[] = [];
456456
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, 0, NULL_CELL_CODE]), false);
457457
line.setCell(0, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
458458
assert.equal(line.translateToString(true, 0, 0, columns), '');
459-
assert.deepEqual(columns, []);
459+
assert.deepEqual(columns, [0]);
460460
});
461461
});
462462
describe('addCharToCell', () => {

src/common/buffer/BufferLine.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,13 @@ export class BufferLine implements IBufferLine {
506506

507507
/**
508508
* If outColumns is specified, it will be filled with column numbers such that
509-
* returnedString[i] is display at outColumns[i] column. When a single cell is
510-
* translated to multiple UTF-16 code units (e.g. surrogate pair) in the
511-
* returned string, the corresponding entries in outColumns will have the same
512-
* column number.
509+
* returnedString[i] is display at outColumns[i] column.
510+
* outColumns[returnedString.length] is where the character following
511+
* returnedString will be displayed.
512+
*
513+
* When a single cell is translated to multiple UTF-16 code units (e.g.
514+
* surrogate pair) in the returned string, the corresponding entries in
515+
* outColumns will have the same column number.
513516
*/
514517
public translateToString(trimRight?: boolean, startCol?: number, endCol?: number, outColumns?: number[]): string {
515518
startCol = startCol ?? 0;
@@ -533,6 +536,9 @@ export class BufferLine implements IBufferLine {
533536
}
534537
startCol += (content >> Content.WIDTH_SHIFT) || 1; // always advance by at least 1
535538
}
539+
if (outColumns) {
540+
outColumns.push(startCol);
541+
}
536542
return result;
537543
}
538544
}

0 commit comments

Comments
 (0)