-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathcanvas-drawer.js
1283 lines (1155 loc) · 39.3 KB
/
canvas-drawer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict"
import { escapeRegExp } from "../deps/underscore-plus"
import Mixin from "mixto"
import * as Main from "../main"
import { styleReader } from "../main"
import CanvasLayer from "../canvas-layer"
const SPEC_MODE = atom.inSpecMode()
// an instance of MinimapElement used for testing and calling spies
let thisSpec
/**
* The `CanvasDrawer` mixin is responsible for the rendering of a `Minimap` in a `canvas` element.
*
* This mixin is injected in the `MinimapElement` prototype, so all these methods are available on any `MinimapElement` instance.
*/
export default class CanvasDrawer extends Mixin {
/** Initializes the canvas elements needed to perform the `Minimap` rendering. */
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
this.drawHighlightOutlineDecoration = drawHighlightOutlineDecoration
this.drawCustomDecoration = drawCustomDecoration
this.StyleReader = styleReader
thisSpec = this
}
/**
* The main canvas layer where lines are rendered.
*
* @type {CanvasLayer}
*/
this.tokensLayer = new CanvasLayer()
/**
* The canvas layer for decorations below the text.
*
* @type {CanvasLayer}
*/
this.backLayer = new CanvasLayer()
/**
* The canvas layer for decorations above the text.
*
* @type {CanvasLayer}
*/
this.frontLayer = new CanvasLayer()
if (!this.pendingChanges) {
/**
* Stores the changes from the text editor.
*
* @type {Object[]}
* @access private
*/
this.pendingChanges = []
}
if (!this.pendingBackDecorationChanges) {
/**
* Stores the changes from the minimap back decorations.
*
* @type {Object[]}
* @access private
*/
this.pendingBackDecorationChanges = []
}
if (!this.pendingFrontDecorationChanges) {
/**
* Stores the changes from the minimap front decorations.
*
* @type {Object[]}
* @access private
*/
this.pendingFrontDecorationChanges = []
}
// the maximum number of tokens to render in one line
this.maxTokensInOneLine = atom.config.get("minimap.maxTokensInOneLine")
}
/**
* Returns the uppermost canvas in the MinimapElement.
*
* @returns {HTMLCanvasElement} The html canvas element
*/
getFrontCanvas() {
return this.frontLayer.canvas
}
/**
* Attaches the canvases into the specified container.
*
* @param {HTMLElement} parent The canvases' container
* @access private
*/
attachCanvases(parent) {
this.backLayer.attach(parent)
this.tokensLayer.attach(parent)
this.frontLayer.attach(parent)
}
/**
* Changes the size of all the canvas layers at once.
*
* @param {number} width The new width for the three canvases
* @param {number} height The new height for the three canvases
* @access private
*/
setCanvasesSize(width, height) {
this.backLayer.setSize(width, height)
this.tokensLayer.setSize(width, height)
this.frontLayer.setSize(width, height)
}
/** Performs an update of the rendered `Minimap` based on the changes registered in the instance. */
updateCanvas() {
const firstRow = this.minimap.getFirstVisibleScreenRow()
const lastRow = this.minimap.getLastVisibleScreenRow()
const devicePixelRatio = this.minimap.getDevicePixelRatio()
const lineHeight = this.minimap.getLineHeight() * devicePixelRatio
const charHeight = this.minimap.getCharHeight() * devicePixelRatio
const charWidth = this.minimap.getCharWidth() * devicePixelRatio
const { width: canvasWidth, height: canvasHeight } = this.tokensLayer.getSize()
const editor = this.minimap.getTextEditor()
const editorElement = this.minimap.getTextEditorElement()
// TODO avoid closure: https://stackoverflow.com/a/46256398/7910299
const getTokenColorClosure = this.displayCodeHighlights
? (scopes) => getTokenColor(scopes, editorElement, this.textOpacity)
: () => getDefaultColor(editorElement, this.textOpacity)
updateTokensLayer(
this.tokensLayer,
firstRow,
lastRow,
this.offscreenFirstRow,
this.offscreenLastRow,
this.pendingChanges,
lineHeight,
charHeight,
charWidth,
canvasWidth,
editor,
editor.getScreenLineCount(),
getInvisibleRegExp(editor),
getTokenColorClosure,
this.ignoreWhitespacesInTokens,
this.maxTokensInOneLine
)
if (SPEC_MODE) {
// call the spy for drawLines which is used inside updateTokensLayer
this.drawLines(firstRow, lastRow)
}
const decorations = this.DecorationManagement.decorationsByTypeThenRows(firstRow, lastRow)
const renderData = {
context: this.backLayer.context,
canvasWidth,
canvasHeight,
lineHeight,
charWidth,
charHeight,
orders: Main.getPluginsOrder(),
}
const drawCustomDecorationLambda = (decoration, data, decorationColor) =>
drawCustomDecoration(decoration, data, decorationColor, editorElement)
backgroundDecorationDispatcher["background-custom"] = drawCustomDecorationLambda
frontDecorationDispatcher["foreground-custom"] = drawCustomDecorationLambda
updateBackDecorationsLayer(
this.backLayer,
firstRow,
lastRow,
this.offscreenFirstRow,
this.offscreenLastRow,
this.pendingBackDecorationChanges,
renderData,
lineHeight,
editorElement,
decorations
)
renderData.context = this.frontLayer.context
updateFrontDecorationsLayer(
this.frontLayer,
firstRow,
lastRow,
this.offscreenFirstRow,
this.offscreenLastRow,
this.pendingFrontDecorationChanges,
renderData,
lineHeight,
editorElement,
decorations
)
this.pendingChanges = []
this.pendingBackDecorationChanges = []
this.pendingFrontDecorationChanges = []
/**
* The first row in the last render of the offscreen canvas.
*
* @type {number}
* @access private
*/
this.offscreenFirstRow = firstRow
/**
* The last row in the last render of the offscreen canvas.
*
* @type {number}
* @access private
*/
this.offscreenLastRow = lastRow
}
// ######## ######## ### ## ##
// ## ## ## ## ## ## ## ## ##
// ## ## ## ## ## ## ## ## ##
// ## ## ######## ## ## ## ## ##
// ## ## ## ## ######### ## ## ##
// ## ## ## ## ## ## ## ## ##
// ######## ## ## ## ## ### ###
/**
* Routine used to render changes in specific ranges for one layer.
*
* @param {CanvasLayer} layer The layer to redraw
* @param {Object[]} intactRanges An array of the ranges to leave intact
* @param {number} firstRow FirstRow the first row of the range to update
* @param {number} lastRow LastRow the last row of the range to update
* @param {Function} method The render method to use for the lines drawing
* @access private Unused (inlined the code for performance reasons)
*/
// redrawRangesOnLayer (layer, intactRanges, firstRow, lastRow, method) {
// const devicePixelRatio = this.minimap.getDevicePixelRatio()
// const lineHeight = this.minimap.getLineHeight() * devicePixelRatio
//
// layer.clearCanvas()
//
// if (intactRanges.length === 0) {
// method.call(this, firstRow, lastRow, 0)
// } else {
// for (let j = 0, len = intactRanges.length; j < len; j++) {
// const intact = intactRanges[j]
//
// layer.copyPartFromOffscreen(
// intact.offscreenRow * lineHeight,
// (intact.start - firstRow) * lineHeight,
// (intact.end - intact.start) * lineHeight
// )
// }
// drawLinesForRanges(method, intactRanges, firstRow, lastRow)
// }
//
// layer.resetOffscreenSize()
// layer.copyToOffscreen()
// }
/**
* Renders the lines between the intact ranges when an update has pending changes.
*
* @param {Function} method The render method to use for the lines drawing
* @param {Object[]} intactRanges The intact ranges in the minimap
* @param {number} firstRow The first row of the rendered region
* @param {number} lastRow The last row of the rendered region
* @access private Unused (inlined the code for performance reasons)
*/
// drawLinesForRanges (method, ranges, firstRow, lastRow) {
// let currentRow = firstRow
// for (let i = 0, len = ranges.length; i < len; i++) {
// const range = ranges[i]
//
// method.call(this, currentRow, range.start, currentRow - firstRow)
//
// currentRow = range.end
// }
// if (currentRow <= lastRow) {
// method.call(this, currentRow, lastRow, currentRow - firstRow)
// }
// }
}
// ######## ######## ### ## ##
// ## ## ## ## ## ## ## ## ##
// ## ## ## ## ## ## ## ## ##
// ## ## ######## ## ## ## ## ##
// ## ## ## ## ######### ## ## ##
// ## ## ## ## ## ## ## ## ##
// ######## ## ## ## ## ### ###
/**
* Performs an update of the tokens layer using the pending changes array.
*
* @param {CanvasLayer} tokensLayer
* @param {number} firstRow FirstRow the first row of the range to update
* @param {number} lastRow LastRow the last row of the range to update
* @param {number} offscreenFirstRow
* @param {number} offscreenLastRow
* @param {Array<>} pendingChanges
* @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 {TextEditor} editor This.minimap.getTextEditor()
* @param {(t: Token) => string} getTokenColorClosure
* @param {boolean} ignoreWhitespacesInTokens This.ignoreWhitespacesInTokens
* @param {number} maxTokensInOneLine This.maxTokensInOneLine
* @access private
*/
function updateTokensLayer(
tokensLayer,
firstRow,
lastRow,
offscreenFirstRow,
offscreenLastRow,
pendingChanges,
lineHeight,
charHeight,
charWidth,
canvasWidth,
editor,
editorScreenLineCount,
invisibleRegExp,
getTokenColorClosure,
ignoreWhitespacesInTokens,
maxTokensInOneLine
) {
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
const intactRanges = computeIntactRanges(firstRow, lastRow, pendingChanges, offscreenFirstRow, offscreenLastRow)
// redrawRangesOnLayer
const context = tokensLayer.context
tokensLayer.clearCanvas()
if (intactRanges.length === 0) {
drawLines(
firstRow,
lastRow,
0,
lineHeight,
charHeight,
charWidth,
canvasWidth,
context,
editor,
editorScreenLineCount,
invisibleRegExp,
getTokenColorClosure,
ignoreWhitespacesInTokens,
maxTokensInOneLine
)
} else {
for (let j = 0, len = intactRanges.length; j < len; j++) {
const intact = intactRanges[j]
tokensLayer.copyPartFromOffscreen(
intact.offscreenRow * lineHeight,
(intact.start - firstRow) * lineHeight,
(intact.end - intact.start) * lineHeight
)
}
// drawLinesForRanges
let currentRow = firstRow
for (let i = 0, len = intactRanges.length; i < len; i++) {
const range = intactRanges[i]
drawLines(
currentRow,
range.start,
currentRow - firstRow,
lineHeight,
charHeight,
charWidth,
canvasWidth,
context,
editor,
editorScreenLineCount,
invisibleRegExp,
getTokenColorClosure,
ignoreWhitespacesInTokens,
maxTokensInOneLine
)
currentRow = range.end
}
if (currentRow <= lastRow) {
drawLines(
currentRow,
lastRow,
currentRow - firstRow,
lineHeight,
charHeight,
charWidth,
canvasWidth,
context,
editor,
editorScreenLineCount,
invisibleRegExp,
getTokenColorClosure,
ignoreWhitespacesInTokens,
maxTokensInOneLine
)
}
}
tokensLayer.resetOffscreenSize()
tokensLayer.copyToOffscreen()
}
/**
* Performs an update of the back decorations layer using the pending back decorations changes arrays.
*
* @param {CanvasLayer} backLayer
* @param {number} firstRow FirstRow the first row of the range to update
* @param {number} lastRow LastRow the last row of the range to update
* @param {number} offscreenFirstRow
* @param {number} offscreenLastRow
* @param {Array<>} pendingBackDecorationChanges
* @param {Object} renderData
* @param {number} lineHeight This.minimap.getLineHeight() * devicePixelRatio
* @param {TextEditorElement} editorElement This.minimap.getTextEditorElement()
* @param {Decoration[]} decorations
* @access private
*/
function updateBackDecorationsLayer(
backLayer,
firstRow,
lastRow,
offscreenFirstRow,
offscreenLastRow,
pendingBackDecorationChanges,
renderData,
lineHeight,
editorElement,
decorations
) {
const intactRanges = computeIntactRanges(
firstRow,
lastRow,
pendingBackDecorationChanges,
offscreenFirstRow,
offscreenLastRow
)
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
// redrawRangesOnLayer
backLayer.clearCanvas()
if (intactRanges.length === 0) {
drawBackDecorationsForLines(firstRow, lastRow, 0, renderData, lineHeight, editorElement, decorations)
} else {
for (let j = 0, len = intactRanges.length; j < len; j++) {
const intact = intactRanges[j]
backLayer.copyPartFromOffscreen(
intact.offscreenRow * lineHeight,
(intact.start - firstRow) * lineHeight,
(intact.end - intact.start) * lineHeight
)
}
// drawLinesForRanges
let currentRow = firstRow
for (let i = 0, len = intactRanges.length; i < len; i++) {
const range = intactRanges[i]
drawBackDecorationsForLines(
currentRow,
range.start,
currentRow - firstRow,
renderData,
lineHeight,
editorElement,
decorations
)
currentRow = range.end
}
if (currentRow <= lastRow) {
drawBackDecorationsForLines(
currentRow,
lastRow,
currentRow - firstRow,
renderData,
lineHeight,
editorElement,
decorations
)
}
}
backLayer.resetOffscreenSize()
backLayer.copyToOffscreen()
}
/**
* Performs an update of the front decorations layer using the pending front decorations changes arrays.
*
* @param {CanvasLayer} frontLayer
* @param {number} firstRow FirstRow the first row of the range to update
* @param {number} lastRow LastRow the last row of the range to update
* @param {number} offscreenFirstRow
* @param {number} offscreenLastRow
* @param {Array<>} pendingFrontDecorationChanges
* @param {Object} renderData
* @param {number} lineHeight This.minimap.getLineHeight() * devicePixelRatio
* @param {TextEditorElement} editorElement This.minimap.getTextEditorElement()
* @param {Decoration[]} decorations
* @access private
*/
function updateFrontDecorationsLayer(
frontLayer,
firstRow,
lastRow,
offscreenFirstRow,
offscreenLastRow,
pendingFrontDecorationChanges,
renderData,
lineHeight,
editorElement,
decorations
) {
const intactRanges = computeIntactRanges(
firstRow,
lastRow,
pendingFrontDecorationChanges,
offscreenFirstRow,
offscreenLastRow
)
// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
// redrawRangesOnLayer
frontLayer.clearCanvas()
if (intactRanges.length === 0) {
drawFrontDecorationsForLines(firstRow, lastRow, 0, renderData, lineHeight, editorElement, decorations)
} else {
for (let j = 0, len = intactRanges.length; j < len; j++) {
const intact = intactRanges[j]
frontLayer.copyPartFromOffscreen(
intact.offscreenRow * lineHeight,
(intact.start - firstRow) * lineHeight,
(intact.end - intact.start) * lineHeight
)
}
// drawLinesForRanges
let currentRow = firstRow
for (let i = 0, len = intactRanges.length; i < len; i++) {
const range = intactRanges[i]
drawFrontDecorationsForLines(
currentRow,
range.start,
currentRow - firstRow,
renderData,
lineHeight,
editorElement,
decorations
)
currentRow = range.end
}
if (currentRow <= lastRow) {
drawFrontDecorationsForLines(
currentRow,
lastRow,
currentRow - firstRow,
renderData,
lineHeight,
editorElement,
decorations
)
}
}
frontLayer.resetOffscreenSize()
frontLayer.copyToOffscreen()
}
const whitespaceTokenRegex = /^\s+$/
const oneOrMoreWhiteSpaceRegexp = /\s+/
/**
* Draws a single token on the given context.
*
* @param {CanvasRenderingContext2D} context The target canvas context
* @param {string} text The token's text content
* @param {string} color The token's CSS color
* @param {number} x The x position of the token in the line
* @param {number} y The y position of the line in the minimap
* @param {number} charWidth The width of a character in the minimap
* @param {number} charHeight The height of a character in the minimap
* @returns {number} The x position at the end of the token
* @access private
*/
function drawToken(context, text, color, x, y, charWidth, charHeight, ignoreWhitespacesInTokens) {
context.fillStyle = color
if (ignoreWhitespacesInTokens) {
const length = text.length * charWidth
context.fillRect(x, y, length, charHeight)
return x + length
} else {
let chars = 0
for (let j = 0, len = text.length; j < len; j++) {
const char = text[j]
if (char === " ") {
if (chars > 0) {
context.fillRect(x - chars * charWidth, y, chars * charWidth, charHeight)
}
chars = 0
} else {
chars++
}
x += charWidth
}
if (chars > 0) {
context.fillRect(x - chars * charWidth, y, chars * charWidth, charHeight)
}
return x
}
}
/**
* 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 {number} editorScreenLineCount
* @param {RegExp} invisibleRegExp
* @param {(t: Token) => string} getTokenColorClosure
* @param {boolean} ignoreWhitespacesInTokens This.ignoreWhitespacesInTokens
* @param {number} maxTokensInOneLine This.maxTokensInOneLine
* @access private
*/
function drawLines(
firstRow,
lastRow,
offsetRow,
lineHeight,
charHeight,
charWidth,
canvasWidth,
context,
editor,
editorScreenLineCount,
invisibleRegExp,
getTokenColorClosure,
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
lastRow = Math.min(lastRow, editorScreenLineCount)
for (let line = firstRow; line < lastRow; line++) {
const editorTokensForScreenRow = editor.tokensForScreenRow(line)
const numToken = editorTokensForScreenRow.length
const numTokenToRender = Math.min(numToken, maxTokensInOneLine)
if (lastLine !== line) {
x = 0
y += lineHeight
lastLine = line
context.clearRect(x, y, canvasWidth, lineHeight)
}
for (let iToken = 0; iToken < numTokenToRender; iToken++) {
const token = editorTokensForScreenRow[iToken]
const tokenText = token.text.replace(invisibleRegExp, " ")
const tokenScopes = token.scopes
if (x > canvasWidth) {
continue
}
if (whitespaceTokenRegex.test(tokenText)) {
x += tokenText.length * charWidth
} else {
x = drawToken(
context,
tokenText,
getTokenColorClosure(tokenScopes),
x,
y,
charWidth,
charHeight,
ignoreWhitespacesInTokens
)
}
}
}
context.fill()
}
/**
* Returns the regexp to replace invisibles substitution characters in editor lines.
*
* @param {TextEditor} editor
* @returns {RegExp} The regular expression to match invisible characters
* @access private
*/
function getInvisibleRegExp(editor) {
const invisibles = editor.getInvisibles()
const regexp = []
if (invisibles.cr != null) {
regexp.push(invisibles.cr)
}
if (invisibles.eol != null) {
regexp.push(invisibles.eol)
}
if (invisibles.space != null) {
regexp.push(invisibles.space)
}
if (invisibles.tab != null) {
regexp.push(invisibles.tab)
}
if (regexp.length !== 0) {
return RegExp(
regexp
.filter((s) => {
return typeof s === "string"
})
.map(escapeRegExp)
.join("|"),
"g"
)
} else {
return null
}
}
/**
* Dispatchers for decoration drawing (custom decoration drawer added dynamically)
*
* @param {Object} backgroundDecorationDispatcher An object with the type to render as key and the render method as value
*/
const backgroundDecorationDispatcher = {
line: drawLineDecoration,
"highlight-under": drawHighlightDecoration,
}
/**
* Dispatchers for decoration drawing (custom decoration drawer added dynamically)
*
* @param {Object} frontDecorationDispatcher An object with the type to render as key and the render method as value
*/
const frontDecorationDispatcher = {
gutter: drawGutterDecoration,
"highlight-over": drawHighlightDecoration,
"highlight-outline": drawHighlightOutlineDecoration,
}
/**
* Draws a line decoration.
*
* @param {Decoration} decoration The decoration to render
* @param {Object} data The data need to perform the render
* @param {string} decorationColor Decoration color
* @access private
*/
function drawLineDecoration(decoration, data, decorationColor) {
const { context, lineHeight, canvasWidth, yRow } = data
context.fillStyle = decorationColor
context.fillRect(0, yRow, canvasWidth, lineHeight)
}
/**
* Draws a gutter decoration.
*
* @param {Decoration} decoration The decoration to render
* @param {Object} data The data need to perform the render
* @param {string} decorationColor Decoration color
* @access private
*/
function drawGutterDecoration(decoration, data, decorationColor) {
const { context, lineHeight, yRow } = data
context.fillStyle = decorationColor
context.fillRect(0, yRow, 1, lineHeight)
}
/**
* Draws a highlight decoration.
*
* It renders only the part of the highlight corresponding to the specified row.
*
* @param {Decoration} decoration The decoration to render
* @param {Object} data The data need to perform the render
* @param {string} decorationColor Decoration color
* @access private
*/
function drawHighlightDecoration(decoration, data, decorationColor) {
const { context, lineHeight, charWidth, canvasWidth, screenRow, yRow } = data
const range = decoration.getMarker().getScreenRange()
const rowSpan = range.end.row - range.start.row
context.fillStyle = decorationColor
if (rowSpan === 0) {
const colSpan = range.end.column - range.start.column
context.fillRect(range.start.column * charWidth, yRow, colSpan * charWidth, lineHeight)
} else if (screenRow === range.start.row) {
const x = range.start.column * charWidth
context.fillRect(x, yRow, canvasWidth - x, lineHeight)
} else if (screenRow === range.end.row) {
context.fillRect(0, yRow, range.end.column * charWidth, lineHeight)
} else {
context.fillRect(0, yRow, canvasWidth, lineHeight)
}
}
/**
* Draws a highlight outline decoration.
*
* It renders only the part of the highlight corresponding to the specified row.
*
* @param {Decoration} decoration The decoration to render
* @param {Object} data The data need to perform the render
* @param {string} decorationColor Decoration color
* @access private
*/
function drawHighlightOutlineDecoration(decoration, data, decorationColor) {
const { context, lineHeight, charWidth, canvasWidth, screenRow, yRow } = data
let bottomWidth, colSpan, width, xBottomStart, xEnd, xStart
const range = decoration.getMarker().getScreenRange()
const rowSpan = range.end.row - range.start.row
const yStart = yRow
const yEnd = yStart + lineHeight
context.fillStyle = decorationColor
if (rowSpan === 0) {
colSpan = range.end.column - range.start.column
width = colSpan * charWidth
xStart = range.start.column * charWidth
xEnd = xStart + width
context.fillRect(xStart, yStart, width, 1)
context.fillRect(xStart, yEnd - 1, width, 1)
context.fillRect(xStart, yStart, 1, lineHeight)
context.fillRect(xEnd, yStart, 1, lineHeight)
} else if (rowSpan === 1) {
xStart = range.start.column * charWidth
xEnd = range.end.column * charWidth
if (screenRow === range.start.row) {
width = canvasWidth - xStart
xBottomStart = Math.max(xStart, xEnd)
bottomWidth = canvasWidth - xBottomStart
context.fillRect(xStart, yStart, width, 1)
context.fillRect(xBottomStart, yEnd - 1, bottomWidth, 1)
context.fillRect(xStart, yStart, 1, lineHeight)
context.fillRect(canvasWidth - 1, yStart, 1, lineHeight)
} else {
width = canvasWidth - xStart
bottomWidth = canvasWidth - xEnd
context.fillRect(0, yStart, xStart, 1)
context.fillRect(0, yEnd - 1, xEnd, 1)
context.fillRect(0, yStart, 1, lineHeight)
context.fillRect(xEnd, yStart, 1, lineHeight)
}
} else {
xStart = range.start.column * charWidth
xEnd = range.end.column * charWidth
if (screenRow === range.start.row) {
width = canvasWidth - xStart
context.fillRect(xStart, yStart, width, 1)
context.fillRect(xStart, yStart, 1, lineHeight)
context.fillRect(canvasWidth - 1, yStart, 1, lineHeight)
} else if (screenRow === range.end.row) {
width = canvasWidth - xStart
context.fillRect(0, yEnd - 1, xEnd, 1)
context.fillRect(0, yStart, 1, lineHeight)
context.fillRect(xEnd, yStart, 1, lineHeight)
} else {
context.fillRect(0, yStart, 1, lineHeight)
context.fillRect(canvasWidth - 1, yStart, 1, lineHeight)
if (screenRow === range.start.row + 1) {
context.fillRect(0, yStart, xStart, 1)
}
if (screenRow === range.end.row - 1) {
context.fillRect(xEnd, yEnd - 1, canvasWidth - xEnd, 1)
}
}
}
}
/**
* Draws a custom decoration.
*
* It renders only the part of the highlight corresponding to the specified row.
*
* @param {Decoration} decoration The decoration to render
* @param {Object} data The data need to perform the render
* @param {string} decorationColor Decoration color
* @param {TextEditorElement} editorElement
* @access private
*/
function drawCustomDecoration(decoration, data, decorationColor, editorElement) {
const renderRoutine = decoration.getProperties().render
if (renderRoutine) {
data.color = decorationColor
renderRoutine(decoration, data, editorElement)
}
}
/**
* Draws the specified decorations for the current `screenRow`.
*
* The `decorations` object contains all the decorations grouped by type and then rows.
*
* @param {number} screenRow The screen row index for which render decorations
* @param {Object} decorations The object containing all the decorations
* @param {Object} renderData The object containing the render data
* @param {Object} types An object with the type to render as key and the render method as value
* @param {TextEditorElement} editorElement
* @access private
*/
function drawDecorations(screenRow, decorations, renderData, types, editorElement) {
let decorationsToRender = []
renderData.context.clearRect(0, renderData.yRow, renderData.canvasWidth, renderData.lineHeight)
for (const i in types) {
decorationsToRender = decorationsToRender.concat(decorations[i] != null ? decorations[i][screenRow] || [] : [])
}
decorationsToRender.sort(
(a, b) => (renderData.orders[a.properties.plugin] || 0) - (renderData.orders[b.properties.plugin] || 0)
)
if (decorationsToRender != null ? decorationsToRender.length : undefined) {
for (let i = 0, len = decorationsToRender.length; i < len; i++) {
const decoration = decorationsToRender[i]
const decorationDrawer = types[decoration.properties.type]
if (!SPEC_MODE) {
decorationDrawer(decoration, renderData, /* decorationColor */ getDecorationColor(decoration, editorElement))
} else {
// get the real function name from the mangeld Parcel names
const functionName = decorationDrawer.name.split("$").pop().replace("Lambda", "")
// call the spy:
thisSpec[functionName](
decoration,
renderData,
/* decorationColor */ getDecorationColor(decoration, editorElement)
)
}
}
}
}