Skip to content

Commit 0b8acee

Browse files
authored
Merge pull request microsoft#246858 from andrewbranch/tsgo
Allow disabling built-in TS/JS extension in favor of tsgo
2 parents 7dd1f05 + dc2c318 commit 0b8acee

File tree

5 files changed

+115
-3
lines changed

5 files changed

+115
-3
lines changed

extensions/typescript-language-features/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@
203203
"description": "%typescript.implementationsCodeLens.enabled%",
204204
"scope": "window"
205205
},
206+
"typescript.experimental.useTsgo": {
207+
"type": "boolean",
208+
"default": false,
209+
"markdownDescription": "%typescript.useTsgo%",
210+
"scope": "window",
211+
"tags": ["experimental"]
212+
},
206213
"typescript.implementationsCodeLens.showOnInterfaceMethods": {
207214
"type": "boolean",
208215
"default": false,
@@ -1615,6 +1622,18 @@
16151622
"command": "javascript.removeUnusedImports",
16161623
"title": "%typescript.removeUnusedImports%",
16171624
"category": "JavaScript"
1625+
},
1626+
{
1627+
"command": "typescript.experimental.enableTsgo",
1628+
"title": "Use TypeScript Go (Experimental)",
1629+
"category": "TypeScript",
1630+
"enablement": "!config.typescript.experimental.useTsgo && config.typescript-go.executablePath"
1631+
},
1632+
{
1633+
"command": "typescript.experimental.disableTsgo",
1634+
"title": "Stop using TypeScript Go (Experimental)",
1635+
"category": "TypeScript",
1636+
"enablement": "config.typescript.experimental.useTsgo"
16181637
}
16191638
],
16201639
"menus": {

extensions/typescript-language-features/package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"configuration.suggest.completeFunctionCalls": "Complete functions with their parameter signature.",
1313
"configuration.suggest.includeAutomaticOptionalChainCompletions": "Enable/disable showing completions on potentially undefined values that insert an optional chain call. Requires strict null checks to be enabled.",
1414
"configuration.suggest.includeCompletionsForImportStatements": "Enable/disable auto-import-style completions on partially-typed import statements.",
15+
"typescript.useTsgo": "Disables TypeScript and JavaScript language features to allow usage of the TypeScript Go experimental extension. Requires TypeScript Go to be installed and configured. Requires reloading extensions after changing this setting.",
1516
"typescript.tsdk.desc": "Specifies the folder path to the tsserver and `lib*.d.ts` files under a TypeScript install to use for IntelliSense, for example: `./node_modules/typescript/lib`.\n\n- When specified as a user setting, the TypeScript version from `typescript.tsdk` automatically replaces the built-in TypeScript version.\n- When specified as a workspace setting, `typescript.tsdk` allows you to switch to use that workspace version of TypeScript for IntelliSense with the `TypeScript: Select TypeScript version` command.\n\nSee the [TypeScript documentation](https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-newer-typescript-versions) for more detail about managing TypeScript versions.",
1617
"typescript.disableAutomaticTypeAcquisition": "Disables [automatic type acquisition](https://code.visualstudio.com/docs/nodejs/working-with-javascript#_typings-and-automatic-type-acquisition). Automatic type acquisition fetches `@types` packages from npm to improve IntelliSense for external libraries.",
1718
"typescript.enablePromptUseWorkspaceTsdk": "Enables prompting of users to use the TypeScript version configured in the workspace for Intellisense.",

extensions/typescript-language-features/src/commands/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ActiveJsTsEditorTracker } from '../ui/activeJsTsEditorTracker';
99
import { Lazy } from '../utils/lazy';
1010
import { CommandManager } from './commandManager';
1111
import { ConfigurePluginCommand } from './configurePlugin';
12+
import { EnableTsgoCommand, DisableTsgoCommand } from './useTsgo';
1213
import { JavaScriptGoToProjectConfigCommand, TypeScriptGoToProjectConfigCommand } from './goToProjectConfiguration';
1314
import { LearnMoreAboutRefactoringsCommand } from './learnMoreAboutRefactorings';
1415
import { OpenJsDocLinkCommand } from './openJsDocLink';
@@ -35,4 +36,6 @@ export function registerBaseCommands(
3536
commandManager.register(new LearnMoreAboutRefactoringsCommand());
3637
commandManager.register(new TSServerRequestCommand(lazyClientHost));
3738
commandManager.register(new OpenJsDocLinkCommand());
39+
commandManager.register(new EnableTsgoCommand());
40+
commandManager.register(new DisableTsgoCommand());
3841
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as vscode from 'vscode';
7+
import { Command } from './commandManager';
8+
9+
export class EnableTsgoCommand implements Command {
10+
public readonly id = 'typescript.experimental.enableTsgo';
11+
12+
public async execute(): Promise<void> {
13+
await updateTsgoSetting(true);
14+
}
15+
}
16+
17+
export class DisableTsgoCommand implements Command {
18+
public readonly id = 'typescript.experimental.disableTsgo';
19+
20+
public async execute(): Promise<void> {
21+
await updateTsgoSetting(false);
22+
}
23+
}
24+
25+
/**
26+
* Updates the TypeScript Go setting and reloads extension host.
27+
* @param enable Whether to enable or disable TypeScript Go
28+
*/
29+
async function updateTsgoSetting(enable: boolean): Promise<void> {
30+
const tsgoExtension = vscode.extensions.getExtension('typescript.typescript-lsp');
31+
// Error if the TypeScript Go extension is not installed with a button to open the GitHub repo
32+
if (!tsgoExtension) {
33+
const selection = await vscode.window.showErrorMessage(
34+
vscode.l10n.t('The TypeScript Go extension is not installed.'),
35+
{
36+
title: vscode.l10n.t('Open on GitHub'),
37+
isCloseAffordance: true,
38+
}
39+
);
40+
41+
if (selection) {
42+
await vscode.env.openExternal(vscode.Uri.parse('https://github.com/microsoft/typescript-go'));
43+
}
44+
}
45+
46+
const tsConfig = vscode.workspace.getConfiguration('typescript');
47+
const currentValue = tsConfig.get<boolean>('experimental.useTsgo', false);
48+
if (currentValue === enable) {
49+
return;
50+
}
51+
52+
// Determine the target scope for the configuration update
53+
let target = vscode.ConfigurationTarget.Global;
54+
const inspect = tsConfig.inspect<boolean>('experimental.useTsgo');
55+
if (inspect?.workspaceValue !== undefined) {
56+
target = vscode.ConfigurationTarget.Workspace;
57+
} else if (inspect?.workspaceFolderValue !== undefined) {
58+
target = vscode.ConfigurationTarget.WorkspaceFolder;
59+
} else {
60+
// If setting is not defined yet, use the same scope as typescript-go.executablePath
61+
const tsgoConfig = vscode.workspace.getConfiguration('typescript-go');
62+
const tsgoInspect = tsgoConfig.inspect<string>('executablePath');
63+
64+
if (tsgoInspect?.workspaceValue !== undefined) {
65+
target = vscode.ConfigurationTarget.Workspace;
66+
} else if (tsgoInspect?.workspaceFolderValue !== undefined) {
67+
target = vscode.ConfigurationTarget.WorkspaceFolder;
68+
}
69+
}
70+
71+
// Update the setting, restart the extension host, and enable the TypeScript Go extension
72+
await tsConfig.update('experimental.useTsgo', enable, target);
73+
await vscode.commands.executeCommand('workbench.action.restartExtensionHost');
74+
}

extensions/typescript-language-features/src/extension.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as fs from 'fs';
88
import * as vscode from 'vscode';
99
import { Api, getExtensionApi } from './api';
1010
import { CommandManager } from './commands/commandManager';
11+
import { DisableTsgoCommand } from './commands/useTsgo';
1112
import { registerBaseCommands } from './commands/index';
1213
import { ElectronServiceConfigurationProvider } from './configuration/configuration.electron';
1314
import { ExperimentationTelemetryReporter, IExperimentationTelemetryReporter } from './experimentTelemetryReporter';
@@ -28,12 +29,26 @@ import * as temp from './utils/temp.electron';
2829
export function activate(
2930
context: vscode.ExtensionContext
3031
): Api {
31-
const pluginManager = new PluginManager();
32-
context.subscriptions.push(pluginManager);
33-
3432
const commandManager = new CommandManager();
3533
context.subscriptions.push(commandManager);
3634

35+
// Disable extension if using the experimental TypeScript Go extension
36+
const config = vscode.workspace.getConfiguration('typescript');
37+
const useTsgo = config.get<boolean>('experimental.useTsgo', false);
38+
39+
if (useTsgo) {
40+
commandManager.register(new DisableTsgoCommand());
41+
// Return a no-op API when disabled
42+
return {
43+
getAPI() {
44+
return undefined;
45+
}
46+
};
47+
}
48+
49+
const pluginManager = new PluginManager();
50+
context.subscriptions.push(pluginManager);
51+
3752
const onCompletionAccepted = new vscode.EventEmitter<vscode.CompletionItem>();
3853
context.subscriptions.push(onCompletionAccepted);
3954

0 commit comments

Comments
 (0)