Skip to content

Commit

Permalink
Implement support for server side notification to show a toast with
Browse files Browse the repository at this point in the history
command
  • Loading branch information
dibarbet committed Aug 7, 2023
1 parent 7dc5cbd commit cee0dbb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/lsptoolshost/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export function registerCommands(
context: vscode.ExtensionContext,
languageServer: RoslynLanguageServer,
optionProvider: OptionProvider,
hostExecutableResolver: IHostExecutableResolver
hostExecutableResolver: IHostExecutableResolver,
outputChannel: vscode.OutputChannel
) {
// It is very important to be careful about the types used as parameters for these command callbacks.
// If the arguments are coming from the server as json, it is NOT appropriate to use type definitions
Expand Down Expand Up @@ -50,6 +51,9 @@ export function registerCommands(
)
)
);
context.subscriptions.push(
vscode.commands.registerCommand('csharp.showOutputWindow', async () => outputChannel.show())
);
}

/**
Expand Down
54 changes: 53 additions & 1 deletion src/lsptoolshost/roslynLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
CompletionItem,
PartialResultParams,
ProtocolRequestType,
MessageType,
} from 'vscode-languageclient/node';
import { PlatformInformation } from '../shared/platform';
import { readConfigurations } from './configurationMiddleware';
Expand Down Expand Up @@ -198,6 +199,57 @@ export class RoslynLanguageServer {
);
});

this._languageClient.onNotification(RoslynProtocol.ShowToastNotification.type, async (notification) => {
const messageOptions: vscode.MessageOptions = {
modal: false,
};
const commands = notification.commands.map((command) => command.title);
const executeCommandByName = async (result: string | undefined) => {
if (result) {
const command = notification.commands.find((command) => command.title === result);
if (!command) {
throw new Error(`Unknown command ${result}`);
}

if (command.arguments) {
await vscode.commands.executeCommand(command.command, ...command.arguments);
} else {
await vscode.commands.executeCommand(command.command);
}
}
};

switch (notification.messageType) {
case MessageType.Error: {
const result = await vscode.window.showErrorMessage(
notification.message,
messageOptions,
...commands
);
executeCommandByName(result);
break;
}
case MessageType.Warning: {
const result = await vscode.window.showWarningMessage(
notification.message,
messageOptions,
...commands
);
executeCommandByName(result);
break;
}
default: {
const result = await vscode.window.showInformationMessage(
notification.message,
messageOptions,
...commands
);
executeCommandByName(result);
break;
}
}
});

this.registerExtensionsChanged(this._languageClient);
this.registerTelemetryChanged(this._languageClient);

Expand Down Expand Up @@ -664,7 +716,7 @@ export async function activateRoslynLanguageServer(
);

// Register any commands that need to be handled by the extension.
registerCommands(context, _languageServer, optionProvider, hostExecutableResolver);
registerCommands(context, _languageServer, optionProvider, hostExecutableResolver, _channel);

registerRazorCommands(context, _languageServer);

Expand Down
13 changes: 13 additions & 0 deletions src/lsptoolshost/roslynProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Command } from 'vscode';
import * as lsp from 'vscode-languageserver-protocol';

export interface WorkspaceDebugConfigurationParams {
Expand Down Expand Up @@ -116,6 +117,12 @@ export interface DebugAttachResult {
didAttach: boolean;
}

export interface ShowToastNotificationParams {
messageType: lsp.MessageType;
message: string;
commands: Command[];
}

export namespace WorkspaceDebugConfigurationRequest {
export const method = 'workspace/debugConfiguration';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
Expand All @@ -142,6 +149,12 @@ export namespace ProjectInitializationCompleteNotification {
export const type = new lsp.NotificationType(method);
}

export namespace ShowToastNotification {
export const method = 'window/showToast';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.serverToClient;
export const type = new lsp.NotificationType<ShowToastNotificationParams>(method);
}

export namespace RunTestsRequest {
export const method = 'textDocument/runTests';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
Expand Down

0 comments on commit cee0dbb

Please sign in to comment.