Skip to content

Commit

Permalink
Merge branch 'main' into exports-module
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaeumer authored Feb 2, 2024
2 parents 0bf22b9 + 295aaa3 commit 1f95cc5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
34 changes: 34 additions & 0 deletions client-node-tests/src/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,40 @@ suite('Client integration', () => {
);
});

test('Progress percentage is an integer', async () => {
const progressToken = 'TEST-PROGRESS-PERCENTAGE';
const percentages: Array<number | undefined> = [];
let currentProgressResolver: (value: unknown) => void | undefined;

// Set up middleware that calls the current resolve function when it gets its 'end' progress event.
middleware.handleWorkDoneProgress = (token: lsclient.ProgressToken, params: lsclient.WorkDoneProgressBegin | lsclient.WorkDoneProgressReport | lsclient.WorkDoneProgressEnd, next) => {
if (token === progressToken) {
const percentage = params.kind === 'report' || params.kind === 'begin' ? params.percentage : undefined;
percentages.push(percentage);

if (params.kind === 'end') {
setImmediate(currentProgressResolver);
}
}
return next(token, params);
};

// Trigger a progress event.
await new Promise<unknown>((resolve) => {
currentProgressResolver = resolve;
void client.sendRequest(
new lsclient.ProtocolRequestType<any, null, never, any, any>('testing/sendSampleProgress'),
{},
tokenSource.token,
);
});

middleware.handleWorkDoneProgress = undefined;

// Ensure percentages are rounded according to the spec
assert.deepStrictEqual(percentages, [0, 50, undefined]);
});

test('Document Formatting', async () => {
const provider = client.getFeature(lsclient.DocumentFormattingRequest.method).getProvider(document);
isDefined(provider);
Expand Down
8 changes: 8 additions & 0 deletions client-node-tests/src/servers/testServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ connection.onRequest(
void connection.sendProgress(WorkDoneProgress.type, progressToken, { kind: 'begin', title: 'Test Progress' });
void connection.sendProgress(WorkDoneProgress.type, progressToken, { kind: 'report', percentage: 50, message: 'Halfway!' });
void connection.sendProgress(WorkDoneProgress.type, progressToken, { kind: 'end', message: 'Completed!' });

// According to the spec, the reported percentage has to be an integer.
// Because JS doesn't have integer support, we have rounding code in place.
const progressToken2 = 'TEST-PROGRESS-PERCENTAGE';
const progress = connection.window.attachWorkDoneProgress(progressToken2);
progress.begin('Test Progress', 0.1);
progress.report(49.9, 'Halfway!');
progress.done();
},
);

Expand Down
10 changes: 8 additions & 2 deletions server/src/common/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ class WorkDoneProgressReporterImpl implements WorkDoneProgressReporter {
const param: WorkDoneProgressBegin = {
kind: 'begin',
title,
percentage,
message,
cancellable
};
if (typeof percentage === 'number') {
// Round to the nearest integer, because the percentage
// is a uinteger according to the specification
param.percentage = Math.round(percentage);
}
this._connection.sendProgress(WorkDoneProgress.type, this._token, param);
}

Expand All @@ -59,7 +63,9 @@ class WorkDoneProgressReporterImpl implements WorkDoneProgressReporter {
kind: 'report'
};
if (typeof arg0 === 'number') {
param.percentage = arg0;
// Round to the nearest integer, because the percentage
// is a uinteger according to the specification
param.percentage = Math.round(arg0);
if (arg1 !== undefined) {
param.message = arg1;
}
Expand Down

0 comments on commit 1f95cc5

Please sign in to comment.