Skip to content

Commit 11bf618

Browse files
authored
(feat) use setting instead of command for toggling ts plugin (#983)
A command would need to be reentered every time an extension update happens
1 parent 80828d6 commit 11bf618

File tree

4 files changed

+99
-60
lines changed

4 files changed

+99
-60
lines changed

packages/svelte-vscode/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ More docs and troubleshooting: [See here](/docs/README.md).
5959

6060
### Settings
6161

62+
##### `svelte.enable-ts-plugin`
63+
64+
Enables a TypeScript plugin which provides intellisense for Svelte files inside TS/JS files. _Default_: `false`
65+
6266
##### `svelte.language-server.runtime`
6367

6468
Path to the node executable you would like to use to run the language server.

packages/svelte-vscode/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
"type": "object",
5555
"title": "Svelte",
5656
"properties": {
57+
"svelte.enable-ts-plugin": {
58+
"type": "boolean",
59+
"default": false,
60+
"title": "Enable TypeScript Svelte plugin",
61+
"description": "Enables a TypeScript plugin which provides intellisense for Svelte files inside TS/JS files."
62+
},
5763
"svelte.language-server.runtime": {
5864
"scope": "application",
5965
"type": "string",
@@ -478,10 +484,6 @@
478484
{
479485
"command": "svelte.extractComponent",
480486
"title": "Svelte: Extract Component"
481-
},
482-
{
483-
"command": "svelte.toggleTsPlugin",
484-
"title": "Svelte: Toggle TS Plugin"
485487
}
486488
],
487489
"menus": {

packages/svelte-vscode/src/extension.ts

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1+
import * as path from 'path';
12
import {
2-
workspace,
3+
commands,
34
ExtensionContext,
4-
TextDocument,
5+
extensions,
6+
IndentAction,
7+
languages,
58
Position,
9+
ProgressLocation,
610
Range,
7-
commands,
8-
window,
9-
WorkspaceEdit,
11+
TextDocument,
1012
Uri,
11-
ProgressLocation,
1213
ViewColumn,
13-
languages,
14-
IndentAction,
15-
extensions
14+
window,
15+
workspace,
16+
WorkspaceEdit
1617
} from 'vscode';
1718
import {
19+
ExecuteCommandRequest,
1820
LanguageClientOptions,
19-
TextDocumentPositionParams,
2021
RequestType,
2122
RevealOutputChannelOn,
22-
WorkspaceEdit as LSWorkspaceEdit,
2323
TextDocumentEdit,
24-
ExecuteCommandRequest
24+
TextDocumentPositionParams,
25+
WorkspaceEdit as LSWorkspaceEdit
2526
} from 'vscode-languageclient';
2627
import { LanguageClient, ServerOptions, TransportKind } from 'vscode-languageclient/node';
28+
import CompiledCodeContentProvider from './CompiledCodeContentProvider';
2729
import { activateTagClosing } from './html/autoClose';
2830
import { EMPTY_ELEMENTS } from './html/htmlEmptyTagsShared';
29-
import CompiledCodeContentProvider from './CompiledCodeContentProvider';
30-
import * as path from 'path';
31-
import { readFileSync, writeFileSync } from 'fs';
31+
import { TsPlugin } from './tsplugin';
3232

3333
namespace TagCloseRequest {
3434
export const type: RequestType<TextDocumentPositionParams, string, any> = new RequestType(
@@ -173,7 +173,7 @@ export function activate(context: ExtensionContext) {
173173

174174
addExtracComponentCommand(getLS, context);
175175

176-
context.subscriptions.push(commands.registerCommand('svelte.toggleTsPlugin', toggleTsPlugin));
176+
TsPlugin.create(context);
177177

178178
languages.setLanguageConfiguration('svelte', {
179179
indentationRules: {
@@ -387,46 +387,6 @@ function addExtracComponentCommand(getLS: () => LanguageClient, context: Extensi
387387
);
388388
}
389389

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-
430390
function createLanguageServer(serverOptions: ServerOptions, clientOptions: LanguageClientOptions) {
431391
return new LanguageClient('svelte', 'Svelte', serverOptions, clientOptions);
432392
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { readFileSync, writeFileSync } from 'fs';
2+
import { join } from 'path';
3+
import { commands, ExtensionContext, extensions, window, workspace } from 'vscode';
4+
5+
export class TsPlugin {
6+
private enabled: boolean;
7+
8+
static create(context: ExtensionContext) {
9+
new TsPlugin(context);
10+
}
11+
12+
private constructor(context: ExtensionContext) {
13+
this.enabled = this.getEnabledState();
14+
this.toggleTsPlugin(this.enabled);
15+
16+
context.subscriptions.push(
17+
workspace.onDidChangeConfiguration(() => {
18+
const enabled = this.getEnabledState();
19+
if (enabled !== this.enabled) {
20+
this.enabled = enabled;
21+
this.toggleTsPlugin(this.enabled);
22+
}
23+
})
24+
);
25+
}
26+
27+
private getEnabledState(): boolean {
28+
return workspace.getConfiguration('svelte').get<boolean>('enable-ts-plugin') ?? false;
29+
}
30+
31+
private toggleTsPlugin(enable: boolean) {
32+
const extension = extensions.getExtension('svelte.svelte-vscode');
33+
if (!extension) {
34+
// This shouldn't be possible
35+
return;
36+
}
37+
38+
const packageJson = join(extension.extensionPath, 'package.json');
39+
const enabled = '"typescriptServerPlugins"';
40+
const disabled = '"typescriptServerPlugins-disabled"';
41+
try {
42+
const packageText = readFileSync(packageJson, 'utf8');
43+
if (packageText.includes(disabled) && enable) {
44+
const newText = packageText.replace(disabled, enabled);
45+
writeFileSync(packageJson, newText, 'utf8');
46+
this.showReload(true);
47+
} else if (packageText.includes(enabled) && !enable) {
48+
const newText = packageText.replace(enabled, disabled);
49+
writeFileSync(packageJson, newText, 'utf8');
50+
this.showReload(false);
51+
} else if (!packageText.includes(enabled) && !packageText.includes(disabled)) {
52+
window.showWarningMessage('Unknown Svelte for VS Code package.json status.');
53+
}
54+
} catch (err) {
55+
window.showWarningMessage(
56+
'Svelte for VS Code package.json update failed, TypeScript plugin could not be toggled.'
57+
);
58+
}
59+
}
60+
61+
private async showReload(enabled: boolean) {
62+
// Restarting the TSServer via a commend isn't enough, the whole VS Code window needs to reload
63+
const reload = await window.showInformationMessage(
64+
` TypeScript Svelte Plugin ${
65+
enabled ? 'enabled' : 'disabled'
66+
}, please reload VS Code to restart the TS Server.`,
67+
'Reload Window'
68+
);
69+
if (reload) {
70+
commands.executeCommand('workbench.action.reloadWindow');
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)