From 173cdcb372f40e7564bad963a90d3e823e29916a Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 14 Mar 2017 17:05:56 -0600 Subject: [PATCH] Cache rendered screen lines on component to avoid drifting from model The model may have screen lines that aren't yet rendered in the page, and we want to avoid referring to them on mouse clicks. --- src/text-editor-component.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/text-editor-component.js b/src/text-editor-component.js index cb8b93e54..ab5a17f35 100644 --- a/src/text-editor-component.js +++ b/src/text-editor-component.js @@ -100,6 +100,7 @@ class TextEditorComponent { if (this.pendingAutoscroll) this.initiateAutoscroll() this.populateVisibleRowRange() const longestLineToMeasure = this.checkForNewLongestLine() + this.queryScreenLinesToRender() this.queryDecorationsToRender() etch.updateSync(this) @@ -293,8 +294,6 @@ class TextEditorComponent { const tileWidth = this.getContentWidth() const displayLayer = this.getModel().displayLayer - const screenLines = displayLayer.getScreenLines(startRow, endRow) - const tileNodes = new Array(this.getRenderedTileCount()) for (let tileStartRow = startRow; tileStartRow < endRow; tileStartRow += rowsPerTile) { @@ -313,7 +312,7 @@ class TextEditorComponent { width: tileWidth, top: this.topPixelPositionForRow(tileStartRow), lineHeight: this.measurements.lineHeight, - screenLines: screenLines.slice(tileStartRow - startRow, tileEndRow - startRow), + screenLines: this.renderedScreenLines.slice(tileStartRow - startRow, tileEndRow - startRow), lineDecorations, highlightDecorations, displayLayer, @@ -417,6 +416,17 @@ class TextEditorComponent { return process.platform } + queryScreenLinesToRender () { + this.renderedScreenLines = this.getModel().displayLayer.getScreenLines( + this.getRenderedStartRow(), + this.getRenderedEndRow() + ) + } + + renderedScreenLineForRow (row) { + return this.renderedScreenLines[row - this.getRenderedStartRow()] + } + queryDecorationsToRender () { this.decorationsToRender.lineNumbers.clear() this.decorationsToRender.lines.clear() @@ -1206,7 +1216,7 @@ class TextEditorComponent { this.horizontalPositionsToMeasure.forEach((columnsToMeasure, row) => { columnsToMeasure.sort((a, b) => a - b) - const screenLine = this.getModel().displayLayer.getScreenLine(row) + const screenLine = this.renderedScreenLineForRow(row) const lineNode = this.lineNodesByScreenLineId.get(screenLine.id) if (!lineNode) { @@ -1270,7 +1280,7 @@ class TextEditorComponent { pixelLeftForRowAndColumn (row, column) { if (column === 0) return 0 - const screenLine = this.getModel().displayLayer.getScreenLine(row) + const screenLine = this.renderedScreenLineForRow(row) return this.horizontalPixelPositionsByScreenLineId.get(screenLine.id).get(column) } @@ -1284,7 +1294,7 @@ class TextEditorComponent { const linesClientLeft = this.refs.lineTiles.getBoundingClientRect().left const targetClientLeft = linesClientLeft + Math.max(0, left) - const screenLine = this.getModel().displayLayer.getScreenLine(row) + const screenLine = this.renderedScreenLineForRow(row) const textNodes = this.textNodesByScreenLineId.get(screenLine.id) let containingTextNodeIndex