Skip to content

Commit c22ae67

Browse files
authored
Merge pull request #748 from atom-minimap/inline-updateTokensLayer
2 parents 552edf2 + 063b036 commit c22ae67

File tree

6 files changed

+84
-71
lines changed

6 files changed

+84
-71
lines changed

lib/mixins/canvas-drawer.js

Lines changed: 79 additions & 70 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
@@ -130,7 +131,10 @@ export default class CanvasDrawer extends Mixin {
130131
const editor = this.minimap.getTextEditor()
131132
const editorElement = this.minimap.getTextEditorElement()
132133

133-
this.updateTokensLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, this.tokensLayer.context, editor, editorElement, this.displayCodeHighlights, this.ignoreWhitespacesInTokens, this.maxTokensInOneLine)
134+
// TODO avoid closure: https://stackoverflow.com/a/46256398/7910299
135+
const getTokenColor = this.displayCodeHighlights ? (t) => this.getTokenColor(t, editorElement) : () => this.getDefaultColor(editorElement)
136+
137+
this.updateTokensLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, this.tokensLayer.context, editor, getTokenColor, this.ignoreWhitespacesInTokens, this.maxTokensInOneLine)
134138

135139
const decorations = this.minimap.decorationsByTypeThenRows(firstRow, lastRow)
136140

@@ -144,6 +148,10 @@ export default class CanvasDrawer extends Mixin {
144148
orders: Main.getPluginsOrder()
145149
}
146150

151+
const drawCustomDecorationLambda = (decoration, data, decorationColor) => drawCustomDecoration(decoration, data, decorationColor, editorElement)
152+
backgroundDecorationDispatcher['background-custom'] = drawCustomDecorationLambda
153+
frontDecorationDispatcher['foreground-custom'] = drawCustomDecorationLambda
154+
147155
this.updateBackDecorationsLayer(firstRow, lastRow, renderData, lineHeight, editorElement, decorations)
148156

149157
renderData.context = this.frontLayer.context
@@ -180,23 +188,27 @@ export default class CanvasDrawer extends Mixin {
180188
* @param {number} canvasWidth this.tokensLayer.getSize().width
181189
* @param {CanvasRenderingContext2D} context this.tokensLayer.context
182190
* @param {TextEditor} editor this.minimap.getTextEditor()
183-
* @param {TextEditorElement} editorElement this.minimap.getTextEditorElement()
184-
* @param {boolean} displayCodeHighlights this.displayCodeHighlights
191+
* @param {(t: Token) => string} getTokenColor
185192
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
186193
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
187194
* @access private
188195
*/
189-
updateTokensLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine) {
196+
updateTokensLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine) {
190197
const intactRanges = computeIntactRanges(firstRow, lastRow, this.pendingChanges, this.offscreenFirstRow, this.offscreenLastRow)
191198

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

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

196203
this.tokensLayer.clearCanvas()
197204

205+
if (SPEC_MODE) {
206+
// call the spy
207+
this.drawLines(firstRow, lastRow)
208+
}
209+
198210
if (intactRanges.length === 0) {
199-
this.drawLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine)
211+
drawLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
200212
} else {
201213
for (let j = 0, len = intactRanges.length; j < len; j++) {
202214
const intact = intactRanges[j]
@@ -212,12 +224,12 @@ export default class CanvasDrawer extends Mixin {
212224
for (let i = 0, len = intactRanges.length; i < len; i++) {
213225
const range = intactRanges[i]
214226

215-
this.drawLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine)
227+
drawLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
216228

217229
currentRow = range.end
218230
}
219231
if (currentRow <= lastRow) {
220-
this.drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine)
232+
drawLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, getTokenColor, ignoreWhitespacesInTokens, maxTokensInOneLine)
221233
}
222234
}
223235

