Skip to content
This repository was archived by the owner on Jan 8, 2025. It is now read-only.

Commit d5dff80

Browse files
Get snippets specs passing on modern Tree-sitter
1 parent 1717027 commit d5dff80

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

spec/snippets-spec.js

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Snippets = require('../lib/snippets');
44
const {TextEditor} = require('atom');
55

66
describe("Snippets extension", () => {
7-
let editorElement, editor;
7+
let editorElement, editor, languageMode;
88

99
const simulateTabKeyEvent = (param) => {
1010
if (param == null) {
@@ -15,24 +15,28 @@ describe("Snippets extension", () => {
1515
atom.keymaps.handleKeyboardEvent(event);
1616
};
1717

18-
beforeEach(() => {
18+
beforeEach(async () => {
1919
if (atom.notifications != null) { spyOn(atom.notifications, 'addError'); }
2020
spyOn(Snippets, 'loadAll');
2121
spyOn(Snippets, 'getUserSnippetsPath').andReturn('');
2222

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');
2727

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;
3233
});
3334

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');
3640
});
3741

3842
describe("provideSnippets interface", () => {
@@ -49,13 +53,13 @@ describe("Snippets extension", () => {
4953
expect(snippetsInterface.bundledSnippetsLoaded()).toBe(true);
5054
});
5155

52-
it("resets the loaded state after snippets is deactivated", () => {
56+
it("resets the loaded state after snippets is deactivated", async () => {
5357
expect(snippetsInterface.bundledSnippetsLoaded()).toBe(false);
5458
Snippets.doneLoading();
5559
expect(snippetsInterface.bundledSnippetsLoaded()).toBe(true);
5660

57-
waitsForPromise(() => atom.packages.deactivatePackage('snippets'));
58-
waitsForPromise(() => atom.packages.activatePackage('snippets'));
61+
await atom.packages.deactivatePackage('snippets');
62+
await atom.packages.activatePackage('snippets');
5963

6064
runs(() => {
6165
expect(snippetsInterface.bundledSnippetsLoaded()).toBe(false);
@@ -604,7 +608,7 @@ third tabstop $3\
604608
});
605609

606610
describe("when the snippet spans multiple lines", () => {
607-
beforeEach(() => {
611+
beforeEach(async () => {
608612
editor.update({autoIndent: true});
609613
// editor.update() returns a Promise that never gets resolved, so we
610614
// need to return undefined to avoid a timeout in the spec.
@@ -621,10 +625,12 @@ third tabstop $3\
621625
expect(editor.getCursorBufferPosition()).toEqual([4, 4]);
622626
});
623627

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 () => {
625629
editor.setCursorScreenPosition([2, Infinity]);
626630
editor.insertNewline();
631+
await languageMode.atTransactionEnd();
627632
editor.insertText('t4b');
633+
await languageMode.atTransactionEnd();
628634
atom.commands.dispatch(editorElement, 'snippets:expand');
629635

630636
expect(editor.lineTextForBufferRow(3)).toBe(" = line 1 {"); // 4 + 1 spaces (because the tab stop is invisible)
@@ -633,25 +639,30 @@ third tabstop $3\
633639
expect(editor.getCursorBufferPosition()).toEqual([3, 4]);
634640
});
635641

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 () => {
637643
editor.setCursorScreenPosition([2, Infinity]);
638644
editor.insertNewline();
645+
await languageMode.atTransactionEnd();
639646
editor.insertText('t4');
647+
await languageMode.atTransactionEnd();
640648
atom.commands.dispatch(editorElement, 'snippets:expand');
641649

642650
expect(editor.getSelectedBufferRange()).toEqual([[3, 9], [3, 10]]);
643651
atom.commands.dispatch(editorElement, 'snippets:next-tab-stop');
644652
expect(editor.getSelectedBufferRange()).toEqual([[4, 6], [4, 13]]);
645653

646654
editor.insertText('t4');
655+
await languageMode.atTransactionEnd();
647656
atom.commands.dispatch(editorElement, 'snippets:expand');
648657

649658
expect(editor.getSelectedBufferRange()).toEqual([[4, 11], [4, 12]]);
650659
atom.commands.dispatch(editorElement, 'snippets:next-tab-stop');
651660
expect(editor.getSelectedBufferRange()).toEqual([[5, 8], [5, 15]]);
652661

653662
editor.setText(''); // Clear editor
663+
await languageMode.atTransactionEnd();
654664
editor.insertText('t4');
665+
await languageMode.atTransactionEnd();
655666
atom.commands.dispatch(editorElement, 'snippets:expand');
656667

657668
expect(editor.getSelectedBufferRange()).toEqual([[0, 5], [0, 6]]);
@@ -661,8 +672,9 @@ third tabstop $3\
661672
});
662673

663674
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 () => {
665676
editor.insertText('t113');
677+
await languageMode.atTransactionEnd();
666678
expect(editor.getCursorScreenPosition()).toEqual([0, 4]);
667679

668680
simulateTabKeyEvent();
@@ -673,6 +685,7 @@ third tabstop $3\
673685
editor.undo();
674686

675687
editor.insertText("tt1");
688+
await languageMode.atTransactionEnd();
676689
expect(editor.getCursorScreenPosition()).toEqual([0, 3]);
677690

678691
simulateTabKeyEvent();
@@ -681,8 +694,10 @@ third tabstop $3\
681694

682695
editor.undo();
683696
editor.undo();
697+
await languageMode.atTransactionEnd();
684698

685699
editor.insertText("@t1");
700+
await languageMode.atTransactionEnd();
686701
expect(editor.getCursorScreenPosition()).toEqual([0, 3]);
687702

688703
simulateTabKeyEvent();
@@ -873,16 +888,17 @@ third tabstop $3\
873888
});
874889

875890
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 () => {
877892
editor.setText('t12');
878893
editor.setCursorScreenPosition([0, 3]);
879894
simulateTabKeyEvent();
880895
expect(editor.getText()).toBe("[b][/b]");
896+
await languageMode.atTransactionEnd();
881897
editor.insertText('img src');
882898
expect(editor.getText()).toBe("[img src][/img]");
883899
});
884900

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 () => {
886902
editor.setText('t12');
887903
editor.setCursorScreenPosition([0, 3]);
888904
simulateTabKeyEvent();
@@ -1362,18 +1378,23 @@ foo\
13621378
});
13631379

13641380
describe("when the editor is not a pane item (regression)", () => {
1365-
it("handles tab stops correctly", () => {
1381+
it("handles tab stops correctly", async () => {
13661382
editor = new TextEditor();
13671383
atom.grammars.assignLanguageMode(editor, 'source.js');
1384+
let languageMode = editor.getBuffer().getLanguageMode();
13681385
editorElement = editor.getElement();
1386+
await languageMode.ready;
13691387

13701388
editor.insertText('t2');
1389+
await languageMode.atTransactionEnd();
13711390
simulateTabKeyEvent();
13721391
editor.insertText('ABC');
1392+
await languageMode.atTransactionEnd();
13731393
expect(editor.getText()).toContain('go here first:(ABC)');
13741394

13751395
editor.undo();
13761396
editor.undo();
1397+
await languageMode.atTransactionEnd();
13771398
expect(editor.getText()).toBe('t2');
13781399
simulateTabKeyEvent();
13791400
editor.insertText('ABC');

0 commit comments

Comments
 (0)