Skip to content

Commit 682efc7

Browse files
authored
Merge pull request #1887 from jerch/fix_linkifier_offset
account empty cells in stringIndexToBufferIndex
2 parents 8e88ce8 + 9791d8a commit 682efc7

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Buffer.test.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ describe('Buffer', () => {
12781278
const s = terminal.buffer.iterator(true).next().content;
12791279
assert.equal(input, s);
12801280
for (let i = 10; i < input.length; ++i) {
1281-
const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i);
1281+
const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i, true);
12821282
const j = (i - 0) << 1;
12831283
assert.deepEqual([(j / terminal.cols) | 0, j % terminal.cols], bufferIndex);
12841284
}
@@ -1290,7 +1290,7 @@ describe('Buffer', () => {
12901290
const s = terminal.buffer.iterator(true).next().content;
12911291
assert.equal(input, s);
12921292
for (let i = 0; i < input.length; ++i) {
1293-
const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i);
1293+
const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i, true);
12941294
assert.equal(input[i], terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]);
12951295
}
12961296
});
@@ -1302,7 +1302,7 @@ describe('Buffer', () => {
13021302
const s = terminal.buffer.iterator(true).next().content;
13031303
assert.equal(input, s);
13041304
for (let i = 0; i < input.length; ++i) {
1305-
const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i);
1305+
const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i, true);
13061306
assert.equal(
13071307
(!(i % 3))
13081308
? input[i]
@@ -1312,6 +1312,13 @@ describe('Buffer', () => {
13121312
terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]);
13131313
}
13141314
});
1315+
1316+
it('should handle \t in lines correctly', () => {
1317+
const input = '\thttps://google.de';
1318+
terminal.writeSync(input);
1319+
const s = terminal.buffer.iterator(true).next().content;
1320+
assert.equal(s, Array(terminal.getOption('tabStopWidth') + 1).join(' ') + 'https://google.de');
1321+
});
13151322
});
13161323
describe('BufferStringIterator', function(): void {
13171324
it('iterator does not overflow buffer limits', function(): void {

src/Buffer.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,19 @@ export class Buffer implements IBuffer {
449449
* @param stringIndex index within the string
450450
* @param startCol column offset the string was retrieved from
451451
*/
452-
public stringIndexToBufferIndex(lineIndex: number, stringIndex: number): BufferIndex {
452+
public stringIndexToBufferIndex(lineIndex: number, stringIndex: number, trimRight: boolean = false): BufferIndex {
453453
while (stringIndex) {
454454
const line = this.lines.get(lineIndex);
455455
if (!line) {
456456
return [-1, -1];
457457
}
458-
for (let i = 0; i < line.length; ++i) {
459-
stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length;
458+
const length = (trimRight) ? line.getTrimmedLength() : line.length;
459+
for (let i = 0; i < length; ++i) {
460+
if (line.get(i)[CHAR_DATA_WIDTH_INDEX]) {
461+
// empty cells report a string length of 0, but get replaced
462+
// with a whitespace in translateToString, thus replace with 1
463+
stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1;
464+
}
460465
if (stringIndex < 0) {
461466
return [lineIndex, i];
462467
}

0 commit comments

Comments
 (0)