@@ -433,7 +445,7 @@ export default class CanvasDrawer extends Mixin {
433445
// (intact.end - intact.start) * lineHeight
434446
// )
435447
// }
436-
// this.drawLinesForRanges(method, intactRanges, firstRow, lastRow)
448+
// drawLinesForRanges(method, intactRanges, firstRow, lastRow)
437449
// }
438450
//
439451
// layer.resetOffscreenSize()
@@ -485,9 +497,6 @@ export default class CanvasDrawer extends Mixin {
485497
drawBackDecorationsForLines (firstRow, lastRow, offsetRow, renderData, lineHeight, editorElement, decorations) {
486498
if (firstRow > lastRow) { return }
487499

488-
const drawCustomDecorationLambda = (decoration, data, decorationColor) => drawCustomDecoration(decoration, data, decorationColor, editorElement)
489-
backgroundDecorationDispatcher['background-custom'] = drawCustomDecorationLambda
490-
491500
for (let screenRow = firstRow; screenRow <= lastRow; screenRow++) {
492501
renderData.row = offsetRow + (screenRow - firstRow)
493502
renderData.yRow = renderData.row * lineHeight
@@ -519,9 +528,6 @@ export default class CanvasDrawer extends Mixin {
519528
drawFrontDecorationsForLines (firstRow, lastRow, offsetRow, renderData, lineHeight, editorElement, decorations) {
520529
if (firstRow > lastRow) { return }
521530

522-
const drawCustomDecorationLambda = (decoration, data, decorationColor) => drawCustomDecoration(decoration, data, decorationColor, editorElement)
523-
frontDecorationDispatcher['foreground-custom'] = drawCustomDecorationLambda
524-
525531
for (let screenRow = firstRow; screenRow <= lastRow; screenRow++) {
526532
renderData.row = offsetRow + (screenRow - firstRow)
527533
renderData.yRow = renderData.row * lineHeight
@@ -533,59 +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 {TextEditorElement} editorElement this.minimap.getTextEditorElement()
553-
* @param {boolean} displayCodeHighlights this.displayCodeHighlights
554-
* @param {boolean} ignoreWhitespacesInTokens this.ignoreWhitespacesInTokens
555-
* @param {number} maxTokensInOneLine this.maxTokensInOneLine
556-
* @access private
557-
*/
558-
drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine) {
559-
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
560-
561-
if (firstRow > lastRow) { return }
562-
563-
let lastLine, x
564-
let y = (offsetRow * lineHeight) - lineHeight
565-
eachTokenForScreenRows(firstRow, lastRow, editor, maxTokensInOneLine, (line, token) => {
566-
if (lastLine !== line) {
567-
x = 0
568-
y += lineHeight
569-
lastLine = line
570-
context.clearRect(x, y, canvasWidth, lineHeight)
571-
}
572-
if (x > canvasWidth) { return }
573-
574-
if (/^\s+$/.test(token.text)) {
575-
x += token.text.length * charWidth
576-
} else {
577-
const color = displayCodeHighlights
578-
? this.getTokenColor(token, editorElement)
579-
: this.getDefaultColor(editorElement)
580-
581-
x = drawToken(
582-
context, token.text, color, x, y, charWidth, charHeight, ignoreWhitespacesInTokens
583-
)
584-
}
585-
})
586-
context.fill()
587-
}
588-
589542
/**
590543
* Draws the specified decorations for the current `screenRow`.
591544
*
@@ -723,6 +676,54 @@ function eachTokenForScreenRows (startRow, endRow, editor, maxTokensInOneLine, c
723676
}
724677
}
725678

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+
726727
/**
727728
* Returns the regexp to replace invisibles substitution characters
728729
* in editor lines.
@@ -747,13 +748,21 @@ function getInvisibleRegExp (editor) {
747748
}
748749
}
749750

750-
// dispatchers for decoration drawing (custom decoration drawer added dynamically)
751-
751+
/**
752+
* dispatchers for decoration drawing (custom decoration drawer added dynamically)
753+
* @param {Object} backgroundDecorationDispatcher an object with the type to render as key and the
754+
* render method as value
755+
*/
752756
const backgroundDecorationDispatcher = {
753757
line: drawLineDecoration,
754758
'highlight-under': drawHighlightDecoration
755759
}
756760

761+
/**
762+
* dispatchers for decoration drawing (custom decoration drawer added dynamically)
763+
* @param {Object} frontDecorationDispatcher an object with the type to render as key and the
764+
* render method as value
765+
*/
757766
const frontDecorationDispatcher = {
758767
gutter: drawGutterDecoration,
759768
'highlight-over': drawHighlightDecoration,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"build": "cross-env NODE_ENV=production parcel build --target main lib/main.js",
1717
"build-commit": "npm run clean && build-commit -o dist",
1818
"esdoc": "esdoc -c esdoc.json",
19-
"test": "cross-env NODE_ENV=test atom --test spec",
19+
"test": "atom --test spec",
2020
"bump": "ncu -u",
2121
"prepare": "npm run build"
2222
},

spec/canvas-layer-spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
process.env.NODE_ENV = 'test'
23

34
const CanvasLayer = require('../lib/canvas-layer')
45

spec/minimap-element-spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
process.env.NODE_ENV = 'test'
23

34
const fs = require('fs-plus')
45
const Main = require('../dist/main')

spec/minimap-main-spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
process.env.NODE_ENV = 'test'
23

34
require('./helpers/workspace')
45
const { Minimap } = require('../dist/main')

spec/minimap-spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
process.env.NODE_ENV = 'test'
23

34
require('./helpers/workspace')
45

0 commit comments

Comments
 (0)