Skip to content

Commit

Permalink
fix: factor editorScreenLineCount and invisibleRegExp out of the loop
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 3, 2021
1 parent 7e72819 commit 9359279
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/mixins/canvas-drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class CanvasDrawer extends Mixin {
// TODO avoid closure: https://stackoverflow.com/a/46256398/7910299
const getTokenColor = this.displayCodeHighlights ? (t) => this.getTokenColor(t, editorElement) : () => this.getDefaultColor(editorElement)

this.updateTokensLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, this.tokensLayer.context, editor, getTokenColor, this.ignoreWhitespacesInTokens, this.maxTokensInOneLine)
this.updateTokensLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, this.tokensLayer.context, editor, editor.getScreenLineCount(), getInvisibleRegExp(editor), getTokenColor, this.ignoreWhitespacesInTokens, this.maxTokensInOneLine)

const decorations = this.minimap.decorationsByTypeThenRows(firstRow, lastRow)

Expand Down Expand Up @@ -193,7 +193,7 @@ export default class CanvasDrawer extends Mixin {
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
* @access private
*/
updateTokensLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
updateTokensLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
const intactRanges = computeIntactRanges(firstRow, lastRow, this.pendingChanges, this.offscreenFirstRow, this.offscreenLastRow)

// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
Expand All @@ -208,7 +208,7 @@ export default class CanvasDrawer extends Mixin {
}

if (intactRanges.length === 0) {
drawLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
drawLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
} else {
for (let j = 0, len = intactRanges.length; j < len; j++) {
const intact = intactRanges[j]
Expand All @@ -224,12 +224,12 @@ export default class CanvasDrawer extends Mixin {
for (let i = 0, len = intactRanges.length; i < len; i++) {
const range = intactRanges[i]

drawLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
drawLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)

currentRow = range.end
}
if (currentRow <= lastRow) {
drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
}
}

Expand Down Expand Up @@ -654,13 +654,14 @@ function drawToken (context, text, color, x, y, charWidth, charHeight, ignoreWhi
* @param {number} startRow The start row
* @param {number} endRow The end row
* @param {TextEditor} editor
* @param {number} editorScreenLineCount
* @param {RegExp} invisibleRegExp
* @param {number} maxTokensInOneLine the maximum number of tokens to render in one line
* @return {Array<Array>} An array of tokens by line
* @access private
*/
function eachTokenForScreenRows (startRow, endRow, editor, maxTokensInOneLine, callback) {
const invisibleRegExp = getInvisibleRegExp(editor)
endRow = Math.min(endRow, editor.getScreenLineCount())
function eachTokenForScreenRows (startRow, endRow, editor, editorScreenLineCount, invisibleRegExp, maxTokensInOneLine, callback) {
endRow = Math.min(endRow, editorScreenLineCount)

for (let row = startRow; row < endRow; row++) {
const editorTokensForScreenRow = editor.tokensForScreenRow(row)
Expand Down Expand Up @@ -692,19 +693,21 @@ function eachTokenForScreenRows (startRow, endRow, editor, maxTokensInOneLine, c
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
* @param {TextEditor} editor this.minimap.getTextEditor()
* @param {number} editorScreenLineCount
* @param {RegExp} invisibleRegExp
* @param {(t: Token) => string} getTokenColor
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
* @access private
*/
function drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
function drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.

if (firstRow > lastRow) { return }

let lastLine, x
let y = (offsetRow * lineHeight) - lineHeight
eachTokenForScreenRows(firstRow, lastRow, editor, maxTokensInOneLine, (line, token) => {
eachTokenForScreenRows(firstRow, lastRow, editor, editorScreenLineCount, invisibleRegExp, maxTokensInOneLine, (line, token) => {
if (lastLine !== line) {
x = 0
y += lineHeight
Expand Down

0 comments on commit 9359279

Please sign in to comment.