Skip to content
Open
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
25 changes: 12 additions & 13 deletions _extension/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ export class Client {
};
}

async initialize(context: vscode.ExtensionContext): Promise<void> {
async initialize(context: vscode.ExtensionContext): Promise<vscode.Disposable> {
const exe = await getExe(context);
this.start(context, exe);
return this.start(context, exe);
}

async start(context: vscode.ExtensionContext, exe: { path: string; version: string; }): Promise<void> {
async start(context: vscode.ExtensionContext, exe: { path: string; version: string; }): Promise<vscode.Disposable> {
this.exe = exe;
this.outputChannel.appendLine(`Resolved to ${this.exe.path}`);

Expand Down Expand Up @@ -119,14 +119,12 @@ export class Client {
await this.client.start();
vscode.commands.executeCommand("setContext", "typescript.native-preview.serverRunning", true);
this.onStartedCallbacks.forEach(callback => callback());
context.subscriptions.push(
new vscode.Disposable(() => {
if (this.client) {
this.client.stop();
}
vscode.commands.executeCommand("setContext", "typescript.native-preview.serverRunning", false);
}),
);
return new vscode.Disposable(() => {
if (this.client) {
this.client.stop();
}
vscode.commands.executeCommand("setContext", "typescript.native-preview.serverRunning", false);
});
}

getCurrentExe(): { path: string; version: string; } | undefined {
Expand All @@ -145,7 +143,7 @@ export class Client {
});
}

async restart(context: vscode.ExtensionContext): Promise<void> {
async restart(context: vscode.ExtensionContext): Promise<vscode.Disposable> {
if (!this.client) {
return Promise.reject(new Error("Language client is not initialized"));
}
Expand All @@ -157,6 +155,7 @@ export class Client {
}

this.outputChannel.appendLine(`Restarting language server...`);
return this.client.restart();
this.client.restart();
return new vscode.Disposable(() => {});
}
}
18 changes: 12 additions & 6 deletions _extension/src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from "vscode";
import { Client } from "./client";

export function registerCommands(context: vscode.ExtensionContext, client: Client, outputChannel: vscode.OutputChannel, traceOutputChannel: vscode.OutputChannel): void {
export function registerEnablementCommands(context: vscode.ExtensionContext): void {
context.subscriptions.push(vscode.commands.registerCommand("typescript.native-preview.enable", () => {
// Fire and forget, because this will restart the extension host and cause an error if we await
updateUseTsgoSetting(true);
Expand All @@ -11,23 +11,29 @@ export function registerCommands(context: vscode.ExtensionContext, client: Clien
// Fire and forget, because this will restart the extension host and cause an error if we await
updateUseTsgoSetting(false);
}));
}

export function registerLanguageCommands(context: vscode.ExtensionContext, client: Client, outputChannel: vscode.OutputChannel, traceOutputChannel: vscode.OutputChannel): vscode.Disposable[] {
const disposables: vscode.Disposable[] = [];

context.subscriptions.push(vscode.commands.registerCommand("typescript.native-preview.restart", () => {
disposables.push(vscode.commands.registerCommand("typescript.native-preview.restart", () => {
return client.restart(context);
}));

context.subscriptions.push(vscode.commands.registerCommand("typescript.native-preview.output.focus", () => {
disposables.push(vscode.commands.registerCommand("typescript.native-preview.output.focus", () => {
outputChannel.show();
}));

context.subscriptions.push(vscode.commands.registerCommand("typescript.native-preview.lsp-trace.focus", () => {
disposables.push(vscode.commands.registerCommand("typescript.native-preview.lsp-trace.focus", () => {
traceOutputChannel.show();
}));

context.subscriptions.push(vscode.commands.registerCommand("typescript.native-preview.selectVersion", async () => {
disposables.push(vscode.commands.registerCommand("typescript.native-preview.selectVersion", async () => {
}));

context.subscriptions.push(vscode.commands.registerCommand("typescript.native-preview.showMenu", showCommands));
disposables.push(vscode.commands.registerCommand("typescript.native-preview.showMenu", showCommands));

return disposables;
}

/**
Expand Down
59 changes: 43 additions & 16 deletions _extension/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
import * as vscode from "vscode";

import { Client } from "./client";
import { registerCommands } from "./commands";
import {
registerEnablementCommands,
registerLanguageCommands,
} from "./commands";
import { setupStatusBar } from "./statusBar";
import { setupVersionStatusItem } from "./versionStatusItem";

export async function activate(context: vscode.ExtensionContext) {
await vscode.commands.executeCommand("setContext", "typescript.native-preview.serverRunning", false);

registerEnablementCommands(context);
const output = vscode.window.createOutputChannel("typescript-native-preview", "log");
const traceOutput = vscode.window.createOutputChannel("typescript-native-preview (LSP)");
const client = new Client(output, traceOutput);
registerCommands(context, client, output, traceOutput);
context.subscriptions.push(output, traceOutput);

context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(event => {
const majorVersion = parseInt(vscode.version.split(".")[0]);
const minorVersion = parseInt(vscode.version.split(".")[1]);
const needsExtHostRestartOnChange = majorVersion <= 1 && minorVersion < 105;
let disposeLanguageFeatures: vscode.Disposable | undefined;

context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(async event => {
if (event.affectsConfiguration("typescript.experimental.useTsgo")) {
// Delay because the command to change the config setting will restart
// the extension host, so no need to show a message
setTimeout(async () => {
const selected = await vscode.window.showInformationMessage("TypeScript Native Preview setting has changed. Restart extensions to apply changes.", "Restart Extensions");
if (selected) {
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
if (needsExtHostRestartOnChange) {
// Delay because the command to change the config setting will restart
// the extension host, so no need to show a message
setTimeout(async () => {
const selected = await vscode.window.showInformationMessage("TypeScript Native Preview setting has changed. Restart extensions to apply changes.", "Restart Extensions");
if (selected) {
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
}
}, 100);
}
else {
const useTsgo = vscode.workspace.getConfiguration("typescript").get<boolean>("experimental.useTsgo");
if (useTsgo) {
disposeLanguageFeatures = await activateLanguageFeatures(context, output, traceOutput);
context.subscriptions.push(disposeLanguageFeatures);
}
}, 100);
else {
disposeLanguageFeatures?.dispose();
disposeLanguageFeatures = undefined;
}
}
}
}));

Expand All @@ -41,10 +61,17 @@ export async function activate(context: vscode.ExtensionContext) {
}
}

await client.initialize(context);
setupStatusBar(context);
setupVersionStatusItem(context, client);
disposeLanguageFeatures = await activateLanguageFeatures(context, output, traceOutput);
context.subscriptions.push(disposeLanguageFeatures);
}

export async function deactivate(): Promise<void> {
async function activateLanguageFeatures(context: vscode.ExtensionContext, output: vscode.OutputChannel, traceOutput: vscode.OutputChannel): Promise<vscode.Disposable> {
const disposables: vscode.Disposable[] = [];

const client = new Client(output, traceOutput);
disposables.push(...registerLanguageCommands(context, client, output, traceOutput));
disposables.push(await client.initialize(context));
disposables.push(setupStatusBar());
disposables.push(...setupVersionStatusItem(client));
return vscode.Disposable.from(...disposables);
}
4 changes: 2 additions & 2 deletions _extension/src/statusBar.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as vscode from "vscode";

export function setupStatusBar(context: vscode.ExtensionContext): void {
export function setupStatusBar(): vscode.Disposable {
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarItem.text = "$(beaker) tsgo";
statusBarItem.tooltip = "TypeScript Native Preview Language Server";
statusBarItem.command = "typescript.native-preview.showMenu";
statusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.warningBackground");
statusBarItem.show();
context.subscriptions.push(statusBarItem);
return statusBarItem;
}
13 changes: 7 additions & 6 deletions _extension/src/versionStatusItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { Client } from "./client";
import { jsTsLanguageModes } from "./util";

export function setupVersionStatusItem(
context: vscode.ExtensionContext,
client: Client,
): void {
): vscode.Disposable[] {
const statusItem = vscode.languages.createLanguageStatusItem("typescript.native-preview.version", jsTsLanguageModes);
statusItem.name = "TypeScript Native Preview version";
statusItem.detail = "TypeScript Native Preview version";
context.subscriptions.push(client.onStarted(() => {
statusItem.text = client.getCurrentExe()!.version;
}));
context.subscriptions.push(statusItem);
return [
statusItem,
client.onStarted(() => {
statusItem.text = client.getCurrentExe()!.version;
}),
];
}
Loading