Skip to content

Commit

Permalink
add show{Information,Warning,Error}message to VS Code API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Donham committed Jun 28, 2024
1 parent b1927ff commit a62b281
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 6 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ getSession(
options: vscode.AuthenticationGetSessionOptions
): Promise<vscode.AuthenticationSession>;

showInformationMessage<string>(
message: string,
options: vscode.MessageOptions,
...items: string[]
): Promise<string | undefined>;
showWarningMessage<string>(
message: string,
options: vscode.MessageOptions,
...items: string[]
): Promise<string | undefined>;
showErrorMessage<string>(
message: string,
options: vscode.MessageOptions,
...items: string[]
): Promise<string | undefined>;
```

## Development
Expand Down
81 changes: 75 additions & 6 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export function jsonView(obj: object) {
return { data: JSON.stringify(obj), mime: "application/x-json-view" };
}

// VS Code API

const rpcKey = "__vitale_rpc__";

export function getSession(
providerId: string,
scopes: readonly string[],
Expand All @@ -64,11 +68,76 @@ export function getSession(
scopes: readonly string[],
options?: vscode.AuthenticationGetSessionOptions
): Thenable<vscode.AuthenticationSession | undefined>;
export function getSession(
providerId: string,
scopes: readonly string[],
options?: vscode.AuthenticationGetSessionOptions
) {
export function getSession() {
// @ts-ignore
return global[rpcKey].getSession(...arguments);
}

export function showInformationMessage<T extends string>(
message: string,
...items: T[]
): Thenable<T | undefined>;
export function showInformationMessage<T extends string>(
message: string,
options: vscode.MessageOptions,
...items: T[]
): Thenable<T | undefined>;
export function showInformationMessage<T extends vscode.MessageItem>(
message: string,
...items: T[]
): Thenable<T | undefined>;
export function showInformationMessage<T extends vscode.MessageItem>(
message: string,
options: vscode.MessageOptions,
...items: T[]
): Thenable<T | undefined>;
export function showInformationMessage() {
// @ts-ignore
return global[rpcKey].showInformationMessage(...arguments);
}

export function showWarningMessage<T extends string>(
message: string,
...items: T[]
): Thenable<T | undefined>;
export function showWarningMessage<T extends string>(
message: string,
options: vscode.MessageOptions,
...items: T[]
): Thenable<T | undefined>;
export function showWarningMessage<T extends vscode.MessageItem>(
message: string,
...items: T[]
): Thenable<T | undefined>;
export function showWarningMessage<T extends vscode.MessageItem>(
message: string,
options: vscode.MessageOptions,
...items: T[]
): Thenable<T | undefined>;
export function showWarningMessage() {
// @ts-ignore
return global[rpcKey].showWarningMessage(...arguments);
}

export function showErrorMessage<T extends string>(
message: string,
...items: T[]
): Thenable<T | undefined>;
export function showErrorMessage<T extends string>(
message: string,
options: vscode.MessageOptions,
...items: T[]
): Thenable<T | undefined>;
export function showErrorMessage<T extends vscode.MessageItem>(
message: string,
...items: T[]
): Thenable<T | undefined>;
export function showErrorMessage<T extends vscode.MessageItem>(
message: string,
options: vscode.MessageOptions,
...items: T[]
): Thenable<T | undefined>;
export function showErrorMessage() {
// @ts-ignore
return global["__vitale_rpc__"].getSession(providerId, scopes, options);
return global[rpcKey].showErrorMessage(...arguments);
}
3 changes: 3 additions & 0 deletions packages/server/src/rpc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ export type ClientFunctions = {

// VS Code API
getSession: typeof vscode.authentication.getSession;
showInformationMessage: typeof vscode.window.showInformationMessage;
showWarningMessage: typeof vscode.window.showWarningMessage;
showErrorMessage: typeof vscode.window.showErrorMessage;
};
45 changes: 45 additions & 0 deletions packages/server/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,51 @@ export class Rpc {
return sessions[0];
}

async showInformationMessage(
message: string,
options: vscode.MessageOptions,
...items: vscode.MessageItem[]
) {
// TODO(jaked)
// it doesn't make sense to call this for all clients
const res = await Promise.all(
Array.from(this.clients.values()).map((client) =>
client.showInformationMessage(message, options, ...items)
)
);
return res[0];
}

async showWarningMessage(
message: string,
options: vscode.MessageOptions,
...items: vscode.MessageItem[]
) {
// TODO(jaked)
// it doesn't make sense to call this for all clients
const res = await Promise.all(
Array.from(this.clients.values()).map((client) =>
client.showWarningMessage(message, options, ...items)
)
);
return res[0];
}

async showErrorMessage(
message: string,
options: vscode.MessageOptions,
...items: vscode.MessageItem[]
) {
// TODO(jaked)
// it doesn't make sense to call this for all clients
const res = await Promise.all(
Array.from(this.clients.values()).map((client) =>
client.showErrorMessage(message, options, ...items)
)
);
return res[0];
}

markCellsDirty(cells: { path: string; cellId: string }[]) {
if (cells.length === 0) {
return;
Expand Down
3 changes: 3 additions & 0 deletions packages/vscode/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export class NotebookController {

// VS Code API
getSession: vscode.authentication.getSession,
showInformationMessage: vscode.window.showInformationMessage,
showWarningMessage: vscode.window.showWarningMessage,
showErrorMessage: vscode.window.showErrorMessage,
},
{
post: (msg) => ws.send(msg),
Expand Down

0 comments on commit a62b281

Please sign in to comment.