Skip to content

Commit 09771b6

Browse files
committed
fix: factor out getTokenColor lambda
1 parent aca84c0 commit 09771b6

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

lib/mixins/canvas-drawer.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ export default class CanvasDrawer extends Mixin {
130130
const editor = this.minimap.getTextEditor()
131131
const editorElement = this.minimap.getTextEditorElement()
132132

133-
this.updateTokensLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, this.tokensLayer.context, editor, editorElement, this.displayCodeHighlights, this.ignoreWhitespacesInTokens, this.maxTokensInOneLine)
133+
// TODO avoid closure: https://stackoverflow.com/a/46256398/7910299
134+
const getTokenColor = this.displayCodeHighlights ? (t) => this.getTokenColor(t, editorElement) : () => this.getDefaultColor(editorElement)
135+
136+
this.updateTokensLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, this.tokensLayer.context, editor, getTokenColor, this.ignoreWhitespacesInTokens, this.maxTokensInOneLine)
134137

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

@@ -184,13 +187,12 @@ export default class CanvasDrawer extends Mixin {
184187
* @param {number} canvasWidth this.tokensLayer.getSize().width
185188
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
186189
* @param {TextEditor} editor this.minimap.getTextEditor()
187-
* @param {TextEditorElement} editorElement this.minimap.getTextEditorElement()
188-
* @param {boolean} displayCodeHighlights this.displayCodeHighlights
190+
* @param {(t: Token) => string} getTokenColor
189191
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
190192
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
191193
* @access private
192194
*/
193-
updateTokensLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine) {
195+
updateTokensLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
194196
const intactRanges = computeIntactRanges(firstRow, lastRow, this.pendingChanges, this.offscreenFirstRow, this.offscreenLastRow)
195197

196198
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
@@ -200,7 +202,7 @@ export default class CanvasDrawer extends Mixin {
200202
this.tokensLayer.clearCanvas()
201203

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

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

221223
currentRow = range.end
222224
}
223225
if (currentRow <= lastRow) {
224-
this.drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine)
226+
this.drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
225227
}
226228
}
227229

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

559560
if (firstRow > lastRow) { return }
@@ -572,12 +573,8 @@ export default class CanvasDrawer extends Mixin {
572573
if (/^\s+$/.test(token.text)) {
573574
x += token.text.length * charWidth
574575
} else {
575-
const color = displayCodeHighlights
576-
? this.getTokenColor(token, editorElement)
577-
: this.getDefaultColor(editorElement)
578-
579576
x = drawToken(
580-
context, token.text, color, x, y, charWidth, charHeight, ignoreWhitespacesInTokens
577+
context, token.text, getTokenColor(token), x, y, charWidth, charHeight, ignoreWhitespacesInTokens
581578
)
582579
}
583580
})

0 commit comments

Comments
 (0)