Skip to content

Commit 436f4b7

Browse files
committed
fix: refactor the parameters of drawLines out of loop
1 parent 5059ea3 commit 436f4b7

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

lib/mixins/canvas-drawer.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,28 @@ export default class CanvasDrawer extends Mixin {
157157
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
158158

159159
// redrawRangesOnLayer inlined (this.redrawRangesOnLayer(this.tokensLayer, intactRanges, firstRow, lastRow, this.drawLines))
160-
const layer = this.tokensLayer
161160

162161
const devicePixelRatio = this.minimap.getDevicePixelRatio()
163162
const lineHeight = this.minimap.getLineHeight() * devicePixelRatio
163+
const charHeight = this.minimap.getCharHeight() * devicePixelRatio
164+
const charWidth = this.minimap.getCharWidth() * devicePixelRatio
165+
const canvasWidth = this.tokensLayer.getSize().width
166+
const context = this.tokensLayer.context
167+
const editor = this.minimap.getTextEditor()
168+
const editorElement = this.minimap.getTextEditorElement()
169+
const displayCodeHighlights = this.displayCodeHighlights
170+
const ignoreWhitespacesInTokens = this.ignoreWhitespacesInTokens
171+
const maxTokensInOneLine = this.maxTokensInOneLine
164172

165-
layer.clearCanvas()
173+
this.tokensLayer.clearCanvas()
166174

167175
if (intactRanges.length === 0) {
168-
this.drawLines(firstRow, lastRow, 0)
176+
this.drawLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine)
169177
} else {
170178
for (let j = 0, len = intactRanges.length; j < len; j++) {
171179
const intact = intactRanges[j]
172180

173-
layer.copyPartFromOffscreen(
181+
this.tokensLayer.copyPartFromOffscreen(
174182
intact.offscreenRow * lineHeight,
175183
(intact.start - firstRow) * lineHeight,
176184
(intact.end - intact.start) * lineHeight
@@ -181,17 +189,17 @@ export default class CanvasDrawer extends Mixin {
181189
for (let i = 0, len = intactRanges.length; i < len; i++) {
182190
const range = intactRanges[i]
183191

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

186194
currentRow = range.end
187195
}
188196
if (currentRow <= lastRow) {
189-
this.drawLines(currentRow, lastRow, currentRow - firstRow)
197+
this.drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine)
190198
}
191199
}
192200

193-
layer.resetOffscreenSize()
194-
layer.copyToOffscreen()
201+
this.tokensLayer.resetOffscreenSize()
202+
this.tokensLayer.copyToOffscreen()
195203
}
196204

197205
/**
@@ -459,25 +467,26 @@ export default class CanvasDrawer extends Mixin {
459467
* @param {number} lastRow the last row to render
460468
* @param {number} offsetRow the relative offset to apply to rows when
461469
* rendering them
470+
* @param {number} lineHeight this.minimap.getLineHeight() * devicePixelRatio
471+
* @param {number} charHeight this.minimap.getCharHeight() * devicePixelRatio
472+
* @param {number} charWidth this.minimap.getCharWidth() * devicePixelRatio
473+
* @param {number} canvasWidth this.tokensLayer.getSize().width
474+
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
475+
* @param {TextEditor} editor this.minimap.getTextEditor()
476+
* @param {TextEditorElement} editorElement this.minimap.getTextEditorElement()
477+
* @param {boolean} displayCodeHighlights this.displayCodeHighlights
478+
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
479+
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
462480
* @access private
463481
*/
464-
drawLines (firstRow, lastRow, offsetRow) {
465-
if (firstRow > lastRow) { return }
466-
467-
const devicePixelRatio = this.minimap.getDevicePixelRatio()
468-
const lineHeight = this.minimap.getLineHeight() * devicePixelRatio
469-
const charHeight = this.minimap.getCharHeight() * devicePixelRatio
470-
const charWidth = this.minimap.getCharWidth() * devicePixelRatio
471-
const displayCodeHighlights = this.displayCodeHighlights
472-
const context = this.tokensLayer.context
473-
const { width: canvasWidth } = this.tokensLayer.getSize()
482+
drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine) {
483+
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
474484

475-
const editor = this.minimap.getTextEditor()
476-
const editorElement = this.minimap.getTextEditorElement()
485+
if (firstRow > lastRow) { return }
477486

478487
let lastLine, x
479488
let y = (offsetRow * lineHeight) - lineHeight
480-
eachTokenForScreenRows(firstRow, lastRow, editor, this.maxTokensInOneLine, (line, token) => {
489+
eachTokenForScreenRows(firstRow, lastRow, editor, maxTokensInOneLine, (line, token) => {
481490
if (lastLine !== line) {
482491
x = 0
483492
y += lineHeight
@@ -494,7 +503,7 @@ export default class CanvasDrawer extends Mixin {
494503
: this.getDefaultColor(editorElement)
495504

496505
x = drawToken(
497-
context, token.text, color, x, y, charWidth, charHeight, this.ignoreWhitespacesInTokens
506+
context, token.text, color, x, y, charWidth, charHeight, ignoreWhitespacesInTokens
498507
)
499508
}
500509
})

0 commit comments

Comments
 (0)