Skip to content

VSCode: Add activationEvents for glimmer-ts/js file types #895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import {
commands,
languages,
ViewColumn,
window,
Uri,
Range,
Position,
CodeAction,
workspace,
TextEditor,
} from 'vscode';
import { commands, languages, ViewColumn, window, Uri, Range } from 'vscode';
import * as path from 'path';
import { describe, afterEach, test } from 'mocha';
import { expect } from 'expect';
Expand All @@ -28,15 +17,28 @@ describe('Smoke test: Loose Mode + GTS with TS Plugin Mode', () => {
describe('loose mode aka ts + hbs two-file components', () => {
describe('diagnostics', () => {
test('reports errors and errors disappear when fixed', async () => {
let scriptURI = Uri.file(`${rootDir}/app/components/colocated-layout-with-errors.hbs`);
let tsScriptURI = Uri.file(`${rootDir}/app/components/colocated-layout-with-errors.ts`);

// Open the backing TS component file. Currently this is required in order to activate the VSCode,
// which is currently only configured to activate for .gts and .gjs files.
await window.showTextDocument(tsScriptURI, {
viewColumn: ViewColumn.One,
});

let hbsScriptURI = Uri.file(`${rootDir}/app/components/colocated-layout-with-errors.hbs`);

// Open the script and the template
let scriptEditor = await window.showTextDocument(scriptURI, { viewColumn: ViewColumn.One });
let scriptEditor = await window.showTextDocument(hbsScriptURI, {
viewColumn: ViewColumn.One,
});

// Wait for a diagnostic to appear in the template
await waitUntil(() => languages.getDiagnostics(scriptURI).length, 'diagnostic to appear');
await waitUntil(
() => languages.getDiagnostics(hbsScriptURI).length,
'diagnostic to appear',
);

expect(languages.getDiagnostics(scriptURI)).toMatchObject([
expect(languages.getDiagnostics(hbsScriptURI)).toMatchObject([
{
message:
"Property 'messageeee' does not exist on type 'ColocatedLayoutComponent'. Did you mean 'message'?",
Expand All @@ -52,65 +54,10 @@ describe('Smoke test: Loose Mode + GTS with TS Plugin Mode', () => {

// Wait for the diagnostic to disappear
await waitUntil(
() => languages.getDiagnostics(scriptURI).length == 0,
() => languages.getDiagnostics(hbsScriptURI).length == 0,
'diagnostic to disappear',
);
});
});
});
});

/**
* It takes a little while for the TS Plugin to fully activate, and unfortunately
* VSCode won't automatically re-trigger/re-calculate diagnostics for a file after
* a TS Plugin kicks in, so we need some way to know that the TS Plugin is activated
* before we edit the file.
*
* To accomplish this, this function inserts invalid TS into the .gts file and waits
* for diagnostics to show up.
*/
async function hackishlyWaitForTypescriptPluginToActivate(
scriptEditor: TextEditor,
scriptURI: Uri,
): Promise<void> {
let invalidAssignment = 'let s: string = 123;';
await scriptEditor.edit((edit) => {
edit.insert(new Position(0, 0), invalidAssignment);
});

let numSpacesAdded = 0;
const startTime = Date.now();

// eslint-disable-next-line no-constant-condition
while (true) {
await scriptEditor.edit((edit) => {
edit.insert(new Position(0, 0), ' ');
});
numSpacesAdded++;

if (languages.getDiagnostics(scriptURI).length) {
break;
}

if (Date.now() - startTime > 5000) {
throw new Error(
'Timed out waiting for TS Plugin to activate (i.e. waiting for diagnostics to show up)',
);
}

// We'd love to wait for a smaller increment than 1000 but the editor
// debounces before triggering diagnostics so we need a large enough time.
await new Promise((resolve) => setTimeout(resolve, 1000));
}

// Remove our invalid assignment
await scriptEditor.edit((edit) => {
edit.replace(new Range(0, 0, 0, invalidAssignment.length + numSpacesAdded), '');
});

await new Promise((resolve) => setTimeout(resolve, 1000));

if (languages.getDiagnostics(scriptURI).length) {
throw new Error('Diagnostics still showing up after removing invalid assignment');
}
}
5 changes: 4 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
"release-plan": {
"skipNpmPublish": true
},
"activationEvents": [],
"activationEvents": [
"onLanguage:glimmer-js",
"onLanguage:glimmer-ts"
],
"contributes": {
"languages": [
{
Expand Down
Loading