Skip to content

Commit ffa4481

Browse files
kyliauKeen Yee Liau
authored andcommitted
feat: new command 'openAngularLog' for opening log file from editor
Add a new command for opening the Angular log file right from the editor.
1 parent cdb3751 commit ffa4481

File tree

6 files changed

+61
-13
lines changed

6 files changed

+61
-13
lines changed

client/src/commands.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,67 @@
88

99
import * as vscode from 'vscode';
1010
import * as lsp from 'vscode-languageclient';
11+
import {ServerOptions} from '../../common/out/initialize';
1112

1213
/**
1314
* Represent a vscode command with an ID and an impl function `execute`.
1415
*/
1516
interface Command {
1617
id: string;
17-
execute(): Promise<vscode.Disposable>;
18+
execute(): Promise<unknown>;
1819
}
1920

2021
/**
2122
* Restart the language server by killing the process then spanwing a new one.
2223
* @param client language client
24+
* @param context extension context for adding disposables
2325
*/
24-
function restartNgServer(client: lsp.LanguageClient): Command {
26+
function restartNgServer(client: lsp.LanguageClient, context: vscode.ExtensionContext): Command {
2527
return {
2628
id: 'angular.restartNgServer',
2729
async execute() {
2830
await client.stop();
29-
return client.start();
31+
context.subscriptions.push(client.start());
32+
},
33+
};
34+
}
35+
36+
/**
37+
* Open the current server log file in a new editor.
38+
*/
39+
function openLogFile(client: lsp.LanguageClient): Command {
40+
return {
41+
id: 'angular.openLogFile',
42+
async execute() {
43+
const serverOptions: ServerOptions|undefined = client.initializeResult?.serverOptions;
44+
if (!serverOptions?.logFile) {
45+
// TODO: We could show a MessageItem to help users automatically update
46+
// the configuration option then restart the server, but we currently do
47+
// not reload the server options when restarting the server.
48+
vscode.window.showErrorMessage(
49+
`Angular Server logging is off. Please set 'angular.log' and reload the window.`);
50+
return;
51+
}
52+
const document = await vscode.workspace.openTextDocument(serverOptions.logFile);
53+
return vscode.window.showTextDocument(document);
3054
},
3155
};
3256
}
3357

3458
/**
3559
* Register all supported vscode commands for the Angular extension.
3660
* @param client language client
61+
* @param context extension context for adding disposables
3762
*/
38-
export function registerCommands(client: lsp.LanguageClient): vscode.Disposable[] {
63+
export function registerCommands(
64+
client: lsp.LanguageClient, context: vscode.ExtensionContext): void {
3965
const commands: Command[] = [
40-
restartNgServer(client),
66+
restartNgServer(client, context),
67+
openLogFile(client),
4168
];
4269

43-
const disposables = commands.map((command) => {
44-
return vscode.commands.registerCommand(command.id, command.execute);
45-
});
46-
47-
return disposables;
70+
for (const command of commands) {
71+
const disposable = vscode.commands.registerCommand(command.id, command.execute);
72+
context.subscriptions.push(disposable);
73+
}
4874
}

client/src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export function activate(context: vscode.ExtensionContext) {
5353

5454
// Push the disposable to the context's subscriptions so that the
5555
// client can be deactivated on extension deactivation
56-
context.subscriptions.push(...registerCommands(client), client.start());
56+
registerCommands(client, context);
57+
context.subscriptions.push(client.start());
5758

5859
client.onDidChangeState((e: lsp.StateChangeEvent) => {
5960
if (e.newState === lsp.State.Running) {

common/initialize.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
export interface ServerOptions {
10+
logFile?: string;
11+
}

integration/lsp/viewengine_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('initialization', () => {
114114
});
115115
client.listen();
116116
const response = await initializeServer(client);
117-
expect(response).toEqual({
117+
expect(response).toEqual(jasmine.objectContaining({
118118
capabilities: {
119119
textDocumentSync: 2,
120120
completionProvider: {
@@ -130,7 +130,7 @@ describe('initialization', () => {
130130
},
131131
},
132132
},
133-
});
133+
}));
134134
client.dispose();
135135
});
136136
});

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
"command": "angular.restartNgServer",
2323
"title": "Restart Angular Language server",
2424
"category": "Angular"
25+
},
26+
{
27+
"command": "angular.openLogFile",
28+
"title": "Open Angular Server log",
29+
"category": "Angular"
2530
}
2631
],
2732
"configuration": {

server/src/session.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import * as ts from 'typescript/lib/tsserverlibrary';
1010
import * as lsp from 'vscode-languageserver';
1111

12+
import {ServerOptions} from '../../common/out/initialize';
1213
import * as notification from '../../common/out/notifications';
1314

1415
import {tsCompletionEntryToLspCompletionItem} from './completion';
@@ -247,6 +248,9 @@ export class Session {
247248
}
248249

249250
private onInitialize(params: lsp.InitializeParams): lsp.InitializeResult {
251+
const serverOptions: ServerOptions = {
252+
logFile: this.logger.getLogFileName(),
253+
};
250254
return {
251255
capabilities: {
252256
textDocumentSync: lsp.TextDocumentSyncKind.Incremental,
@@ -263,6 +267,7 @@ export class Session {
263267
workspaceFolders: {supported: true},
264268
},
265269
},
270+
serverOptions,
266271
};
267272
}
268273

0 commit comments

Comments
 (0)