Skip to content

Commit 9359279

Browse files
committed
fix: factor editorScreenLineCount and invisibleRegExp out of the loop
1 parent 7e72819 commit 9359279

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/mixins/canvas-drawer.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export default class CanvasDrawer extends Mixin {
134134
// TODO avoid closure: https://stackoverflow.com/a/46256398/7910299
135135
const getTokenColor = this.displayCodeHighlights ? (t) => this.getTokenColor(t, editorElement) : () => this.getDefaultColor(editorElement)
136136

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

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

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

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

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

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

229229
currentRow = range.end
230230
}
231231
if (currentRow <= lastRow) {
232-
drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
232+
drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
233233
}
234234
}
235235

@@ -654,13 +654,14 @@ function drawToken (context, text, color, x, y, charWidth, charHeight, ignoreWhi
654654
* @param {number} startRow The start row
655655
* @param {number} endRow The end row
656656
* @param {TextEditor} editor
657+
* @param {number} editorScreenLineCount
658+
* @param {RegExp} invisibleRegExp
657659
* @param {number} maxTokensInOneLine the maximum number of tokens to render in one line
658660
* @return {Array<Array>} An array of tokens by line
659661
* @access private
660662
*/
661-
function eachTokenForScreenRows (startRow, endRow, editor, maxTokensInOneLine, callback) {
662-
const invisibleRegExp = getInvisibleRegExp(editor)
663-
endRow = Math.min(endRow, editor.getScreenLineCount())
663+
function eachTokenForScreenRows (startRow, endRow, editor, editorScreenLineCount, invisibleRegExp, maxTokensInOneLine, callback) {
664+
endRow = Math.min(endRow, editorScreenLineCount)
664665

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

703706
if (firstRow > lastRow) { return }
704707

705708
let lastLine, x
706709
let y = (offsetRow * lineHeight) - lineHeight
707-
eachTokenForScreenRows(firstRow, lastRow, editor, maxTokensInOneLine, (line, token) => {
710+
eachTokenForScreenRows(firstRow, lastRow, editor, editorScreenLineCount, invisibleRegExp, maxTokensInOneLine, (line, token) => {
708711
if (lastLine !== line) {
709712
x = 0
710713
y += lineHeight

0 commit comments

Comments
 (0)