Skip to content

Commit bf9f598

Browse files
committed
fix: make drawLines a free function
1 parent 09771b6 commit bf9f598

File tree

1 file changed

+59
-53
lines changed

1 file changed

+59
-53
lines changed

lib/mixins/canvas-drawer.js

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class CanvasDrawer extends Mixin {
2323
initializeCanvas () {
2424
if (SPEC_MODE) {
2525
// class methods only used for spying the calls
26+
this.drawLines = (firstLine, lastLine) => { console.log({ firstLine, lastLine }) }
2627
this.drawLineDecoration = drawLineDecoration
2728
this.drawGutterDecoration = drawGutterDecoration
2829
this.drawHighlightDecoration = drawHighlightDecoration
@@ -197,12 +198,17 @@ export default class CanvasDrawer extends Mixin {
197198

198199
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
199200

200-
// redrawRangesOnLayer inlined (this.redrawRangesOnLayer(this.tokensLayer, intactRanges, firstRow, lastRow, this.drawLines))
201+
// redrawRangesOnLayer inlined (this.redrawRangesOnLayer(this.tokensLayer, intactRanges, firstRow, lastRow, drawLines))
201202

202203
this.tokensLayer.clearCanvas()
203204

205+
if (SPEC_MODE) {
206+
// call the spy
207+
this.drawLines(firstRow, lastRow)
208+
}
209+
204210
if (intactRanges.length === 0) {
205-
this.drawLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
211+
drawLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
206212
} else {
207213
for (let j = 0, len = intactRanges.length; j < len; j++) {
208214
const intact = intactRanges[j]
@@ -218,12 +224,12 @@ export default class CanvasDrawer extends Mixin {
218224
for (let i = 0, len = intactRanges.length; i < len; i++) {
219225
const range = intactRanges[i]
220226

221-
this.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, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
222228

223229
currentRow = range.end
224230
}
225231
if (currentRow <= lastRow) {
226-
this.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, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
227233
}
228234
}
229235

@@ -439,7 +445,7 @@ export default class CanvasDrawer extends Mixin {
439445
// (intact.end - intact.start) * lineHeight
440446
// )
441447
// }
442-
// this.drawLinesForRanges(method, intactRanges, firstRow, lastRow)
448+
// drawLinesForRanges(method, intactRanges, firstRow, lastRow)
443449
// }
444450
//
445451
// layer.resetOffscreenSize()
@@ -533,54 +539,6 @@ export default class CanvasDrawer extends Mixin {
533539
renderData.context.fill()
534540
}
535541

536-
/**
537-
* Draws lines on the corresponding layer.
538-
*
539-
* The lines range to draw is specified by the `firstRow` and `lastRow`
540-
* parameters.
541-
*
542-
* @param {number} firstRow the first row to render
543-
* @param {number} lastRow the last row to render
544-
* @param {number} offsetRow the relative offset to apply to rows when
545-
* rendering them
546-
* @param {number} lineHeight this.minimap.getLineHeight() * devicePixelRatio
547-
* @param {number} charHeight this.minimap.getCharHeight() * devicePixelRatio
548-
* @param {number} charWidth this.minimap.getCharWidth() * devicePixelRatio
549-
* @param {number} canvasWidth this.tokensLayer.getSize().width
550-
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
551-
* @param {TextEditor} editor this.minimap.getTextEditor()
552-
* @param {(t: Token) => string} getTokenColor
553-
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
554-
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
555-
* @access private
556-
*/
557-
drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
558-
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
559-
560-
if (firstRow > lastRow) { return }
561-
562-
let lastLine, x
563-
let y = (offsetRow * lineHeight) - lineHeight
564-
eachTokenForScreenRows(firstRow, lastRow, editor, maxTokensInOneLine, (line, token) => {
565-
if (lastLine !== line) {
566-
x = 0
567-
y += lineHeight
568-
lastLine = line
569-
context.clearRect(x, y, canvasWidth, lineHeight)
570-
}
571-
if (x > canvasWidth) { return }
572-
573-
if (/^\s+$/.test(token.text)) {
574-
x += token.text.length * charWidth
575-
} else {
576-
x = drawToken(
577-
context, token.text, getTokenColor(token), x, y, charWidth, charHeight, ignoreWhitespacesInTokens
578-
)
579-
}
580-
})
581-
context.fill()
582-
}
583-
584542
/**
585543
* Draws the specified decorations for the current `screenRow`.
586544
*
@@ -718,6 +676,54 @@ function eachTokenForScreenRows (startRow, endRow, editor, maxTokensInOneLine, c
718676
}
719677
}
720678

679+
/**
680+
* Draws lines on the corresponding layer.
681+
*
682+
* The lines range to draw is specified by the `firstRow` and `lastRow`
683+
* parameters.
684+
*
685+
* @param {number} firstRow the first row to render
686+
* @param {number} lastRow the last row to render
687+
* @param {number} offsetRow the relative offset to apply to rows when
688+
* rendering them
689+
* @param {number} lineHeight this.minimap.getLineHeight() * devicePixelRatio
690+
* @param {number} charHeight this.minimap.getCharHeight() * devicePixelRatio
691+
* @param {number} charWidth this.minimap.getCharWidth() * devicePixelRatio
692+
* @param {number} canvasWidth this.tokensLayer.getSize().width
693+
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
694+
* @param {TextEditor} editor this.minimap.getTextEditor()
695+
* @param {(t: Token) => string} getTokenColor
696+
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
697+
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
698+
* @access private
699+
*/
700+
function drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
701+
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
702+
703+
if (firstRow > lastRow) { return }
704+
705+
let lastLine, x
706+
let y = (offsetRow * lineHeight) - lineHeight
707+
eachTokenForScreenRows(firstRow, lastRow, editor, maxTokensInOneLine, (line, token) => {
708+
if (lastLine !== line) {
709+
x = 0
710+
y += lineHeight
711+
lastLine = line
712+
context.clearRect(x, y, canvasWidth, lineHeight)
713+
}
714+
if (x > canvasWidth) { return }
715+
716+
if (/^\s+$/.test(token.text)) {
717+
x += token.text.length * charWidth
718+
} else {
719+
x = drawToken(
720+
context, token.text, getTokenColor(token), x, y, charWidth, charHeight, ignoreWhitespacesInTokens
721+
)
722+
}
723+
})
724+
context.fill()
725+
}
726+
721727
/**
722728
* Returns the regexp to replace invisibles substitution characters
723729
* in editor lines.

0 commit comments

Comments
 (0)