Skip to content

Commit

Permalink
fix: make updateTokensLayer a free function
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 3, 2021
1 parent 6e93ff1 commit 02e3bf8
Showing 1 changed file with 65 additions and 62 deletions.
127 changes: 65 additions & 62 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, editor.getScreenLineCount(), getInvisibleRegExp(editor), getTokenColor, this.ignoreWhitespacesInTokens, this.maxTokensInOneLine)
updateTokensLayer(this.tokensLayer, firstRow, lastRow, this.offscreenFirstRow, this.offscreenLastRow, this.pendingChanges, lineHeight, charHeight, charWidth, canvasWidth, editor, editor.getScreenLineCount(), getInvisibleRegExp(editor), getTokenColor, this.ignoreWhitespacesInTokens, this.maxTokensInOneLine)

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

Expand Down Expand Up @@ -176,67 +176,6 @@ export default class CanvasDrawer extends Mixin {
this.offscreenLastRow = lastRow
}

/**
* Performs an update of the tokens layer using the pending changes array.
*
* @param {number} firstRow firstRow the first row of the range to update
* @param {number} lastRow lastRow the last row of the range to update
*
* @param {number} lineHeight this.minimap.getLineHeight() * devicePixelRatio
* @param {number} charHeight this.minimap.getCharHeight() * devicePixelRatio
* @param {number} charWidth this.minimap.getCharWidth() * devicePixelRatio
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
* @param {TextEditor} editor this.minimap.getTextEditor()
* @param {(t: Token) => string} getTokenColor
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
* @access private
*/
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.

// redrawRangesOnLayer inlined (this.redrawRangesOnLayer(this.tokensLayer, intactRanges, firstRow, lastRow, drawLines))

this.tokensLayer.clearCanvas()

if (SPEC_MODE) {
// call the spy
this.drawLines(firstRow, lastRow)
}

if (intactRanges.length === 0) {
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]

this.tokensLayer.copyPartFromOffscreen(
intact.offscreenRow * lineHeight,
(intact.start - firstRow) * lineHeight,
(intact.end - intact.start) * lineHeight
)
}
// drawLinesForRanges inlined
let currentRow = firstRow
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, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)

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

this.tokensLayer.resetOffscreenSize()
this.tokensLayer.copyToOffscreen()
}

/**
* Performs an update of the back decorations layer using the pending back
* decorations changes arrays.
Expand Down Expand Up @@ -606,6 +545,70 @@ export default class CanvasDrawer extends Mixin {
// ## ## ## ## ## ## ## ## ##
// ######## ## ## ## ## ### ###

/**
* Performs an update of the tokens layer using the pending changes array.
*
* @param {CanvasLayer} tokensLayer
* @param {number} firstRow firstRow the first row of the range to update
* @param {number} lastRow lastRow the last row of the range to update
* @param {number} offscreenFirstRow
* @param {number} offscreenLastRow
* @param {Array<>} pendingChanges
* @param {number} lineHeight this.minimap.getLineHeight() * devicePixelRatio
* @param {number} charHeight this.minimap.getCharHeight() * devicePixelRatio
* @param {number} charWidth this.minimap.getCharWidth() * devicePixelRatio
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {TextEditor} editor this.minimap.getTextEditor()
* @param {(t: Token) => string} getTokenColor
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
* @access private
*/
function updateTokensLayer (tokensLayer, firstRow, lastRow, offscreenFirstRow, offscreenLastRow, pendingChanges, lineHeight, charHeight, charWidth, canvasWidth, editor, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.

const intactRanges = computeIntactRanges(firstRow, lastRow, pendingChanges, offscreenFirstRow, offscreenLastRow)

// redrawRangesOnLayer inlined (this.redrawRangesOnLayer(this.tokensLayer, intactRanges, firstRow, lastRow, drawLines))
const context = tokensLayer.context

tokensLayer.clearCanvas()

if (SPEC_MODE) {
// call the spy
this.drawLines(firstRow, lastRow)
}

if (intactRanges.length === 0) {
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]

tokensLayer.copyPartFromOffscreen(
intact.offscreenRow * lineHeight,
(intact.start - firstRow) * lineHeight,
(intact.end - intact.start) * lineHeight
)
}
// drawLinesForRanges inlined
let currentRow = firstRow
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, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)

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

tokensLayer.resetOffscreenSize()
tokensLayer.copyToOffscreen()
}

const emptyLineRegexp = /^\s+$/
const whiteSpaceRegexp = /\s+/

Expand Down

0 comments on commit 02e3bf8

Please sign in to comment.