Skip to content

Commit 80828d6

Browse files
authored
(feat) add TS plugin toggle to extension (#980)
1 parent de2bf9e commit 80828d6

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

packages/svelte-vscode/package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
"onCommand:svelte.restartLanguageServer"
4545
],
4646
"contributes": {
47+
"typescriptServerPlugins-disabled": [
48+
{
49+
"name": "typescript-svelte-plugin",
50+
"enableForWorkspaceTypeScriptVersions": true
51+
}
52+
],
4753
"configuration": {
4854
"type": "object",
4955
"title": "Svelte",
@@ -472,6 +478,10 @@
472478
{
473479
"command": "svelte.extractComponent",
474480
"title": "Svelte: Extract Component"
481+
},
482+
{
483+
"command": "svelte.toggleTsPlugin",
484+
"title": "Svelte: Toggle TS Plugin"
475485
}
476486
],
477487
"menus": {
@@ -513,6 +523,7 @@
513523
"dependencies": {
514524
"lodash": "^4.17.19",
515525
"svelte-language-server": "*",
516-
"vscode-languageclient": "^7.0.0"
526+
"vscode-languageclient": "^7.0.0",
527+
"typescript-svelte-plugin": "*"
517528
}
518529
}

packages/svelte-vscode/src/extension.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { activateTagClosing } from './html/autoClose';
2828
import { EMPTY_ELEMENTS } from './html/htmlEmptyTagsShared';
2929
import CompiledCodeContentProvider from './CompiledCodeContentProvider';
3030
import * as path from 'path';
31+
import { readFileSync, writeFileSync } from 'fs';
3132

3233
namespace TagCloseRequest {
3334
export const type: RequestType<TextDocumentPositionParams, string, any> = new RequestType(
@@ -172,6 +173,8 @@ export function activate(context: ExtensionContext) {
172173

173174
addExtracComponentCommand(getLS, context);
174175

176+
context.subscriptions.push(commands.registerCommand('svelte.toggleTsPlugin', toggleTsPlugin));
177+
175178
languages.setLanguageConfiguration('svelte', {
176179
indentationRules: {
177180
// Matches a valid opening tag that is:
@@ -384,6 +387,46 @@ function addExtracComponentCommand(getLS: () => LanguageClient, context: Extensi
384387
);
385388
}
386389

390+
function toggleTsPlugin() {
391+
const extension = extensions.getExtension('svelte.svelte-vscode');
392+
if (!extension) {
393+
// This shouldn't be possible
394+
return;
395+
}
396+
397+
const packageJson = path.join(extension.extensionPath, 'package.json');
398+
const enabled = '"typescriptServerPlugins"';
399+
const disabled = '"typescriptServerPlugins-disabled"';
400+
try {
401+
const packageText = readFileSync(packageJson, 'utf8');
402+
if (packageText.includes(disabled)) {
403+
const newText = packageText.replace(disabled, enabled);
404+
writeFileSync(packageJson, newText, 'utf8');
405+
showReload(true);
406+
} else if (packageText.includes(enabled)) {
407+
const newText = packageText.replace(enabled, disabled);
408+
writeFileSync(packageJson, newText, 'utf8');
409+
showReload(false);
410+
} else {
411+
window.showWarningMessage('Unknown Svelte for VS Code package.json status.');
412+
}
413+
} catch (err) {
414+
window.showWarningMessage('Svelte for VS Code package.json update failed.');
415+
}
416+
417+
async function showReload(enabled: boolean) {
418+
const reload = await window.showInformationMessage(
419+
` TypeScript Svelte Plugin ${
420+
enabled ? 'enabled' : 'disabled'
421+
}, please reload VS Code to restart the TS Server.`,
422+
'Reload Window'
423+
);
424+
if (reload) {
425+
commands.executeCommand('workbench.action.reloadWindow');
426+
}
427+
}
428+
}
429+
387430
function createLanguageServer(serverOptions: ServerOptions, clientOptions: LanguageClientOptions) {
388431
return new LanguageClient('svelte', 'Svelte', serverOptions, clientOptions);
389432
}

0 commit comments

Comments
 (0)