Skip to content

Commit

Permalink
fix: factor out getTokenColor lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 2, 2021
1 parent aca84c0 commit 09771b6
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions lib/mixins/canvas-drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ export default class CanvasDrawer extends Mixin {
const editor = this.minimap.getTextEditor()
const editorElement = this.minimap.getTextEditorElement()

this.updateTokensLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, this.tokensLayer.context, editor, editorElement, this.displayCodeHighlights, this.ignoreWhitespacesInTokens, this.maxTokensInOneLine)
// 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)

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

Expand Down Expand Up @@ -184,13 +187,12 @@ export default class CanvasDrawer extends Mixin {
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
* @param {TextEditor} editor this.minimap.getTextEditor()
* @param {TextEditorElement} editorElement this.minimap.getTextEditorElement()
* @param {boolean} displayCodeHighlights this.displayCodeHighlights
* @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, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine) {
updateTokensLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, 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 @@ -200,7 +202,7 @@ export default class CanvasDrawer extends Mixin {
this.tokensLayer.clearCanvas()

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

this.drawLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine)
this.drawLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)

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

Expand Down Expand Up @@ -547,13 +549,12 @@ export default class CanvasDrawer extends Mixin {
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
* @param {TextEditor} editor this.minimap.getTextEditor()
* @param {TextEditorElement} editorElement this.minimap.getTextEditorElement()
* @param {boolean} displayCodeHighlights this.displayCodeHighlights
* @param {(t: Token) => string} getTokenColor
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
* @access private
*/
drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine) {
drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.

if (firstRow > lastRow) { return }
Expand All @@ -572,12 +573,8 @@ export default class CanvasDrawer extends Mixin {
if (/^\s+$/.test(token.text)) {
x += token.text.length * charWidth
} else {
const color = displayCodeHighlights
? this.getTokenColor(token, editorElement)
: this.getDefaultColor(editorElement)

x = drawToken(
context, token.text, color, x, y, charWidth, charHeight, ignoreWhitespacesInTokens
context, token.text, getTokenColor(token), x, y, charWidth, charHeight, ignoreWhitespacesInTokens
)
}
})
Expand Down

0 comments on commit 09771b6

Please sign in to comment.