@@ -4,7 +4,7 @@ const Snippets = require('../lib/snippets');
4
4
const { TextEditor} = require ( 'atom' ) ;
5
5
6
6
describe ( "Snippets extension" , ( ) => {
7
- let editorElement , editor ;
7
+ let editorElement , editor , languageMode ;
8
8
9
9
const simulateTabKeyEvent = ( param ) => {
10
10
if ( param == null ) {
@@ -15,24 +15,28 @@ describe("Snippets extension", () => {
15
15
atom . keymaps . handleKeyboardEvent ( event ) ;
16
16
} ;
17
17
18
- beforeEach ( ( ) => {
18
+ beforeEach ( async ( ) => {
19
19
if ( atom . notifications != null ) { spyOn ( atom . notifications , 'addError' ) ; }
20
20
spyOn ( Snippets , 'loadAll' ) ;
21
21
spyOn ( Snippets , 'getUserSnippetsPath' ) . andReturn ( '' ) ;
22
22
23
- waitsForPromise ( ( ) => atom . workspace . open ( path . join ( __dirname , 'fixtures' , 'sample.js' ) ) ) ;
24
- waitsForPromise ( ( ) => atom . packages . activatePackage ( 'language-javascript' ) ) ;
25
- waitsForPromise ( ( ) => atom . packages . activatePackage ( 'language-html' ) ) ;
26
- waitsForPromise ( ( ) => atom . packages . activatePackage ( 'snippets' ) ) ;
23
+ await atom . workspace . open ( path . join ( __dirname , 'fixtures' , 'sample.js' ) ) ;
24
+ await atom . packages . activatePackage ( 'language-javascript' ) ;
25
+ await atom . packages . activatePackage ( 'language-html' ) ;
26
+ await atom . packages . activatePackage ( 'snippets' ) ;
27
27
28
- runs ( ( ) => {
29
- editor = atom . workspace . getActiveTextEditor ( ) ;
30
- editorElement = atom . views . getView ( editor ) ;
31
- } ) ;
28
+ editor = atom . workspace . getActiveTextEditor ( ) ;
29
+ editorElement = atom . views . getView ( editor ) ;
30
+ languageMode = editor . getBuffer ( ) . getLanguageMode ( ) ;
31
+ await languageMode . ready ;
32
+ languageMode . useAsyncParsing = false ;
32
33
} ) ;
33
34
34
- afterEach ( ( ) => {
35
- waitsForPromise ( ( ) => atom . packages . deactivatePackage ( 'snippets' ) ) ;
35
+ afterEach ( async ( ) => {
36
+ if ( languageMode ) {
37
+ await languageMode . atTransactionEnd ( ) ;
38
+ }
39
+ await atom . packages . deactivatePackage ( 'snippets' ) ;
36
40
} ) ;
37
41
38
42
describe ( "provideSnippets interface" , ( ) => {
@@ -49,13 +53,13 @@ describe("Snippets extension", () => {
49
53
expect ( snippetsInterface . bundledSnippetsLoaded ( ) ) . toBe ( true ) ;
50
54
} ) ;
51
55
52
- it ( "resets the loaded state after snippets is deactivated" , ( ) => {
56
+ it ( "resets the loaded state after snippets is deactivated" , async ( ) => {
53
57
expect ( snippetsInterface . bundledSnippetsLoaded ( ) ) . toBe ( false ) ;
54
58
Snippets . doneLoading ( ) ;
55
59
expect ( snippetsInterface . bundledSnippetsLoaded ( ) ) . toBe ( true ) ;
56
60
57
- waitsForPromise ( ( ) => atom . packages . deactivatePackage ( 'snippets' ) ) ;
58
- waitsForPromise ( ( ) => atom . packages . activatePackage ( 'snippets' ) ) ;
61
+ await atom . packages . deactivatePackage ( 'snippets' ) ;
62
+ await atom . packages . activatePackage ( 'snippets' ) ;
59
63
60
64
runs ( ( ) => {
61
65
expect ( snippetsInterface . bundledSnippetsLoaded ( ) ) . toBe ( false ) ;
@@ -604,7 +608,7 @@ third tabstop $3\
604
608
} ) ;
605
609
606
610
describe ( "when the snippet spans multiple lines" , ( ) => {
607
- beforeEach ( ( ) => {
611
+ beforeEach ( async ( ) => {
608
612
editor . update ( { autoIndent : true } ) ;
609
613
// editor.update() returns a Promise that never gets resolved, so we
610
614
// need to return undefined to avoid a timeout in the spec.
@@ -621,10 +625,12 @@ third tabstop $3\
621
625
expect ( editor . getCursorBufferPosition ( ) ) . toEqual ( [ 4 , 4 ] ) ;
622
626
} ) ;
623
627
624
- it ( "indents the subsequent lines of the snippet based on the indent level before the snippet is inserted" , ( ) => {
628
+ it ( "indents the subsequent lines of the snippet based on the indent level before the snippet is inserted" , async ( ) => {
625
629
editor . setCursorScreenPosition ( [ 2 , Infinity ] ) ;
626
630
editor . insertNewline ( ) ;
631
+ await languageMode . atTransactionEnd ( ) ;
627
632
editor . insertText ( 't4b' ) ;
633
+ await languageMode . atTransactionEnd ( ) ;
628
634
atom . commands . dispatch ( editorElement , 'snippets:expand' ) ;
629
635
630
636
expect ( editor . lineTextForBufferRow ( 3 ) ) . toBe ( " = line 1 {" ) ; // 4 + 1 spaces (because the tab stop is invisible)
@@ -633,25 +639,30 @@ third tabstop $3\
633
639
expect ( editor . getCursorBufferPosition ( ) ) . toEqual ( [ 3 , 4 ] ) ;
634
640
} ) ;
635
641
636
- it ( "does not change the relative positioning of the tab stops when inserted multiple times" , ( ) => {
642
+ it ( "does not change the relative positioning of the tab stops when inserted multiple times" , async ( ) => {
637
643
editor . setCursorScreenPosition ( [ 2 , Infinity ] ) ;
638
644
editor . insertNewline ( ) ;
645
+ await languageMode . atTransactionEnd ( ) ;
639
646
editor . insertText ( 't4' ) ;
647
+ await languageMode . atTransactionEnd ( ) ;
640
648
atom . commands . dispatch ( editorElement , 'snippets:expand' ) ;
641
649
642
650
expect ( editor . getSelectedBufferRange ( ) ) . toEqual ( [ [ 3 , 9 ] , [ 3 , 10 ] ] ) ;
643
651
atom . commands . dispatch ( editorElement , 'snippets:next-tab-stop' ) ;
644
652
expect ( editor . getSelectedBufferRange ( ) ) . toEqual ( [ [ 4 , 6 ] , [ 4 , 13 ] ] ) ;
645
653
646
654
editor . insertText ( 't4' ) ;
655
+ await languageMode . atTransactionEnd ( ) ;
647
656
atom . commands . dispatch ( editorElement , 'snippets:expand' ) ;
648
657
649
658
expect ( editor . getSelectedBufferRange ( ) ) . toEqual ( [ [ 4 , 11 ] , [ 4 , 12 ] ] ) ;
650
659
atom . commands . dispatch ( editorElement , 'snippets:next-tab-stop' ) ;
651
660
expect ( editor . getSelectedBufferRange ( ) ) . toEqual ( [ [ 5 , 8 ] , [ 5 , 15 ] ] ) ;
652
661
653
662
editor . setText ( '' ) ; // Clear editor
663
+ await languageMode . atTransactionEnd ( ) ;
654
664
editor . insertText ( 't4' ) ;
665
+ await languageMode . atTransactionEnd ( ) ;
655
666
atom . commands . dispatch ( editorElement , 'snippets:expand' ) ;
656
667
657
668
expect ( editor . getSelectedBufferRange ( ) ) . toEqual ( [ [ 0 , 5 ] , [ 0 , 6 ] ] ) ;
@@ -661,8 +672,9 @@ third tabstop $3\
661
672
} ) ;
662
673
663
674
describe ( "when multiple snippets match the prefix" , ( ) => {
664
- it ( "expands the snippet that is the longest match for the prefix" , ( ) => {
675
+ it ( "expands the snippet that is the longest match for the prefix" , async ( ) => {
665
676
editor . insertText ( 't113' ) ;
677
+ await languageMode . atTransactionEnd ( ) ;
666
678
expect ( editor . getCursorScreenPosition ( ) ) . toEqual ( [ 0 , 4 ] ) ;
667
679
668
680
simulateTabKeyEvent ( ) ;
@@ -673,6 +685,7 @@ third tabstop $3\
673
685
editor . undo ( ) ;
674
686
675
687
editor . insertText ( "tt1" ) ;
688
+ await languageMode . atTransactionEnd ( ) ;
676
689
expect ( editor . getCursorScreenPosition ( ) ) . toEqual ( [ 0 , 3 ] ) ;
677
690
678
691
simulateTabKeyEvent ( ) ;
@@ -681,8 +694,10 @@ third tabstop $3\
681
694
682
695
editor . undo ( ) ;
683
696
editor . undo ( ) ;
697
+ await languageMode . atTransactionEnd ( ) ;
684
698
685
699
editor . insertText ( "@t1" ) ;
700
+ await languageMode . atTransactionEnd ( ) ;
686
701
expect ( editor . getCursorScreenPosition ( ) ) . toEqual ( [ 0 , 3 ] ) ;
687
702
688
703
simulateTabKeyEvent ( ) ;
@@ -873,16 +888,17 @@ third tabstop $3\
873
888
} ) ;
874
889
875
890
describe ( "when the snippet contains tab stops with transformations" , ( ) => {
876
- it ( "transforms the text typed into the first tab stop before setting it in the transformed tab stop" , ( ) => {
891
+ it ( "transforms the text typed into the first tab stop before setting it in the transformed tab stop" , async ( ) => {
877
892
editor . setText ( 't12' ) ;
878
893
editor . setCursorScreenPosition ( [ 0 , 3 ] ) ;
879
894
simulateTabKeyEvent ( ) ;
880
895
expect ( editor . getText ( ) ) . toBe ( "[b][/b]" ) ;
896
+ await languageMode . atTransactionEnd ( ) ;
881
897
editor . insertText ( 'img src' ) ;
882
898
expect ( editor . getText ( ) ) . toBe ( "[img src][/img]" ) ;
883
899
} ) ;
884
900
885
- it ( "bundles the transform mutations along with the original manual mutation for the purposes of undo and redo" , ( ) => {
901
+ it ( "bundles the transform mutations along with the original manual mutation for the purposes of undo and redo" , async ( ) => {
886
902
editor . setText ( 't12' ) ;
887
903
editor . setCursorScreenPosition ( [ 0 , 3 ] ) ;
888
904
simulateTabKeyEvent ( ) ;
@@ -1362,18 +1378,23 @@ foo\
1362
1378
} ) ;
1363
1379
1364
1380
describe ( "when the editor is not a pane item (regression)" , ( ) => {
1365
- it ( "handles tab stops correctly" , ( ) => {
1381
+ it ( "handles tab stops correctly" , async ( ) => {
1366
1382
editor = new TextEditor ( ) ;
1367
1383
atom . grammars . assignLanguageMode ( editor , 'source.js' ) ;
1384
+ let languageMode = editor . getBuffer ( ) . getLanguageMode ( ) ;
1368
1385
editorElement = editor . getElement ( ) ;
1386
+ await languageMode . ready ;
1369
1387
1370
1388
editor . insertText ( 't2' ) ;
1389
+ await languageMode . atTransactionEnd ( ) ;
1371
1390
simulateTabKeyEvent ( ) ;
1372
1391
editor . insertText ( 'ABC' ) ;
1392
+ await languageMode . atTransactionEnd ( ) ;
1373
1393
expect ( editor . getText ( ) ) . toContain ( 'go here first:(ABC)' ) ;
1374
1394
1375
1395
editor . undo ( ) ;
1376
1396
editor . undo ( ) ;
1397
+ await languageMode . atTransactionEnd ( ) ;
1377
1398
expect ( editor . getText ( ) ) . toBe ( 't2' ) ;
1378
1399
simulateTabKeyEvent ( ) ;
1379
1400
editor . insertText ( 'ABC' ) ;
0 commit comments