-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.ts
59 lines (47 loc) · 1.99 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"use strict";
import * as vscode from "vscode";
let restartTsServerStatusBarItem: vscode.StatusBarItem;
const TYPESCRIPT_EXTENSION_ID = 'vscode.typescript-language-features';
const RESTART_TS_SERVER_LABEL = "$(debug-restart) Restart TS server";
const SUPPORTED_LANGUAGES = [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
];
export async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand("restartTsServerButton.softRestartTsServer", softRestartTsServer));
restartTsServerStatusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
0
);
restartTsServerStatusBarItem.command = "restartTsServerButton.softRestartTsServer";
restartTsServerStatusBarItem.text = RESTART_TS_SERVER_LABEL;
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(updateStatusBarItemVisibility));
context.subscriptions.push(vscode.window.onDidChangeTextEditorSelection(updateStatusBarItemVisibility));
context.subscriptions.push(vscode.workspace.onDidCloseTextDocument(updateStatusBarItemVisibility));
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(updateStatusBarItemVisibility));
updateStatusBarItemVisibility();
}
async function softRestartTsServer() {
const typeScriptExtension = vscode.extensions.getExtension(TYPESCRIPT_EXTENSION_ID);
if (!typeScriptExtension || typeScriptExtension.isActive === false) {
vscode.window.showErrorMessage("TypeScript extension is not active or not running.");
return;
}
await vscode.commands.executeCommand("typescript.restartTsServer");
}
function updateStatusBarItemVisibility(): void {
const { activeTextEditor } = vscode.window;
if (!activeTextEditor
|| !activeTextEditor.document
|| SUPPORTED_LANGUAGES.indexOf(activeTextEditor.document.languageId) === -1
) {
restartTsServerStatusBarItem.hide();
} else {
restartTsServerStatusBarItem.show();
}
}
// this method is called when your extension is deactivated
export function deactivate() {
}