Skip to content

Commit

Permalink
Merge pull request #1703 from Tyriar/1702_isWrapped_fix
Browse files Browse the repository at this point in the history
Speculative fix for NPE in linkifier
  • Loading branch information
Tyriar committed Sep 21, 2018
2 parents 290c95a + 2c62c3a commit 2e8e64e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Linkifier.test.ts
Expand Up @@ -42,6 +42,7 @@ describe('Linkifier', () => {
beforeEach(() => {
terminal = new MockTerminal();
terminal.cols = 100;
terminal.rows = 10;
terminal.buffer = new MockBuffer();
(<MockBuffer>terminal.buffer).setLines(new CircularList<IBufferLine>(20));
terminal.buffer.ydisp = 0;
Expand All @@ -64,6 +65,7 @@ describe('Linkifier', () => {
function assertLinkifiesRow(rowText: string, linkMatcherRegex: RegExp, links: {x: number, length: number}[], done: MochaDone): void {
addRow(rowText);
linkifier.registerLinkMatcher(linkMatcherRegex, () => {});
terminal.rows = terminal.buffer.lines.length - 1;
linkifier.linkifyRows();
// Allow linkify to happen
setTimeout(() => {
Expand Down
12 changes: 8 additions & 4 deletions src/Linkifier.ts
Expand Up @@ -81,18 +81,22 @@ export class Linkifier extends EventEmitter implements ILinkifier {
*/
private _linkifyRows(): void {
this._rowsTimeoutId = null;
const buffer = this._terminal.buffer;

// Ensure the row exists
const absoluteRowIndexStart = this._terminal.buffer.ydisp + this._rowsToLinkify.start;
if (absoluteRowIndexStart >= this._terminal.buffer.lines.length) {
// Ensure the start row exists
const absoluteRowIndexStart = buffer.ydisp + this._rowsToLinkify.start;
if (absoluteRowIndexStart >= buffer.lines.length) {
return;
}

// Invalidate bad end row values (if a resize happened)
const absoluteRowIndexEnd = buffer.ydisp + Math.min(this._rowsToLinkify.end, this._terminal.rows) + 1;

// iterate over the range of unwrapped content strings within start..end (excluding)
// _doLinkifyRow gets full unwrapped lines with the start row as buffer offset for every matcher
// for wrapped content over several rows the iterator might return rows outside the viewport
// we skip those later in _doLinkifyRow
const iterator = this._terminal.buffer.iterator(false, absoluteRowIndexStart, this._terminal.buffer.ydisp + this._rowsToLinkify.end + 1);
const iterator = buffer.iterator(false, absoluteRowIndexStart, absoluteRowIndexEnd);
while (iterator.hasNext()) {
const lineData: IBufferStringIteratorResult = iterator.next();
for (let i = 0; i < this._linkMatchers.length; i++) {
Expand Down

0 comments on commit 2e8e64e

Please sign in to comment.