Skip to content

Commit

Permalink
Merge pull request #18 from savetheclocktower/fix-specs
Browse files Browse the repository at this point in the history
Get `snippets` specs passing on modern Tree-sitter
  • Loading branch information
savetheclocktower authored Jan 7, 2024
2 parents 1717027 + fd9a987 commit 6b91634
Showing 1 changed file with 53 additions and 22 deletions.
75 changes: 53 additions & 22 deletions spec/snippets-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const Snippets = require('../lib/snippets');
const {TextEditor} = require('atom');

describe("Snippets extension", () => {
let editorElement, editor;
let editorElement, editor, languageMode;
let modernTreeSitterIsDefault = null;

const simulateTabKeyEvent = (param) => {
if (param == null) {
Expand All @@ -15,24 +16,37 @@ describe("Snippets extension", () => {
atom.keymaps.handleKeyboardEvent(event);
};

beforeEach(() => {
beforeEach(async () => {
if (modernTreeSitterIsDefault === null) {
let oldSetting = atom.config.getSchema('core.useExperimentalModernTreeSitter');
if (oldSetting?.type === 'boolean') {
modernTreeSitterIsDefault = false;
}
}
if (!modernTreeSitterIsDefault) {
atom.config.set('core.useExperimentalModernTreeSitter', true);
}
if (atom.notifications != null) { spyOn(atom.notifications, 'addError'); }
spyOn(Snippets, 'loadAll');
spyOn(Snippets, 'getUserSnippetsPath').andReturn('');

waitsForPromise(() => atom.workspace.open(path.join(__dirname, 'fixtures', 'sample.js')));
waitsForPromise(() => atom.packages.activatePackage('language-javascript'));
waitsForPromise(() => atom.packages.activatePackage('language-html'));
waitsForPromise(() => atom.packages.activatePackage('snippets'));
await atom.workspace.open(path.join(__dirname, 'fixtures', 'sample.js'));
await atom.packages.activatePackage('language-javascript');
await atom.packages.activatePackage('language-html');
await atom.packages.activatePackage('snippets');

runs(() => {
editor = atom.workspace.getActiveTextEditor();
editorElement = atom.views.getView(editor);
});
editor = atom.workspace.getActiveTextEditor();
editorElement = atom.views.getView(editor);
languageMode = editor.getBuffer().getLanguageMode();
await languageMode.ready;
languageMode.useAsyncParsing = false;
});

afterEach(() => {
waitsForPromise(() => atom.packages.deactivatePackage('snippets'));
afterEach(async () => {
if (languageMode) {
await languageMode.atTransactionEnd();
}
await atom.packages.deactivatePackage('snippets');
});

describe("provideSnippets interface", () => {
Expand All @@ -49,13 +63,13 @@ describe("Snippets extension", () => {
expect(snippetsInterface.bundledSnippetsLoaded()).toBe(true);
});

it("resets the loaded state after snippets is deactivated", () => {
it("resets the loaded state after snippets is deactivated", async () => {
expect(snippetsInterface.bundledSnippetsLoaded()).toBe(false);
Snippets.doneLoading();
expect(snippetsInterface.bundledSnippetsLoaded()).toBe(true);

waitsForPromise(() => atom.packages.deactivatePackage('snippets'));
waitsForPromise(() => atom.packages.activatePackage('snippets'));
await atom.packages.deactivatePackage('snippets');
await atom.packages.activatePackage('snippets');

runs(() => {
expect(snippetsInterface.bundledSnippetsLoaded()).toBe(false);
Expand Down Expand Up @@ -604,7 +618,7 @@ third tabstop $3\
});

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

it("indents the subsequent lines of the snippet based on the indent level before the snippet is inserted", () => {
it("indents the subsequent lines of the snippet based on the indent level before the snippet is inserted", async () => {
editor.setCursorScreenPosition([2, Infinity]);
editor.insertNewline();
await languageMode.atTransactionEnd();
editor.insertText('t4b');
await languageMode.atTransactionEnd();
atom.commands.dispatch(editorElement, 'snippets:expand');

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

it("does not change the relative positioning of the tab stops when inserted multiple times", () => {
it("does not change the relative positioning of the tab stops when inserted multiple times", async () => {
editor.setCursorScreenPosition([2, Infinity]);
editor.insertNewline();
await languageMode.atTransactionEnd();
editor.insertText('t4');
await languageMode.atTransactionEnd();
atom.commands.dispatch(editorElement, 'snippets:expand');

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

editor.insertText('t4');
await languageMode.atTransactionEnd();
atom.commands.dispatch(editorElement, 'snippets:expand');

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

editor.setText(''); // Clear editor
await languageMode.atTransactionEnd();
editor.insertText('t4');
await languageMode.atTransactionEnd();
atom.commands.dispatch(editorElement, 'snippets:expand');

expect(editor.getSelectedBufferRange()).toEqual([[0, 5], [0, 6]]);
Expand All @@ -661,8 +682,9 @@ third tabstop $3\
});

describe("when multiple snippets match the prefix", () => {
it("expands the snippet that is the longest match for the prefix", () => {
it("expands the snippet that is the longest match for the prefix", async () => {
editor.insertText('t113');
await languageMode.atTransactionEnd();
expect(editor.getCursorScreenPosition()).toEqual([0, 4]);

simulateTabKeyEvent();
Expand All @@ -673,6 +695,7 @@ third tabstop $3\
editor.undo();

editor.insertText("tt1");
await languageMode.atTransactionEnd();
expect(editor.getCursorScreenPosition()).toEqual([0, 3]);

simulateTabKeyEvent();
Expand All @@ -681,8 +704,10 @@ third tabstop $3\

editor.undo();
editor.undo();
await languageMode.atTransactionEnd();

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

simulateTabKeyEvent();
Expand Down Expand Up @@ -873,16 +898,17 @@ third tabstop $3\
});

describe("when the snippet contains tab stops with transformations", () => {
it("transforms the text typed into the first tab stop before setting it in the transformed tab stop", () => {
it("transforms the text typed into the first tab stop before setting it in the transformed tab stop", async () => {
editor.setText('t12');
editor.setCursorScreenPosition([0, 3]);
simulateTabKeyEvent();
expect(editor.getText()).toBe("[b][/b]");
await languageMode.atTransactionEnd();
editor.insertText('img src');
expect(editor.getText()).toBe("[img src][/img]");
});

it("bundles the transform mutations along with the original manual mutation for the purposes of undo and redo", () => {
it("bundles the transform mutations along with the original manual mutation for the purposes of undo and redo", async () => {
editor.setText('t12');
editor.setCursorScreenPosition([0, 3]);
simulateTabKeyEvent();
Expand Down Expand Up @@ -1362,18 +1388,23 @@ foo\
});

describe("when the editor is not a pane item (regression)", () => {
it("handles tab stops correctly", () => {
it("handles tab stops correctly", async () => {
editor = new TextEditor();
atom.grammars.assignLanguageMode(editor, 'source.js');
let languageMode = editor.getBuffer().getLanguageMode();
editorElement = editor.getElement();
await languageMode.ready;

editor.insertText('t2');
await languageMode.atTransactionEnd();
simulateTabKeyEvent();
editor.insertText('ABC');
await languageMode.atTransactionEnd();
expect(editor.getText()).toContain('go here first:(ABC)');

editor.undo();
editor.undo();
await languageMode.atTransactionEnd();
expect(editor.getText()).toBe('t2');
simulateTabKeyEvent();
editor.insertText('ABC');
Expand Down

0 comments on commit 6b91634

Please sign in to comment.