Skip to content

Commit 85330a7

Browse files
authored
Merge pull request #749 from atom-minimap/canvas-drawer-optimizations
2 parents 7e72819 + 703c75c commit 85330a7

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

lib/dom-styles-reader.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default class DOMStylesReader {
5858
for (let i = 0, len = scopes.length; i < len; i++) {
5959
const scope = scopes[i]
6060
const node = document.createElement('span')
61-
node.className = scope.replace(/\.+/g, ' ')
61+
node.className = scope.replace(dotRegexp, ' ')
6262

6363
if (parent != null) { parent.appendChild(node) }
6464

@@ -132,6 +132,10 @@ export default class DOMStylesReader {
132132
// ## ## ## ## ## ## ## ## ## ##
133133
// ## ## ######## ######## ## ######## ## ## ######
134134

135+
const dotRegexp = /\.+/g
136+
const rgbExtractRegexp = /rgb(a?)\((\d+), (\d+), (\d+)(, (\d+(\.\d+)?))?\)/
137+
const hueRegexp = /hue-rotate\((\d+)deg\)/
138+
135139
/**
136140
* Computes the output color of `value` with a rotated hue defined
137141
* in `filter`.
@@ -142,10 +146,10 @@ export default class DOMStylesReader {
142146
* @access private
143147
*/
144148
function rotateHue (value, filter) {
145-
const match = value.match(/rgb(a?)\((\d+), (\d+), (\d+)(, (\d+(\.\d+)?))?\)/)
149+
const match = value.match(rgbExtractRegexp)
146150
let [, , r, g, b, , a] = match
147151

148-
let [, hue] = filter.match(/hue-rotate\((\d+)deg\)/)
152+
let [, hue] = filter.match(hueRegexp)
149153

150154
;[r, g, b, a, hue] = [r, g, b, a, hue].map(Number)
151155
;[r, g, b] = rotate(r, g, b, hue)

lib/mixins/canvas-drawer.js

Lines changed: 18 additions & 12 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

@@ -401,7 +401,7 @@ export default class CanvasDrawer extends Mixin {
401401
if (properties.color) { return properties.color }
402402

403403
if (properties.scope) {
404-
const scopeString = properties.scope.split(/\s+/)
404+
const scopeString = properties.scope.split(whiteSpaceRegexp)
405405
return this.DOMStylesReader.retrieveStyleFromDom(scopeString, 'background-color', editorElement, true)
406406
} else {
407407
return this.getDefaultColor(editorElement)
@@ -605,6 +605,9 @@ export default class CanvasDrawer extends Mixin {
605605
// ## ## ## ## ## ## ## ## ##
606606
// ######## ## ## ## ## ### ###
607607

608+
const emptyLineRegexp = /^\s+$/
609+
const whiteSpaceRegexp = /\s+/
610+
608611
/**
609612
* Draws a single token on the given context.
610613
*
@@ -654,13 +657,14 @@ function drawToken (context, text, color, x, y, charWidth, charHeight, ignoreWhi
654657
* @param {number} startRow The start row
655658
* @param {number} endRow The end row
656659
* @param {TextEditor} editor
660+
* @param {number} editorScreenLineCount
661+
* @param {RegExp} invisibleRegExp
657662
* @param {number} maxTokensInOneLine the maximum number of tokens to render in one line
658663
* @return {Array<Array>} An array of tokens by line
659664
* @access private
660665
*/
661-
function eachTokenForScreenRows (startRow, endRow, editor, maxTokensInOneLine, callback) {
662-
const invisibleRegExp = getInvisibleRegExp(editor)
663-
endRow = Math.min(endRow, editor.getScreenLineCount())
666+
function eachTokenForScreenRows (startRow, endRow, editor, editorScreenLineCount, invisibleRegExp, maxTokensInOneLine, callback) {
667+
endRow = Math.min(endRow, editorScreenLineCount)
664668

665669
for (let row = startRow; row < endRow; row++) {
666670
const editorTokensForScreenRow = editor.tokensForScreenRow(row)
@@ -692,19 +696,21 @@ function eachTokenForScreenRows (startRow, endRow, editor, maxTokensInOneLine, c
692696
* @param {number} canvasWidth this.tokensLayer.getSize().width
693697
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
694698
* @param {TextEditor} editor this.minimap.getTextEditor()
699+
* @param {number} editorScreenLineCount
700+
* @param {RegExp} invisibleRegExp
695701
* @param {(t: Token) => string} getTokenColor
696702
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
697703
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
698704
* @access private
699705
*/
700-
function drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
706+
function drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorScreenLineCount, invisibleRegExp, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
701707
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
702708

703709
if (firstRow > lastRow) { return }
704710

705711
let lastLine, x
706712
let y = (offsetRow * lineHeight) - lineHeight
707-
eachTokenForScreenRows(firstRow, lastRow, editor, maxTokensInOneLine, (line, token) => {
713+
eachTokenForScreenRows(firstRow, lastRow, editor, editorScreenLineCount, invisibleRegExp, maxTokensInOneLine, (line, token) => {
708714
if (lastLine !== line) {
709715
x = 0
710716
y += lineHeight
@@ -713,7 +719,7 @@ function drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWi
713719
}
714720
if (x > canvasWidth) { return }
715721

716-
if (/^\s+$/.test(token.text)) {
722+
if (emptyLineRegexp.test(token.text)) {
717723
x += token.text.length * charWidth
718724
} else {
719725
x = drawToken(

0 commit comments

Comments
 (0)