forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-mate-language-mode-spec.js
1114 lines (947 loc) · 46.2 KB
/
text-mate-language-mode-spec.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
const NullGrammar = require('../src/null-grammar')
const TextMateLanguageMode = require('../src/text-mate-language-mode')
const TextBuffer = require('text-buffer')
const {Point, Range} = TextBuffer
const _ = require('underscore-plus')
const dedent = require('dedent')
const {it, fit, ffit, fffit, beforeEach, afterEach} = require('./async-spec-helpers')
describe('TextMateLanguageMode', () => {
let languageMode, buffer, config
beforeEach(async () => {
config = atom.config
config.set('core.useTreeSitterParsers', false)
// enable async tokenization
TextMateLanguageMode.prototype.chunkSize = 5
jasmine.unspy(TextMateLanguageMode.prototype, 'tokenizeInBackground')
await atom.packages.activatePackage('language-javascript')
})
afterEach(() => {
buffer && buffer.destroy()
languageMode && languageMode.destroy()
config.unset('core.useTreeSitterParsers')
})
describe('when the editor is constructed with the largeFileMode option set to true', () => {
it("loads the editor but doesn't tokenize", async () => {
const line = 'a b c d\n'
buffer = new TextBuffer(line.repeat(256 * 1024))
expect(buffer.getText().length).toBe(2 * 1024 * 1024)
languageMode = new TextMateLanguageMode({
buffer,
grammar: atom.grammars.grammarForScopeName('source.js'),
tabLength: 2
})
buffer.setLanguageMode(languageMode)
expect(languageMode.isRowCommented(0)).toBeFalsy()
// It treats the entire line as one big token
let iterator = languageMode.buildHighlightIterator()
iterator.seek({row: 0, column: 0})
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual({row: 0, column: 7})
buffer.insert([0, 0], 'hey"')
iterator = languageMode.buildHighlightIterator()
iterator.seek({row: 0, column: 0})
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual({row: 0, column: 11})
})
})
describe('tokenizing', () => {
describe('when the buffer is destroyed', () => {
beforeEach(() => {
buffer = atom.project.bufferForPathSync('sample.js')
languageMode = new TextMateLanguageMode({buffer, config, config, grammar: atom.grammars.grammarForScopeName('source.js')})
languageMode.startTokenizing()
})
it('stops tokenization', () => {
languageMode.destroy()
spyOn(languageMode, 'tokenizeNextChunk')
advanceClock()
expect(languageMode.tokenizeNextChunk).not.toHaveBeenCalled()
})
})
describe('when the buffer contains soft-tabs', () => {
beforeEach(() => {
buffer = atom.project.bufferForPathSync('sample.js')
languageMode = new TextMateLanguageMode({buffer, config, grammar: atom.grammars.grammarForScopeName('source.js')})
buffer.setLanguageMode(languageMode)
languageMode.startTokenizing()
})
afterEach(() => {
languageMode.destroy()
buffer.release()
})
describe('on construction', () =>
it('tokenizes lines chunk at a time in the background', () => {
const line0 = languageMode.tokenizedLines[0]
expect(line0).toBeUndefined()
const line11 = languageMode.tokenizedLines[11]
expect(line11).toBeUndefined()
// tokenize chunk 1
advanceClock()
expect(languageMode.tokenizedLines[0].ruleStack != null).toBeTruthy()
expect(languageMode.tokenizedLines[4].ruleStack != null).toBeTruthy()
expect(languageMode.tokenizedLines[5]).toBeUndefined()
// tokenize chunk 2
advanceClock()
expect(languageMode.tokenizedLines[5].ruleStack != null).toBeTruthy()
expect(languageMode.tokenizedLines[9].ruleStack != null).toBeTruthy()
expect(languageMode.tokenizedLines[10]).toBeUndefined()
// tokenize last chunk
advanceClock()
expect(languageMode.tokenizedLines[10].ruleStack != null).toBeTruthy()
expect(languageMode.tokenizedLines[12].ruleStack != null).toBeTruthy()
})
)
describe('when the buffer is partially tokenized', () => {
beforeEach(() => {
// tokenize chunk 1 only
advanceClock()
})
describe('when there is a buffer change inside the tokenized region', () => {
describe('when lines are added', () => {
it('pushes the invalid rows down', () => {
expect(languageMode.firstInvalidRow()).toBe(5)
buffer.insert([1, 0], '\n\n')
expect(languageMode.firstInvalidRow()).toBe(7)
})
})
describe('when lines are removed', () => {
it('pulls the invalid rows up', () => {
expect(languageMode.firstInvalidRow()).toBe(5)
buffer.delete([[1, 0], [3, 0]])
expect(languageMode.firstInvalidRow()).toBe(2)
})
})
describe('when the change invalidates all the lines before the current invalid region', () => {
it('retokenizes the invalidated lines and continues into the valid region', () => {
expect(languageMode.firstInvalidRow()).toBe(5)
buffer.insert([2, 0], '/*')
expect(languageMode.firstInvalidRow()).toBe(3)
advanceClock()
expect(languageMode.firstInvalidRow()).toBe(8)
})
})
})
describe('when there is a buffer change surrounding an invalid row', () => {
it('pushes the invalid row to the end of the change', () => {
buffer.setTextInRange([[4, 0], [6, 0]], '\n\n\n')
expect(languageMode.firstInvalidRow()).toBe(8)
})
})
describe('when there is a buffer change inside an invalid region', () => {
it('does not attempt to tokenize the lines in the change, and preserves the existing invalid row', () => {
expect(languageMode.firstInvalidRow()).toBe(5)
buffer.setTextInRange([[6, 0], [7, 0]], '\n\n\n')
expect(languageMode.tokenizedLines[6]).toBeUndefined()
expect(languageMode.tokenizedLines[7]).toBeUndefined()
expect(languageMode.firstInvalidRow()).toBe(5)
})
})
})
describe('when the buffer is fully tokenized', () => {
beforeEach(() => fullyTokenize(languageMode))
describe('when there is a buffer change that is smaller than the chunk size', () => {
describe('when lines are updated, but none are added or removed', () => {
it('updates tokens to reflect the change', () => {
buffer.setTextInRange([[0, 0], [2, 0]], 'foo()\n7\n')
expect(languageMode.tokenizedLines[0].tokens[1]).toEqual({value: '(', scopes: ['source.js', 'meta.function-call.js', 'meta.arguments.js', 'punctuation.definition.arguments.begin.bracket.round.js']})
expect(languageMode.tokenizedLines[1].tokens[0]).toEqual({value: '7', scopes: ['source.js', 'constant.numeric.decimal.js']})
// line 2 is unchanged
expect(languageMode.tokenizedLines[2].tokens[1]).toEqual({value: 'if', scopes: ['source.js', 'keyword.control.js']})
})
describe('when the change invalidates the tokenization of subsequent lines', () => {
it('schedules the invalidated lines to be tokenized in the background', () => {
buffer.insert([5, 30], '/* */')
buffer.insert([2, 0], '/*')
expect(languageMode.tokenizedLines[3].tokens[0].scopes).toEqual(['source.js'])
advanceClock()
expect(languageMode.tokenizedLines[3].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
expect(languageMode.tokenizedLines[4].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
expect(languageMode.tokenizedLines[5].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
})
})
it('resumes highlighting with the state of the previous line', () => {
buffer.insert([0, 0], '/*')
buffer.insert([5, 0], '*/')
buffer.insert([1, 0], 'var ')
expect(languageMode.tokenizedLines[1].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
})
})
describe('when lines are both updated and removed', () => {
it('updates tokens to reflect the change', () => {
buffer.setTextInRange([[1, 0], [3, 0]], 'foo()')
// previous line 0 remains
expect(languageMode.tokenizedLines[0].tokens[0]).toEqual({value: 'var', scopes: ['source.js', 'storage.type.var.js']})
// previous line 3 should be combined with input to form line 1
expect(languageMode.tokenizedLines[1].tokens[0]).toEqual({value: 'foo', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']})
expect(languageMode.tokenizedLines[1].tokens[6]).toEqual({value: '=', scopes: ['source.js', 'keyword.operator.assignment.js']})
// lines below deleted regions should be shifted upward
expect(languageMode.tokenizedLines[2].tokens[1]).toEqual({value: 'while', scopes: ['source.js', 'keyword.control.js']})
expect(languageMode.tokenizedLines[3].tokens[1]).toEqual({value: '=', scopes: ['source.js', 'keyword.operator.assignment.js']})
expect(languageMode.tokenizedLines[4].tokens[1]).toEqual({value: '<', scopes: ['source.js', 'keyword.operator.comparison.js']})
})
})
describe('when the change invalidates the tokenization of subsequent lines', () => {
it('schedules the invalidated lines to be tokenized in the background', () => {
buffer.insert([5, 30], '/* */')
buffer.setTextInRange([[2, 0], [3, 0]], '/*')
expect(languageMode.tokenizedLines[2].tokens[0].scopes).toEqual(['source.js', 'comment.block.js', 'punctuation.definition.comment.begin.js'])
expect(languageMode.tokenizedLines[3].tokens[0].scopes).toEqual(['source.js'])
advanceClock()
expect(languageMode.tokenizedLines[3].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
expect(languageMode.tokenizedLines[4].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
})
})
describe('when lines are both updated and inserted', () => {
it('updates tokens to reflect the change', () => {
buffer.setTextInRange([[1, 0], [2, 0]], 'foo()\nbar()\nbaz()\nquux()')
// previous line 0 remains
expect(languageMode.tokenizedLines[0].tokens[0]).toEqual({ value: 'var', scopes: ['source.js', 'storage.type.var.js']})
// 3 new lines inserted
expect(languageMode.tokenizedLines[1].tokens[0]).toEqual({value: 'foo', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']})
expect(languageMode.tokenizedLines[2].tokens[0]).toEqual({value: 'bar', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']})
expect(languageMode.tokenizedLines[3].tokens[0]).toEqual({value: 'baz', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']})
// previous line 2 is joined with quux() on line 4
expect(languageMode.tokenizedLines[4].tokens[0]).toEqual({value: 'quux', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']})
expect(languageMode.tokenizedLines[4].tokens[4]).toEqual({value: 'if', scopes: ['source.js', 'keyword.control.js']})
// previous line 3 is pushed down to become line 5
expect(languageMode.tokenizedLines[5].tokens[3]).toEqual({value: '=', scopes: ['source.js', 'keyword.operator.assignment.js']})
})
})
describe('when the change invalidates the tokenization of subsequent lines', () => {
it('schedules the invalidated lines to be tokenized in the background', () => {
buffer.insert([5, 30], '/* */')
buffer.insert([2, 0], '/*\nabcde\nabcder')
expect(languageMode.tokenizedLines[2].tokens[0].scopes).toEqual(['source.js', 'comment.block.js', 'punctuation.definition.comment.begin.js'])
expect(languageMode.tokenizedLines[3].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
expect(languageMode.tokenizedLines[4].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
expect(languageMode.tokenizedLines[5].tokens[0].scopes).toEqual(['source.js'])
advanceClock() // tokenize invalidated lines in background
expect(languageMode.tokenizedLines[5].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
expect(languageMode.tokenizedLines[6].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
expect(languageMode.tokenizedLines[7].tokens[0].scopes).toEqual(['source.js', 'comment.block.js'])
expect(languageMode.tokenizedLines[8].tokens[0].scopes).not.toBe(['source.js', 'comment.block.js'])
})
})
})
describe('when there is an insertion that is larger than the chunk size', () => {
it('tokenizes the initial chunk synchronously, then tokenizes the remaining lines in the background', () => {
const commentBlock = _.multiplyString('// a comment\n', languageMode.chunkSize + 2)
buffer.insert([0, 0], commentBlock)
expect(languageMode.tokenizedLines[0].ruleStack != null).toBeTruthy()
expect(languageMode.tokenizedLines[4].ruleStack != null).toBeTruthy()
expect(languageMode.tokenizedLines[5]).toBeUndefined()
advanceClock()
expect(languageMode.tokenizedLines[5].ruleStack != null).toBeTruthy()
expect(languageMode.tokenizedLines[6].ruleStack != null).toBeTruthy()
})
})
})
})
describe('when the buffer contains hard-tabs', () => {
beforeEach(async () => {
atom.packages.activatePackage('language-coffee-script')
buffer = atom.project.bufferForPathSync('sample-with-tabs.coffee')
languageMode = new TextMateLanguageMode({buffer, config, grammar: atom.grammars.grammarForScopeName('source.coffee')})
languageMode.startTokenizing()
})
afterEach(() => {
languageMode.destroy()
buffer.release()
})
describe('when the buffer is fully tokenized', () => {
beforeEach(() => fullyTokenize(languageMode))
})
})
describe('when tokenization completes', () => {
it('emits the `tokenized` event', async () => {
const editor = await atom.workspace.open('sample.js')
const tokenizedHandler = jasmine.createSpy('tokenized handler')
editor.languageMode.onDidTokenize(tokenizedHandler)
fullyTokenize(editor.getBuffer().getLanguageMode())
expect(tokenizedHandler.callCount).toBe(1)
})
it("doesn't re-emit the `tokenized` event when it is re-tokenized", async () => {
const editor = await atom.workspace.open('sample.js')
fullyTokenize(editor.languageMode)
const tokenizedHandler = jasmine.createSpy('tokenized handler')
editor.languageMode.onDidTokenize(tokenizedHandler)
editor.getBuffer().insert([0, 0], "'")
fullyTokenize(editor.languageMode)
expect(tokenizedHandler).not.toHaveBeenCalled()
})
})
describe('when the grammar is updated because a grammar it includes is activated', async () => {
it('re-emits the `tokenized` event', async () => {
let tokenizationCount = 0
const editor = await atom.workspace.open('coffee.coffee')
editor.onDidTokenize(() => { tokenizationCount++ })
fullyTokenize(editor.getBuffer().getLanguageMode())
tokenizationCount = 0
await atom.packages.activatePackage('language-coffee-script')
fullyTokenize(editor.getBuffer().getLanguageMode())
expect(tokenizationCount).toBe(1)
})
it('retokenizes the buffer', async () => {
await atom.packages.activatePackage('language-ruby-on-rails')
await atom.packages.activatePackage('language-ruby')
buffer = atom.project.bufferForPathSync()
buffer.setText("<div class='name'><%= User.find(2).full_name %></div>")
languageMode = new TextMateLanguageMode({buffer, config, grammar: atom.grammars.selectGrammar('test.erb')})
fullyTokenize(languageMode)
expect(languageMode.tokenizedLines[0].tokens[0]).toEqual({
value: "<div class='name'>",
scopes: ['text.html.ruby']
})
await atom.packages.activatePackage('language-html')
fullyTokenize(languageMode)
expect(languageMode.tokenizedLines[0].tokens[0]).toEqual({
value: '<',
scopes: ['text.html.ruby', 'meta.tag.block.div.html', 'punctuation.definition.tag.begin.html']
})
})
})
describe('when the buffer is configured with the null grammar', () => {
it('does not actually tokenize using the grammar', () => {
spyOn(NullGrammar, 'tokenizeLine').andCallThrough()
buffer = atom.project.bufferForPathSync('sample.will-use-the-null-grammar')
buffer.setText('a\nb\nc')
languageMode = new TextMateLanguageMode({buffer, config})
const tokenizeCallback = jasmine.createSpy('onDidTokenize')
languageMode.onDidTokenize(tokenizeCallback)
expect(languageMode.tokenizedLines[0]).toBeUndefined()
expect(languageMode.tokenizedLines[1]).toBeUndefined()
expect(languageMode.tokenizedLines[2]).toBeUndefined()
expect(tokenizeCallback.callCount).toBe(0)
expect(NullGrammar.tokenizeLine).not.toHaveBeenCalled()
fullyTokenize(languageMode)
expect(languageMode.tokenizedLines[0]).toBeUndefined()
expect(languageMode.tokenizedLines[1]).toBeUndefined()
expect(languageMode.tokenizedLines[2]).toBeUndefined()
expect(tokenizeCallback.callCount).toBe(0)
expect(NullGrammar.tokenizeLine).not.toHaveBeenCalled()
})
})
})
describe('.tokenForPosition(position)', () => {
afterEach(() => {
languageMode.destroy()
buffer.release()
})
it('returns the correct token (regression)', () => {
buffer = atom.project.bufferForPathSync('sample.js')
languageMode = new TextMateLanguageMode({buffer, config, grammar: atom.grammars.grammarForScopeName('source.js')})
fullyTokenize(languageMode)
expect(languageMode.tokenForPosition([1, 0]).scopes).toEqual(['source.js'])
expect(languageMode.tokenForPosition([1, 1]).scopes).toEqual(['source.js'])
expect(languageMode.tokenForPosition([1, 2]).scopes).toEqual(['source.js', 'storage.type.var.js'])
})
})
describe('.bufferRangeForScopeAtPosition(selector, position)', () => {
beforeEach(() => {
buffer = atom.project.bufferForPathSync('sample.js')
languageMode = new TextMateLanguageMode({buffer, config, grammar: atom.grammars.grammarForScopeName('source.js')})
fullyTokenize(languageMode)
})
describe('when the selector does not match the token at the position', () =>
it('returns a falsy value', () => expect(languageMode.bufferRangeForScopeAtPosition('.bogus', [0, 1])).toBeUndefined())
)
describe('when the selector matches a single token at the position', () => {
it('returns the range covered by the token', () => {
expect(languageMode.bufferRangeForScopeAtPosition('.storage.type.var.js', [0, 1])).toEqual([[0, 0], [0, 3]])
expect(languageMode.bufferRangeForScopeAtPosition('.storage.type.var.js', [0, 3])).toEqual([[0, 0], [0, 3]])
})
})
describe('when the selector matches a run of multiple tokens at the position', () => {
it('returns the range covered by all contiguous tokens (within a single line)', () => {
expect(languageMode.bufferRangeForScopeAtPosition('.function', [1, 18])).toEqual([[1, 6], [1, 28]])
})
})
})
describe('.tokenizedLineForRow(row)', () => {
it("returns the tokenized line for a row, or a placeholder line if it hasn't been tokenized yet", () => {
buffer = atom.project.bufferForPathSync('sample.js')
const grammar = atom.grammars.grammarForScopeName('source.js')
languageMode = new TextMateLanguageMode({buffer, config, grammar})
const line0 = buffer.lineForRow(0)
const jsScopeStartId = grammar.startIdForScope(grammar.scopeName)
const jsScopeEndId = grammar.endIdForScope(grammar.scopeName)
languageMode.startTokenizing()
expect(languageMode.tokenizedLines[0]).toBeUndefined()
expect(languageMode.tokenizedLineForRow(0).text).toBe(line0)
expect(languageMode.tokenizedLineForRow(0).tags).toEqual([jsScopeStartId, line0.length, jsScopeEndId])
advanceClock(1)
expect(languageMode.tokenizedLines[0]).not.toBeUndefined()
expect(languageMode.tokenizedLineForRow(0).text).toBe(line0)
expect(languageMode.tokenizedLineForRow(0).tags).not.toEqual([jsScopeStartId, line0.length, jsScopeEndId])
})
it('returns undefined if the requested row is outside the buffer range', () => {
buffer = atom.project.bufferForPathSync('sample.js')
const grammar = atom.grammars.grammarForScopeName('source.js')
languageMode = new TextMateLanguageMode({buffer, config, grammar})
fullyTokenize(languageMode)
expect(languageMode.tokenizedLineForRow(999)).toBeUndefined()
})
})
describe('.buildHighlightIterator', () => {
const {TextMateHighlightIterator} = TextMateLanguageMode
it('iterates over the syntactic scope boundaries', () => {
buffer = new TextBuffer({text: 'var foo = 1 /*\nhello*/var bar = 2\n'})
languageMode = new TextMateLanguageMode({buffer, config, grammar: atom.grammars.grammarForScopeName('source.js')})
fullyTokenize(languageMode)
const iterator = languageMode.buildHighlightIterator()
iterator.seek(Point(0, 0))
const expectedBoundaries = [
{position: Point(0, 0), closeTags: [], openTags: ['syntax--source syntax--js', 'syntax--storage syntax--type syntax--var syntax--js']},
{position: Point(0, 3), closeTags: ['syntax--storage syntax--type syntax--var syntax--js'], openTags: []},
{position: Point(0, 8), closeTags: [], openTags: ['syntax--keyword syntax--operator syntax--assignment syntax--js']},
{position: Point(0, 9), closeTags: ['syntax--keyword syntax--operator syntax--assignment syntax--js'], openTags: []},
{position: Point(0, 10), closeTags: [], openTags: ['syntax--constant syntax--numeric syntax--decimal syntax--js']},
{position: Point(0, 11), closeTags: ['syntax--constant syntax--numeric syntax--decimal syntax--js'], openTags: []},
{position: Point(0, 12), closeTags: [], openTags: ['syntax--comment syntax--block syntax--js', 'syntax--punctuation syntax--definition syntax--comment syntax--begin syntax--js']},
{position: Point(0, 14), closeTags: ['syntax--punctuation syntax--definition syntax--comment syntax--begin syntax--js'], openTags: []},
{position: Point(1, 5), closeTags: [], openTags: ['syntax--punctuation syntax--definition syntax--comment syntax--end syntax--js']},
{position: Point(1, 7), closeTags: ['syntax--punctuation syntax--definition syntax--comment syntax--end syntax--js', 'syntax--comment syntax--block syntax--js'], openTags: ['syntax--storage syntax--type syntax--var syntax--js']},
{position: Point(1, 10), closeTags: ['syntax--storage syntax--type syntax--var syntax--js'], openTags: []},
{position: Point(1, 15), closeTags: [], openTags: ['syntax--keyword syntax--operator syntax--assignment syntax--js']},
{position: Point(1, 16), closeTags: ['syntax--keyword syntax--operator syntax--assignment syntax--js'], openTags: []},
{position: Point(1, 17), closeTags: [], openTags: ['syntax--constant syntax--numeric syntax--decimal syntax--js']},
{position: Point(1, 18), closeTags: ['syntax--constant syntax--numeric syntax--decimal syntax--js'], openTags: []}
]
while (true) {
const boundary = {
position: iterator.getPosition(),
closeTags: iterator.getCloseScopeIds().map(scopeId => languageMode.classNameForScopeId(scopeId)),
openTags: iterator.getOpenScopeIds().map(scopeId => languageMode.classNameForScopeId(scopeId))
}
expect(boundary).toEqual(expectedBoundaries.shift())
if (!iterator.moveToSuccessor()) { break }
}
expect(iterator.seek(Point(0, 1)).map(scopeId => languageMode.classNameForScopeId(scopeId))).toEqual([
'syntax--source syntax--js',
'syntax--storage syntax--type syntax--var syntax--js'
])
expect(iterator.getPosition()).toEqual(Point(0, 3))
expect(iterator.seek(Point(0, 8)).map(scopeId => languageMode.classNameForScopeId(scopeId))).toEqual([
'syntax--source syntax--js'
])
expect(iterator.getPosition()).toEqual(Point(0, 8))
expect(iterator.seek(Point(1, 0)).map(scopeId => languageMode.classNameForScopeId(scopeId))).toEqual([
'syntax--source syntax--js',
'syntax--comment syntax--block syntax--js'
])
expect(iterator.getPosition()).toEqual(Point(1, 0))
expect(iterator.seek(Point(1, 18)).map(scopeId => languageMode.classNameForScopeId(scopeId))).toEqual([
'syntax--source syntax--js',
'syntax--constant syntax--numeric syntax--decimal syntax--js'
])
expect(iterator.getPosition()).toEqual(Point(1, 18))
expect(iterator.seek(Point(2, 0)).map(scopeId => languageMode.classNameForScopeId(scopeId))).toEqual([
'syntax--source syntax--js'
])
iterator.moveToSuccessor()
}) // ensure we don't infinitely loop (regression test)
it('does not report columns beyond the length of the line', async () => {
await atom.packages.activatePackage('language-coffee-script')
buffer = new TextBuffer({text: '# hello\n# world'})
languageMode = new TextMateLanguageMode({buffer, config, grammar: atom.grammars.grammarForScopeName('source.coffee')})
fullyTokenize(languageMode)
const iterator = languageMode.buildHighlightIterator()
iterator.seek(Point(0, 0))
iterator.moveToSuccessor()
iterator.moveToSuccessor()
expect(iterator.getPosition().column).toBe(7)
iterator.moveToSuccessor()
expect(iterator.getPosition().column).toBe(0)
iterator.seek(Point(0, 7))
expect(iterator.getPosition().column).toBe(7)
iterator.seek(Point(0, 8))
expect(iterator.getPosition().column).toBe(7)
})
it('correctly terminates scopes at the beginning of the line (regression)', () => {
const grammar = atom.grammars.createGrammar('test', {
'scopeName': 'text.broken',
'name': 'Broken grammar',
'patterns': [
{'begin': 'start', 'end': '(?=end)', 'name': 'blue.broken'},
{'match': '.', 'name': 'yellow.broken'}
]
})
buffer = new TextBuffer({text: 'start x\nend x\nx'})
languageMode = new TextMateLanguageMode({buffer, config, grammar})
fullyTokenize(languageMode)
const iterator = languageMode.buildHighlightIterator()
iterator.seek(Point(1, 0))
expect(iterator.getPosition()).toEqual([1, 0])
expect(iterator.getCloseScopeIds().map(scopeId => languageMode.classNameForScopeId(scopeId))).toEqual(['syntax--blue syntax--broken'])
expect(iterator.getOpenScopeIds().map(scopeId => languageMode.classNameForScopeId(scopeId))).toEqual(['syntax--yellow syntax--broken'])
})
describe('TextMateHighlightIterator.seek(position)', function () {
it('seeks to the leftmost tag boundary greater than or equal to the given position and returns the containing tags', function () {
const languageMode = {
tokenizedLineForRow (row) {
if (row === 0) {
return {
tags: [-1, -2, -3, -4, -5, 3, -3, -4, -6, -5, 4, -6, -3, -4],
text: 'foo bar',
openScopes: []
}
} else {
return null
}
}
}
const iterator = new TextMateHighlightIterator(languageMode)
expect(iterator.seek(Point(0, 0))).toEqual([])
expect(iterator.getPosition()).toEqual(Point(0, 0))
expect(iterator.getCloseScopeIds()).toEqual([])
expect(iterator.getOpenScopeIds()).toEqual([257])
iterator.moveToSuccessor()
expect(iterator.getCloseScopeIds()).toEqual([257])
expect(iterator.getOpenScopeIds()).toEqual([259])
expect(iterator.seek(Point(0, 1))).toEqual([261])
expect(iterator.getPosition()).toEqual(Point(0, 3))
expect(iterator.getCloseScopeIds()).toEqual([])
expect(iterator.getOpenScopeIds()).toEqual([259])
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual(Point(0, 3))
expect(iterator.getCloseScopeIds()).toEqual([259, 261])
expect(iterator.getOpenScopeIds()).toEqual([261])
expect(iterator.seek(Point(0, 3))).toEqual([261])
expect(iterator.getPosition()).toEqual(Point(0, 3))
expect(iterator.getCloseScopeIds()).toEqual([])
expect(iterator.getOpenScopeIds()).toEqual([259])
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual(Point(0, 3))
expect(iterator.getCloseScopeIds()).toEqual([259, 261])
expect(iterator.getOpenScopeIds()).toEqual([261])
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual(Point(0, 7))
expect(iterator.getCloseScopeIds()).toEqual([261])
expect(iterator.getOpenScopeIds()).toEqual([259])
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual(Point(0, 7))
expect(iterator.getCloseScopeIds()).toEqual([259])
expect(iterator.getOpenScopeIds()).toEqual([])
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual(Point(1, 0))
expect(iterator.getCloseScopeIds()).toEqual([])
expect(iterator.getOpenScopeIds()).toEqual([])
expect(iterator.seek(Point(0, 5))).toEqual([261])
expect(iterator.getPosition()).toEqual(Point(0, 7))
expect(iterator.getCloseScopeIds()).toEqual([261])
expect(iterator.getOpenScopeIds()).toEqual([259])
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual(Point(0, 7))
expect(iterator.getCloseScopeIds()).toEqual([259])
expect(iterator.getOpenScopeIds()).toEqual([])
})
})
describe('TextMateHighlightIterator.moveToSuccessor()', function () {
it('reports two boundaries at the same position when tags close, open, then close again without a non-negative integer separating them (regression)', () => {
const languageMode = {
tokenizedLineForRow () {
return {
tags: [-1, -2, -1, -2],
text: '',
openScopes: []
}
}
}
const iterator = new TextMateHighlightIterator(languageMode)
iterator.seek(Point(0, 0))
expect(iterator.getPosition()).toEqual(Point(0, 0))
expect(iterator.getCloseScopeIds()).toEqual([])
expect(iterator.getOpenScopeIds()).toEqual([257])
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual(Point(0, 0))
expect(iterator.getCloseScopeIds()).toEqual([257])
expect(iterator.getOpenScopeIds()).toEqual([257])
iterator.moveToSuccessor()
expect(iterator.getCloseScopeIds()).toEqual([257])
expect(iterator.getOpenScopeIds()).toEqual([])
})
})
})
describe('.suggestedIndentForBufferRow', () => {
let editor
describe('javascript', () => {
beforeEach(async () => {
editor = await atom.workspace.open('sample.js', {autoIndent: false})
await atom.packages.activatePackage('language-javascript')
})
it('bases indentation off of the previous non-blank line', () => {
expect(editor.suggestedIndentForBufferRow(0)).toBe(0)
expect(editor.suggestedIndentForBufferRow(1)).toBe(1)
expect(editor.suggestedIndentForBufferRow(2)).toBe(2)
expect(editor.suggestedIndentForBufferRow(5)).toBe(3)
expect(editor.suggestedIndentForBufferRow(7)).toBe(2)
expect(editor.suggestedIndentForBufferRow(9)).toBe(1)
expect(editor.suggestedIndentForBufferRow(11)).toBe(1)
})
it('does not take invisibles into account', () => {
editor.update({showInvisibles: true})
expect(editor.suggestedIndentForBufferRow(0)).toBe(0)
expect(editor.suggestedIndentForBufferRow(1)).toBe(1)
expect(editor.suggestedIndentForBufferRow(2)).toBe(2)
expect(editor.suggestedIndentForBufferRow(5)).toBe(3)
expect(editor.suggestedIndentForBufferRow(7)).toBe(2)
expect(editor.suggestedIndentForBufferRow(9)).toBe(1)
expect(editor.suggestedIndentForBufferRow(11)).toBe(1)
})
})
describe('css', () => {
beforeEach(async () => {
editor = await atom.workspace.open('css.css', {autoIndent: true})
await atom.packages.activatePackage('language-source')
await atom.packages.activatePackage('language-css')
})
it('does not return negative values (regression)', () => {
editor.setText('.test {\npadding: 0;\n}')
expect(editor.suggestedIndentForBufferRow(2)).toBe(0)
})
})
})
describe('.isFoldableAtRow(row)', () => {
beforeEach(() => {
buffer = atom.project.bufferForPathSync('sample.js')
buffer.insert([10, 0], ' // multi-line\n // comment\n // block\n')
buffer.insert([0, 0], '// multi-line\n// comment\n// block\n')
languageMode = new TextMateLanguageMode({buffer, config, grammar: atom.grammars.grammarForScopeName('source.js')})
buffer.setLanguageMode(languageMode)
fullyTokenize(languageMode)
})
it('includes the first line of multi-line comments', () => {
expect(languageMode.isFoldableAtRow(0)).toBe(true)
expect(languageMode.isFoldableAtRow(1)).toBe(false)
expect(languageMode.isFoldableAtRow(2)).toBe(false)
expect(languageMode.isFoldableAtRow(3)).toBe(true) // because of indent
expect(languageMode.isFoldableAtRow(13)).toBe(true)
expect(languageMode.isFoldableAtRow(14)).toBe(false)
expect(languageMode.isFoldableAtRow(15)).toBe(false)
expect(languageMode.isFoldableAtRow(16)).toBe(false)
buffer.insert([0, Infinity], '\n')
expect(languageMode.isFoldableAtRow(0)).toBe(false)
expect(languageMode.isFoldableAtRow(1)).toBe(false)
expect(languageMode.isFoldableAtRow(2)).toBe(true)
expect(languageMode.isFoldableAtRow(3)).toBe(false)
buffer.undo()
expect(languageMode.isFoldableAtRow(0)).toBe(true)
expect(languageMode.isFoldableAtRow(1)).toBe(false)
expect(languageMode.isFoldableAtRow(2)).toBe(false)
expect(languageMode.isFoldableAtRow(3)).toBe(true)
}) // because of indent
it('includes non-comment lines that precede an increase in indentation', () => {
buffer.insert([2, 0], ' ') // commented lines preceding an indent aren't foldable
expect(languageMode.isFoldableAtRow(1)).toBe(false)
expect(languageMode.isFoldableAtRow(2)).toBe(false)
expect(languageMode.isFoldableAtRow(3)).toBe(true)
expect(languageMode.isFoldableAtRow(4)).toBe(true)
expect(languageMode.isFoldableAtRow(5)).toBe(false)
expect(languageMode.isFoldableAtRow(6)).toBe(false)
expect(languageMode.isFoldableAtRow(7)).toBe(true)
expect(languageMode.isFoldableAtRow(8)).toBe(false)
buffer.insert([7, 0], ' ')
expect(languageMode.isFoldableAtRow(6)).toBe(true)
expect(languageMode.isFoldableAtRow(7)).toBe(false)
expect(languageMode.isFoldableAtRow(8)).toBe(false)
buffer.undo()
expect(languageMode.isFoldableAtRow(6)).toBe(false)
expect(languageMode.isFoldableAtRow(7)).toBe(true)
expect(languageMode.isFoldableAtRow(8)).toBe(false)
buffer.insert([7, 0], ' \n x\n')
expect(languageMode.isFoldableAtRow(6)).toBe(true)
expect(languageMode.isFoldableAtRow(7)).toBe(false)
expect(languageMode.isFoldableAtRow(8)).toBe(false)
buffer.insert([9, 0], ' ')
expect(languageMode.isFoldableAtRow(6)).toBe(true)
expect(languageMode.isFoldableAtRow(7)).toBe(false)
expect(languageMode.isFoldableAtRow(8)).toBe(false)
})
it('returns true if the line starts a multi-line comment', async () => {
editor = await atom.workspace.open('sample-with-comments.js')
fullyTokenize(editor.getBuffer().getLanguageMode())
expect(editor.isFoldableAtBufferRow(1)).toBe(true)
expect(editor.isFoldableAtBufferRow(6)).toBe(true)
expect(editor.isFoldableAtBufferRow(8)).toBe(false)
expect(editor.isFoldableAtBufferRow(11)).toBe(true)
expect(editor.isFoldableAtBufferRow(15)).toBe(false)
expect(editor.isFoldableAtBufferRow(17)).toBe(true)
expect(editor.isFoldableAtBufferRow(21)).toBe(true)
expect(editor.isFoldableAtBufferRow(24)).toBe(true)
expect(editor.isFoldableAtBufferRow(28)).toBe(false)
})
it('returns true for lines that end with a comment and are followed by an indented line', async () => {
editor = await atom.workspace.open('sample-with-comments.js')
expect(editor.isFoldableAtBufferRow(5)).toBe(true)
})
it("does not return true for a line in the middle of a comment that's followed by an indented line", async () => {
editor = await atom.workspace.open('sample-with-comments.js')
fullyTokenize(editor.getBuffer().getLanguageMode())
expect(editor.isFoldableAtBufferRow(7)).toBe(false)
editor.buffer.insert([8, 0], ' ')
expect(editor.isFoldableAtBufferRow(7)).toBe(false)
})
})
describe('.getFoldableRangesAtIndentLevel', () => {
it('returns the ranges that can be folded at the given indent level', () => {
buffer = new TextBuffer(dedent `
if (a) {
b();
if (c) {
d()
if (e) {
f()
}
g()
}
h()
}
i()
if (j) {
k()
}
`)
languageMode = new TextMateLanguageMode({buffer, config})
expect(simulateFold(languageMode.getFoldableRangesAtIndentLevel(0, 2))).toBe(dedent `
if (a) {⋯
}
i()
if (j) {⋯
}
`)
expect(simulateFold(languageMode.getFoldableRangesAtIndentLevel(1, 2))).toBe(dedent `
if (a) {
b();
if (c) {⋯
}
h()
}
i()
if (j) {
k()
}
`)
expect(simulateFold(languageMode.getFoldableRangesAtIndentLevel(2, 2))).toBe(dedent `
if (a) {
b();
if (c) {
d()
if (e) {⋯
}
g()
}
h()
}
i()
if (j) {
k()
}
`)
})
it('folds every foldable range at a given indentLevel', async () => {
editor = await atom.workspace.open('sample-with-comments.js')
fullyTokenize(editor.getBuffer().getLanguageMode())
editor.foldAllAtIndentLevel(2)
const folds = editor.unfoldAll()
expect(folds.length).toBe(5)
expect([folds[0].start.row, folds[0].end.row]).toEqual([6, 8])
expect([folds[1].start.row, folds[1].end.row]).toEqual([11, 16])
expect([folds[2].start.row, folds[2].end.row]).toEqual([17, 20])
expect([folds[3].start.row, folds[3].end.row]).toEqual([21, 22])
expect([folds[4].start.row, folds[4].end.row]).toEqual([24, 25])
})
})
describe('.getFoldableRanges', () => {
it('returns the ranges that can be folded', () => {
buffer = new TextBuffer(dedent `
if (a) {
b();
if (c) {
d()
if (e) {
f()
}
g()
}
h()
}
i()
if (j) {
k()
}
`)
languageMode = new TextMateLanguageMode({buffer, config})
expect(languageMode.getFoldableRanges(2).map(r => r.toString())).toEqual([
...languageMode.getFoldableRangesAtIndentLevel(0, 2),
...languageMode.getFoldableRangesAtIndentLevel(1, 2),
...languageMode.getFoldableRangesAtIndentLevel(2, 2),
].sort((a, b) => (a.start.row - b.start.row) || (a.end.row - b.end.row)).map(r => r.toString()))
})
it('works with multi-line comments', async () => {
await atom.packages.activatePackage('language-javascript')
editor = await atom.workspace.open('sample-with-comments.js', {autoIndent: false})
fullyTokenize(editor.getBuffer().getLanguageMode())
editor.foldAll()
const folds = editor.unfoldAll()
expect(folds.length).toBe(8)
expect([folds[0].start.row, folds[0].end.row]).toEqual([0, 30])
expect([folds[1].start.row, folds[1].end.row]).toEqual([1, 4])
expect([folds[2].start.row, folds[2].end.row]).toEqual([5, 27])
expect([folds[3].start.row, folds[3].end.row]).toEqual([6, 8])
expect([folds[4].start.row, folds[4].end.row]).toEqual([11, 16])
expect([folds[5].start.row, folds[5].end.row]).toEqual([17, 20])
expect([folds[6].start.row, folds[6].end.row]).toEqual([21, 22])
expect([folds[7].start.row, folds[7].end.row]).toEqual([24, 25])
})
})
describe('.getFoldableRangeContainingPoint', () => {
it('returns the range for the smallest fold that contains the given range', () => {
buffer = new TextBuffer(dedent `
if (a) {
b();
if (c) {
d()
if (e) {
f()
}
g()
}
h()
}
i()
if (j) {
k()
}
`)
languageMode = new TextMateLanguageMode({buffer, config})
expect(languageMode.getFoldableRangeContainingPoint(Point(0, 5), 2)).toBeNull()
let range = languageMode.getFoldableRangeContainingPoint(Point(0, 10), 2)
expect(simulateFold([range])).toBe(dedent `
if (a) {⋯
}
i()
if (j) {
k()
}
`)
range = languageMode.getFoldableRangeContainingPoint(Point(7, 0), 2)
expect(simulateFold([range])).toBe(dedent `
if (a) {
b();
if (c) {⋯
}
h()
}
i()
if (j) {
k()
}
`)
range = languageMode.getFoldableRangeContainingPoint(Point(1, Infinity), 2)
expect(simulateFold([range])).toBe(dedent `
if (a) {⋯
}
i()
if (j) {
k()
}