Skip to content

Commit

Permalink
fix: make drawLines a free function
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 2, 2021
1 parent 09771b6 commit bf9f598
Showing 1 changed file with 59 additions and 53 deletions.
112 changes: 59 additions & 53 deletions lib/mixins/canvas-drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class CanvasDrawer extends Mixin {
initializeCanvas () {
if (SPEC_MODE) {
// class methods only used for spying the calls
this.drawLines = (firstLine, lastLine) => { console.log({ firstLine, lastLine }) }
this.drawLineDecoration = drawLineDecoration
this.drawGutterDecoration = drawGutterDecoration
this.drawHighlightDecoration = drawHighlightDecoration
Expand Down Expand Up @@ -197,12 +198,17 @@ export default class CanvasDrawer extends Mixin {

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

// redrawRangesOnLayer inlined (this.redrawRangesOnLayer(this.tokensLayer, intactRanges, firstRow, lastRow, this.drawLines))
// redrawRangesOnLayer inlined (this.redrawRangesOnLayer(this.tokensLayer, intactRanges, firstRow, lastRow, drawLines))

this.tokensLayer.clearCanvas()

if (SPEC_MODE) {
// call the spy
this.drawLines(firstRow, lastRow)
}

if (intactRanges.length === 0) {
this.drawLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
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 @@ -218,12 +224,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, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
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, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
}
}

Expand Down Expand Up @@ -439,7 +445,7 @@ export default class CanvasDrawer extends Mixin {
// (intact.end - intact.start) * lineHeight
// )
// }
// this.drawLinesForRanges(method, intactRanges, firstRow, lastRow)
// drawLinesForRanges(method, intactRanges, firstRow, lastRow)
// }
//
// layer.resetOffscreenSize()
Expand Down Expand Up @@ -533,54 +539,6 @@ export default class CanvasDrawer extends Mixin {
renderData.context.fill()
}

/**
* Draws lines on the corresponding layer.
*
* The lines range to draw is specified by the `firstRow` and `lastRow`
* parameters.
*
* @param {number} firstRow the first row to render
* @param {number} lastRow the last row to render
* @param {number} offsetRow the relative offset to apply to rows when
* rendering them
* @param {number} lineHeight this.minimap.getLineHeight() * devicePixelRatio
* @param {number} charHeight this.minimap.getCharHeight() * devicePixelRatio
* @param {number} charWidth this.minimap.getCharWidth() * devicePixelRatio
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
* @param {TextEditor} editor this.minimap.getTextEditor()
* @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, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.

if (firstRow > lastRow) { return }

let lastLine, x
let y = (offsetRow * lineHeight) - lineHeight
eachTokenForScreenRows(firstRow, lastRow, editor, maxTokensInOneLine, (line, token) => {
if (lastLine !== line) {
x = 0
y += lineHeight
lastLine = line
context.clearRect(x, y, canvasWidth, lineHeight)
}
if (x > canvasWidth) { return }

if (/^\s+$/.test(token.text)) {
x += token.text.length * charWidth
} else {
x = drawToken(
context, token.text, getTokenColor(token), x, y, charWidth, charHeight, ignoreWhitespacesInTokens
)
}
})
context.fill()
}

/**
* Draws the specified decorations for the current `screenRow`.
*
Expand Down Expand Up @@ -718,6 +676,54 @@ function eachTokenForScreenRows (startRow, endRow, editor, maxTokensInOneLine, c
}
}

/**
* Draws lines on the corresponding layer.
*
* The lines range to draw is specified by the `firstRow` and `lastRow`
* parameters.
*
* @param {number} firstRow the first row to render
* @param {number} lastRow the last row to render
* @param {number} offsetRow the relative offset to apply to rows when
* rendering them
* @param {number} lineHeight this.minimap.getLineHeight() * devicePixelRatio
* @param {number} charHeight this.minimap.getCharHeight() * devicePixelRatio
* @param {number} charWidth this.minimap.getCharWidth() * devicePixelRatio
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
* @param {TextEditor} editor this.minimap.getTextEditor()
* @param {(t: Token) => string} getTokenColor
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
* @access private
*/
function 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 }

let lastLine, x
let y = (offsetRow * lineHeight) - lineHeight
eachTokenForScreenRows(firstRow, lastRow, editor, maxTokensInOneLine, (line, token) => {
if (lastLine !== line) {
x = 0
y += lineHeight
lastLine = line
context.clearRect(x, y, canvasWidth, lineHeight)
}
if (x > canvasWidth) { return }

if (/^\s+$/.test(token.text)) {
x += token.text.length * charWidth
} else {
x = drawToken(
context, token.text, getTokenColor(token), x, y, charWidth, charHeight, ignoreWhitespacesInTokens
)
}
})
context.fill()
}

/**
* Returns the regexp to replace invisibles substitution characters
* in editor lines.
Expand Down

0 comments on commit bf9f598

Please sign in to comment.