Skip to content

Commit

Permalink
Add support for progress reporting (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
planger committed Jul 13, 2023
1 parent 7a8e21f commit 67947ae
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 73 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
### Changes

- [launch] Socket-based launch quickstart components now support auto-assigned ports [#33](https://github.com/eclipse-glsp/glsp-vscode-integration/pull/33)
- [launch ] Provide `NodeGlspVscodeServer` to enable direct server integration in the extension context without a dedicated server process [#37](https://github.com/eclipse-glsp/glsp-vscode-integration/pull/37)
- [launch] Provide `NodeGlspVscodeServer` to enable direct server integration in the extension context without a dedicated server process [#37](https://github.com/eclipse-glsp/glsp-vscode-integration/pull/37)
- [diagram] Fixed a bug that prevented proper server-side disposal of diagram sessions [#40](https://github.com/eclipse-glsp/glsp-vscode-integration/pull/40)
- [API] Restructured packages to also provide a node-dependency free entry point for web-extensions ('@eclipse-glsp/vscode-integration/browser`) [#39](https://github.com/eclipse-glsp/glsp-vscode-integration/pull/39)
- [diagram] Add support for server progress reporting [#47](https://github.com/eclipse-glsp/glsp-vscode-integration/pull/47)

### Breaking Changes

Expand Down
89 changes: 88 additions & 1 deletion packages/vscode-integration/src/common/glsp-vscode-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import {
Action,
ActionMessage,
Deferred,
EndProgressAction,
ExportSvgAction,
InitializeClientSessionParameters,
InitializeResult,
Expand All @@ -27,7 +29,9 @@ import {
ServerMessageAction,
SetDirtyStateAction,
SetMarkersAction,
UndoAction
StartProgressAction,
UndoAction,
UpdateProgressAction
} from '@eclipse-glsp/protocol';
import * as vscode from 'vscode';
import { Disposable } from 'vscode-jsonrpc';
Expand All @@ -45,6 +49,15 @@ export interface MessageProcessingResult {
}

export type SelectionState = Omit<SelectAction, 'kind'>;

interface ProgressReporter {
deferred: Deferred<void>;
progress: vscode.Progress<{
message?: string | undefined;
increment?: number | undefined;
}>;
}

/**
* The `GlspVscodeConnector` acts as the bridge between GLSP-Clients and the GLSP-Server
* and is at the core of the Glsp-VSCode integration.
Expand Down Expand Up @@ -79,6 +92,7 @@ export class GlspVscodeConnector<D extends vscode.CustomDocument = vscode.Custom
vscode.CustomDocumentEditEvent<D> | vscode.CustomDocumentContentChangeEvent<D>
>();
protected readonly disposables: vscode.Disposable[] = [];
protected readonly progressReporters: Map<string, ProgressReporter> = new Map();

/**
* A subscribable event which fires with an array containing the IDs of all
Expand Down Expand Up @@ -283,6 +297,17 @@ export class GlspVscodeConnector<D extends vscode.CustomDocument = vscode.Custom
return this.handleServerMessageAction(message as ActionMessage<ServerMessageAction>, client, origin);
}

// progress reporting
if (StartProgressAction.is(message.action)) {
return this.handleStartProgressAction(message as ActionMessage<StartProgressAction>, client, origin);
}
if (UpdateProgressAction.is(message.action)) {
return this.handleUpdateProgressAction(message as ActionMessage<UpdateProgressAction>, client, origin);
}
if (EndProgressAction.is(message.action)) {
return this.handleEndProgressAction(message as ActionMessage<EndProgressAction>, client, origin);
}

// Dirty state & save actions
if (SetDirtyStateAction.is(message.action)) {
return this.handleSetDirtyStateAction(message as ActionMessage<SetDirtyStateAction>, client, origin);
Expand Down Expand Up @@ -330,6 +355,68 @@ export class GlspVscodeConnector<D extends vscode.CustomDocument = vscode.Custom
return { processedMessage: undefined, messageChanged: true };
}

protected handleStartProgressAction(
actionMessage: ActionMessage<StartProgressAction>,
client: GlspVscodeClient<D> | undefined,
origin: MessageOrigin
): MessageProcessingResult {
if (client) {
const { progressId, title, message, percentage } = actionMessage.action;
const deferred = new Deferred<void>();
const location = vscode.ProgressLocation.Notification;
vscode.window.withProgress({ title, location }, progress => {
const reporterId = this.progressReporterId(client, progressId);
this.progressReporters.set(reporterId, { deferred, progress });
progress.report({ message, increment: percentage });
return deferred.promise;
});
}

// Do not propagate action
return { processedMessage: undefined, messageChanged: true };
}

protected handleUpdateProgressAction(
actionMessage: ActionMessage<UpdateProgressAction>,
client: GlspVscodeClient<D> | undefined,
origin: MessageOrigin
): MessageProcessingResult {
if (client) {
const { progressId, message, percentage } = actionMessage.action;
const reporterId = this.progressReporterId(client, progressId);
const reporter = this.progressReporters.get(reporterId);
if (reporter) {
reporter.progress.report({ message, increment: percentage });
}
}

// Do not propagate action
return { processedMessage: undefined, messageChanged: true };
}

protected handleEndProgressAction(
actionMessage: ActionMessage<EndProgressAction>,
client: GlspVscodeClient<D> | undefined,
origin: MessageOrigin
): MessageProcessingResult {
if (client) {
const { progressId } = actionMessage.action;
const reporterId = this.progressReporterId(client, progressId);
const reporter = this.progressReporters.get(reporterId);
if (reporter) {
reporter.deferred.resolve();
this.progressReporters.delete(reporterId);
}
}

// Do not propagate action
return { processedMessage: undefined, messageChanged: true };
}

protected progressReporterId(client: GlspVscodeClient<D>, progressId: string): string {
return `${client.clientId}_${progressId}`;
}

protected handleSetDirtyStateAction(
message: ActionMessage<SetDirtyStateAction>,
client: GlspVscodeClient<D> | undefined,
Expand Down

0 comments on commit 67947ae

Please sign in to comment.