Merge pull request #246858 from andrewbranch/tsgo

Allow disabling built-in TS/JS extension in favor of tsgo
This commit is contained in:
Matt Bierner
2025-04-29 15:36:52 -07:00
committed by GitHub
5 changed files with 115 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ import { ActiveJsTsEditorTracker } from '../ui/activeJsTsEditorTracker';
import { Lazy } from '../utils/lazy';
import { CommandManager } from './commandManager';
import { ConfigurePluginCommand } from './configurePlugin';
import { EnableTsgoCommand, DisableTsgoCommand } from './useTsgo';
import { JavaScriptGoToProjectConfigCommand, TypeScriptGoToProjectConfigCommand } from './goToProjectConfiguration';
import { LearnMoreAboutRefactoringsCommand } from './learnMoreAboutRefactorings';
import { OpenJsDocLinkCommand } from './openJsDocLink';
@@ -35,4 +36,6 @@ export function registerBaseCommands(
commandManager.register(new LearnMoreAboutRefactoringsCommand());
commandManager.register(new TSServerRequestCommand(lazyClientHost));
commandManager.register(new OpenJsDocLinkCommand());
commandManager.register(new EnableTsgoCommand());
commandManager.register(new DisableTsgoCommand());
}

View File

@@ -0,0 +1,74 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { Command } from './commandManager';
export class EnableTsgoCommand implements Command {
public readonly id = 'typescript.experimental.enableTsgo';
public async execute(): Promise<void> {
await updateTsgoSetting(true);
}
}
export class DisableTsgoCommand implements Command {
public readonly id = 'typescript.experimental.disableTsgo';
public async execute(): Promise<void> {
await updateTsgoSetting(false);
}
}
/**
* Updates the TypeScript Go setting and reloads extension host.
* @param enable Whether to enable or disable TypeScript Go
*/
async function updateTsgoSetting(enable: boolean): Promise<void> {
const tsgoExtension = vscode.extensions.getExtension('typescript.typescript-lsp');
// Error if the TypeScript Go extension is not installed with a button to open the GitHub repo
if (!tsgoExtension) {
const selection = await vscode.window.showErrorMessage(
vscode.l10n.t('The TypeScript Go extension is not installed.'),
{
title: vscode.l10n.t('Open on GitHub'),
isCloseAffordance: true,
}
);
if (selection) {
await vscode.env.openExternal(vscode.Uri.parse('https://github.com/microsoft/typescript-go'));
}
}
const tsConfig = vscode.workspace.getConfiguration('typescript');
const currentValue = tsConfig.get<boolean>('experimental.useTsgo', false);
if (currentValue === enable) {
return;
}
// Determine the target scope for the configuration update
let target = vscode.ConfigurationTarget.Global;
const inspect = tsConfig.inspect<boolean>('experimental.useTsgo');
if (inspect?.workspaceValue !== undefined) {
target = vscode.ConfigurationTarget.Workspace;
} else if (inspect?.workspaceFolderValue !== undefined) {
target = vscode.ConfigurationTarget.WorkspaceFolder;
} else {
// If setting is not defined yet, use the same scope as typescript-go.executablePath
const tsgoConfig = vscode.workspace.getConfiguration('typescript-go');
const tsgoInspect = tsgoConfig.inspect<string>('executablePath');
if (tsgoInspect?.workspaceValue !== undefined) {
target = vscode.ConfigurationTarget.Workspace;
} else if (tsgoInspect?.workspaceFolderValue !== undefined) {
target = vscode.ConfigurationTarget.WorkspaceFolder;
}
}
// Update the setting, restart the extension host, and enable the TypeScript Go extension
await tsConfig.update('experimental.useTsgo', enable, target);
await vscode.commands.executeCommand('workbench.action.restartExtensionHost');
}

View File

@@ -8,6 +8,7 @@ import * as fs from 'fs';
import * as vscode from 'vscode';
import { Api, getExtensionApi } from './api';
import { CommandManager } from './commands/commandManager';
import { DisableTsgoCommand } from './commands/useTsgo';
import { registerBaseCommands } from './commands/index';
import { ElectronServiceConfigurationProvider } from './configuration/configuration.electron';
import { ExperimentationTelemetryReporter, IExperimentationTelemetryReporter } from './experimentTelemetryReporter';
@@ -28,12 +29,26 @@ import * as temp from './utils/temp.electron';
export function activate(
context: vscode.ExtensionContext
): Api {
const pluginManager = new PluginManager();
context.subscriptions.push(pluginManager);
const commandManager = new CommandManager();
context.subscriptions.push(commandManager);
// Disable extension if using the experimental TypeScript Go extension
const config = vscode.workspace.getConfiguration('typescript');
const useTsgo = config.get<boolean>('experimental.useTsgo', false);
if (useTsgo) {
commandManager.register(new DisableTsgoCommand());
// Return a no-op API when disabled
return {
getAPI() {
return undefined;
}
};
}
const pluginManager = new PluginManager();
context.subscriptions.push(pluginManager);
const onCompletionAccepted = new vscode.EventEmitter<vscode.CompletionItem>();
context.subscriptions.push(onCompletionAccepted);