Skip to content

Commit e0f21ec

Browse files
Keen Yee Liauayazhafiz
andcommitted
feat: Add restart command (#392)
* feat: Add restart command This PR adds `Angular: Restart Angular server' command to the extension. This will kill the existing language server then spawns a new process. PR closes #267 * Update package.json Co-Authored-By: hafiz <[email protected]>
1 parent 55c3b91 commit e0f21ec

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

client/src/commands.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import * as vscode from 'vscode';
10+
import * as lsp from 'vscode-languageclient';
11+
12+
/**
13+
* Represent a vscode command with an ID and an impl function `execute`.
14+
*/
15+
interface Command {
16+
id: string;
17+
execute(): Promise<vscode.Disposable>;
18+
}
19+
20+
/**
21+
* Restart the language server by killing the process then spanwing a new one.
22+
* @param client language client
23+
*/
24+
function restartNgServer(client: lsp.LanguageClient): Command {
25+
return {
26+
id: 'angular.restartNgServer',
27+
async execute() {
28+
await client.stop();
29+
return client.start();
30+
},
31+
};
32+
}
33+
34+
/**
35+
* Register all supported vscode commands for the Angular extension.
36+
* @param client language client
37+
*/
38+
export function registerCommands(client: lsp.LanguageClient): vscode.Disposable[] {
39+
const commands: Command[] = [
40+
restartNgServer(client),
41+
];
42+
43+
const disposables = commands.map((command) => {
44+
return vscode.commands.registerCommand(command.id, command.execute);
45+
});
46+
47+
return disposables;
48+
}

client/src/extension.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77
*/
88

99
import * as path from 'path';
10-
import {ExtensionContext, ProgressLocation, window, workspace} from 'vscode';
11-
import {LanguageClient, LanguageClientOptions, RevealOutputChannelOn, ServerOptions, TransportKind} from 'vscode-languageclient';
10+
import * as vscode from 'vscode';
11+
import * as lsp from 'vscode-languageclient';
1212

13+
import {registerCommands} from './commands';
1314
import {projectLoadingNotification} from './protocol';
1415

15-
export function activate(context: ExtensionContext) {
16+
export function activate(context: vscode.ExtensionContext) {
1617
// Log file does not yet exist on disk. It is up to the server to create the
1718
// file.
1819
const logFile = path.join(context.logPath, 'nglangsvc.log');
1920

2021
// If the extension is launched in debug mode then the debug server options are used
2122
// Otherwise the run options are used
22-
const serverOptions: ServerOptions = {
23+
const serverOptions: lsp.ServerOptions = {
2324
run: {
2425
module: context.asAbsolutePath(path.join('server', 'server.js')),
25-
transport: TransportKind.ipc,
26+
transport: lsp.TransportKind.ipc,
2627
args: [
2728
'--logFile', logFile,
2829
// TODO: Might want to turn off logging completely.
@@ -36,7 +37,7 @@ export function activate(context: ExtensionContext) {
3637
},
3738
debug: {
3839
module: context.asAbsolutePath(path.join('server', 'out', 'server.js')),
39-
transport: TransportKind.ipc,
40+
transport: lsp.TransportKind.ipc,
4041
args: [
4142
'--logFile',
4243
logFile,
@@ -60,7 +61,7 @@ export function activate(context: ExtensionContext) {
6061
};
6162

6263
// Options to control the language client
63-
const clientOptions: LanguageClientOptions = {
64+
const clientOptions: lsp.LanguageClientOptions = {
6465
// Register the server for Angular templates and TypeScript documents
6566
documentSelector: [
6667
// scheme: 'file' means listen to changes to files on disk only
@@ -72,31 +73,33 @@ export function activate(context: ExtensionContext) {
7273
synchronize: {
7374
fileEvents: [
7475
// Notify the server about file changes to tsconfig.json contained in the workspace
75-
workspace.createFileSystemWatcher('**/tsconfig.json'),
76+
vscode.workspace.createFileSystemWatcher('**/tsconfig.json'),
7677
]
7778
},
7879

7980
// Don't let our output console pop open
80-
revealOutputChannelOn: RevealOutputChannelOn.Never
81+
revealOutputChannelOn: lsp.RevealOutputChannelOn.Never
8182
};
8283

8384
// Create the language client and start the client.
8485
const forceDebug = !!process.env['NG_DEBUG'];
8586
const client =
86-
new LanguageClient('Angular Language Service', serverOptions, clientOptions, forceDebug);
87-
const disposable = client.start();
87+
new lsp.LanguageClient('Angular Language Service', serverOptions, clientOptions, forceDebug);
8888

8989
// Push the disposable to the context's subscriptions so that the
9090
// client can be deactivated on extension deactivation
91-
context.subscriptions.push(disposable);
91+
context.subscriptions.push(
92+
...registerCommands(client),
93+
client.start(),
94+
);
9295

9396
client.onReady().then(() => {
9497
const projectLoadingTasks = new Map<string, {resolve: () => void}>();
9598

9699
client.onNotification(projectLoadingNotification.start, (projectName: string) => {
97-
window.withProgress(
100+
vscode.window.withProgress(
98101
{
99-
location: ProgressLocation.Window,
102+
location: vscode.ProgressLocation.Window,
100103
title: 'Initializing Angular language features',
101104
},
102105
() => new Promise((resolve) => {

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
"categories": [
1717
"Programming Languages"
1818
],
19+
"contributes": {
20+
"commands": [
21+
{
22+
"command": "angular.restartNgServer",
23+
"title": "Restart Angular Language server",
24+
"category": "Angular"
25+
}
26+
]
27+
},
1928
"activationEvents": [
2029
"onLanguage:html",
2130
"onLanguage:typescript"

0 commit comments

Comments
 (0)