Skip to content

Commit 4c7a38f

Browse files
committed
Move Razor command registration to activation
1 parent 2590a23 commit 4c7a38f

File tree

3 files changed

+58
-43
lines changed

3 files changed

+58
-43
lines changed

src/lsptoolshost/roslynLanguageClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import CompositeDisposable from "../CompositeDisposable";
88
import { IDisposable } from "../Disposable";
99

1010
/**
11-
* Implementation of the base LangaugeClient type that allows for additional items to be disposed of
11+
* Implementation of the base LanguageClient type that allows for additional items to be disposed of
1212
* when the base LanguageClient instance is disposed.
1313
*/
1414
export class RoslynLanguageClient extends LanguageClient {

src/lsptoolshost/roslynLanguageServer.ts

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ export class RoslynLanguageServer {
191191
});
192192

193193
this.registerExtensionsChanged(this._languageClient);
194-
this.registerTelemtryChanged(this._languageClient);
194+
this.registerTelemetryChanged(this._languageClient);
195195

196196
// Start the client. This will also launch the server
197197
this._languageClient.start();
198198

199199
// Register Razor dynamic file info handling
200-
this.registerRazor(this._languageClient);
200+
this.registerDynamicFileInfo(this._languageClient);
201201
}
202202

203203
public async stop(): Promise<void> {
@@ -256,6 +256,18 @@ export class RoslynLanguageServer {
256256
return response;
257257
}
258258

259+
/**
260+
* Sends an LSP notification to the server with a given method and parameters.
261+
*/
262+
public async sendNotification<Params>(method: string, params: Params): Promise<any> {
263+
if (!this.isRunning()) {
264+
throw new Error('Tried to send request while server is not started.');
265+
}
266+
267+
let response = await this._languageClient!.sendNotification(method, params);
268+
return response;
269+
}
270+
259271
public async registerSolutionSnapshot(token: vscode.CancellationToken) : Promise<SolutionSnapshotId> {
260272
let response = await _languageServer.sendRequest0(RegisterSolutionSnapshotRequest.type, token);
261273
if (response)
@@ -430,7 +442,7 @@ export class RoslynLanguageServer {
430442
return childProcess;
431443
}
432444

433-
private registerRazor(client: RoslynLanguageClient) {
445+
private registerDynamicFileInfo(client: RoslynLanguageClient) {
434446
// When the Roslyn language server sends a request for Razor dynamic file info, we forward that request along to Razor via
435447
// a command.
436448
client.onRequest(
@@ -439,43 +451,6 @@ export class RoslynLanguageServer {
439451
client.onNotification(
440452
RoslynLanguageServer.removeRazorDynamicFileInfoMethodName,
441453
async notification => vscode.commands.executeCommand(DynamicFileInfoHandler.removeDynamicFileInfoCommand, notification));
442-
443-
// Razor will call into us (via command) for generated file didChange/didClose notifications. We'll then forward these
444-
// notifications along to Roslyn. didOpen notifications are handled separately via the vscode.openTextDocument method.
445-
client.addDisposable(vscode.commands.registerCommand(RoslynLanguageServer.roslynDidChangeCommand, (notification: DidChangeTextDocumentParams) => {
446-
client.sendNotification(DidChangeTextDocumentNotification.method, notification);
447-
}));
448-
client.addDisposable(vscode.commands.registerCommand(RoslynLanguageServer.roslynDidCloseCommand, (notification: DidCloseTextDocumentParams) => {
449-
client.sendNotification(DidCloseTextDocumentNotification.method, notification);
450-
}));
451-
client.addDisposable(vscode.commands.registerCommand(RoslynLanguageServer.roslynPullDiagnosticCommand, async (request: DocumentDiagnosticParams) => {
452-
let diagnosticRequestType = new RequestType<DocumentDiagnosticParams, DocumentDiagnosticReport, any>(DocumentDiagnosticRequest.method);
453-
return await this.sendRequest(diagnosticRequestType, request, CancellationToken.None);
454-
}));
455-
456-
// The VS Code API for code actions (and the vscode.CodeAction type) doesn't support everything that LSP supports,
457-
// namely the data property, which Razor needs to identify which code actions are on their allow list, so we need
458-
// to expose a command for them to directly invoke our code actions LSP endpoints, rather than use built-in commands.
459-
client.addDisposable(vscode.commands.registerCommand(RoslynLanguageServer.provideCodeActionsCommand, async (request: CodeActionParams) => {
460-
return await this.sendRequest(CodeActionRequest.type, request, CancellationToken.None);
461-
}));
462-
client.addDisposable(vscode.commands.registerCommand(RoslynLanguageServer.resolveCodeActionCommand, async (request: CodeAction) => {
463-
return await this.sendRequest(CodeActionResolveRequest.type, request, CancellationToken.None);
464-
}));
465-
466-
client.addDisposable(vscode.commands.registerCommand(RoslynLanguageServer.provideCompletionsCommand, async (request: CompletionParams) => {
467-
return await this.sendRequest(CompletionRequest.type, request, CancellationToken.None);
468-
}));
469-
client.addDisposable(vscode.commands.registerCommand(RoslynLanguageServer.resolveCompletionsCommand, async (request: CompletionItem) => {
470-
return await this.sendRequest(CompletionResolveRequest.type, request, CancellationToken.None);
471-
}));
472-
473-
// Roslyn is responsible for producing a json file containing information for Razor, that comes from the compilation for
474-
// a project. We want to defer this work until necessary, so this command is called by the Razor document manager to tell
475-
// us when they need us to initialize the Razor things.
476-
client.addDisposable(vscode.commands.registerCommand(RoslynLanguageServer.razorInitializeCommand, () => {
477-
client.sendNotification("razor/initialize", { });
478-
}));
479454
}
480455

481456
private registerExtensionsChanged(languageClient: RoslynLanguageClient) {
@@ -503,7 +478,7 @@ export class RoslynLanguageServer {
503478
}));
504479
}
505480

506-
private registerTelemtryChanged(languageClient: RoslynLanguageClient) {
481+
private registerTelemetryChanged(languageClient: RoslynLanguageClient) {
507482
// Subscribe to telemetry events so we can enable/disable as needed
508483
languageClient.addDisposable(vscode.env.onDidChangeTelemetryEnabled((isEnabled: boolean) => {
509484
const title = 'Restart Language Server';
@@ -608,6 +583,8 @@ export async function activateRoslynLanguageServer(context: vscode.ExtensionCont
608583
// Register any commands that need to be handled by the extension.
609584
registerCommands(context, _languageServer);
610585

586+
registerRazorCommands(context, _languageServer);
587+
611588
// Register any needed debugger components that need to communicate with the language server.
612589
registerDebugger(context, _languageServer, platformInfo, optionProvider, _channel);
613590

@@ -646,6 +623,45 @@ export async function activateRoslynLanguageServer(context: vscode.ExtensionCont
646623
_languageServer.start();
647624
}
648625

626+
function registerRazorCommands(context: vscode.ExtensionContext, languageServer: RoslynLanguageServer) {
627+
// Razor will call into us (via command) for generated file didChange/didClose notifications. We'll then forward these
628+
// notifications along to Roslyn. didOpen notifications are handled separately via the vscode.openTextDocument method.
629+
context.subscriptions.push(vscode.commands.registerCommand(RoslynLanguageServer.roslynDidChangeCommand, async (notification: DidChangeTextDocumentParams) => {
630+
await languageServer.sendNotification(DidChangeTextDocumentNotification.method, notification);
631+
}));
632+
context.subscriptions.push(vscode.commands.registerCommand(RoslynLanguageServer.roslynDidCloseCommand, async (notification: DidCloseTextDocumentParams) => {
633+
await languageServer.sendNotification(DidCloseTextDocumentNotification.method, notification);
634+
}));
635+
context.subscriptions.push(vscode.commands.registerCommand(RoslynLanguageServer.roslynPullDiagnosticCommand, async (request: DocumentDiagnosticParams) => {
636+
let diagnosticRequestType = new RequestType<DocumentDiagnosticParams, DocumentDiagnosticReport, any>(DocumentDiagnosticRequest.method);
637+
return await languageServer.sendRequest(diagnosticRequestType, request, CancellationToken.None);
638+
}));
639+
640+
// The VS Code API for code actions (and the vscode.CodeAction type) doesn't support everything that LSP supports,
641+
// namely the data property, which Razor needs to identify which code actions are on their allow list, so we need
642+
// to expose a command for them to directly invoke our code actions LSP endpoints, rather than use built-in commands.
643+
context.subscriptions.push(vscode.commands.registerCommand(RoslynLanguageServer.provideCodeActionsCommand, async (request: CodeActionParams) => {
644+
return await languageServer.sendRequest(CodeActionRequest.type, request, CancellationToken.None);
645+
}));
646+
context.subscriptions.push(vscode.commands.registerCommand(RoslynLanguageServer.resolveCodeActionCommand, async (request: CodeAction) => {
647+
return await languageServer.sendRequest(CodeActionResolveRequest.type, request, CancellationToken.None);
648+
}));
649+
650+
context.subscriptions.push(vscode.commands.registerCommand(RoslynLanguageServer.provideCompletionsCommand, async (request: CompletionParams) => {
651+
return await languageServer.sendRequest(CompletionRequest.type, request, CancellationToken.None);
652+
}));
653+
context.subscriptions.push(vscode.commands.registerCommand(RoslynLanguageServer.resolveCompletionsCommand, async (request: CompletionItem) => {
654+
return await languageServer.sendRequest(CompletionResolveRequest.type, request, CancellationToken.None);
655+
}));
656+
657+
// Roslyn is responsible for producing a json file containing information for Razor, that comes from the compilation for
658+
// a project. We want to defer this work until necessary, so this command is called by the Razor document manager to tell
659+
// us when they need us to initialize the Razor things.
660+
context.subscriptions.push(vscode.commands.registerCommand(RoslynLanguageServer.razorInitializeCommand, async () => {
661+
await languageServer.sendNotification("razor/initialize", { });
662+
}));
663+
}
664+
649665
async function applyAutoInsertEdit(e: vscode.TextDocumentChangeEvent, token: vscode.CancellationToken) {
650666
const change = e.contentChanges[0];
651667

src/utils/getCSharpDevKit.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { CSharpDevKitExports } from "../CSharpDevKitExports";
99
export const csharpDevkitExtensionId = "ms-dotnettools.csdevkit";
1010
export const csharpDevkitIntelliCodeExtensionId = "ms-dotnettools.vscodeintellicode-csharp";
1111

12-
1312
export function getCSharpDevKit(): vscode.Extension<CSharpDevKitExports> | undefined {
1413
return vscode.extensions.getExtension<CSharpDevKitExports>(csharpDevkitExtensionId);
1514
}

0 commit comments

Comments
 (0)