Skip to content

Commit 2188f70

Browse files
WIP: Format workspace pipes
1 parent 0ee6266 commit 2188f70

5 files changed

Lines changed: 129 additions & 10 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as vscode from 'vscode';
2+
3+
/**
4+
* Track known dialogs
5+
*/
6+
const dialogs: {
7+
[key: string]: {
8+
progress: vscode.Progress<{
9+
message?: string;
10+
increment?: number;
11+
}>;
12+
token: vscode.CancellationToken;
13+
resolve: (value: unknown) => void;
14+
};
15+
} = {};
16+
17+
/**
18+
* Shows progress messages and.or updates progress for dialogs that exist
19+
*/
20+
export function VSCodeDisplayOrUpdateProgress(
21+
id: string,
22+
message: string,
23+
increment: number,
24+
finished?: boolean
25+
) {
26+
// check for known progress
27+
if (id in dialogs) {
28+
// if finished, then close
29+
if (finished) {
30+
dialogs[id].resolve(null);
31+
delete dialogs[id];
32+
} else {
33+
// update progress
34+
dialogs[id].progress.report({ message, increment });
35+
}
36+
return;
37+
} else {
38+
// return if we have progress that is finished that we dont know
39+
if (finished) {
40+
return;
41+
}
42+
}
43+
44+
/**
45+
* Create new progress dialog
46+
*/
47+
vscode.window.withProgress(
48+
{
49+
location: vscode.ProgressLocation.Notification,
50+
title: message,
51+
},
52+
(progress, token) => {
53+
// get reference to callback for completion
54+
let resolver: (value: unknown) => void;
55+
56+
// create promise to resolve when we are complete
57+
const prom = new Promise((res, rej) => {
58+
resolver = res;
59+
});
60+
61+
// save as pending dialog
62+
dialogs[id] = { progress, token, resolve: resolver };
63+
64+
// return overall promise
65+
return prom;
66+
}
67+
);
68+
}

libs/vscode/client/src/lib/start-language-server.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from 'vscode-languageclient/node';
3131

3232
import { ON_INDEX } from './events/indexing/on-index';
33+
import { VSCodeDisplayOrUpdateProgress } from './helpers/vscode-display-progress';
3334
import { IDL_CLIENT_OUTPUT_CHANNEL, IDL_LOGGER } from './initialize-client';
3435
import { START_LANGUAGE_SERVER_CONFIG } from './start-language-server.interface';
3536

@@ -240,6 +241,19 @@ export async function StartLanguageServer(ctx: ExtensionContext) {
240241
}
241242
);
242243

244+
// listen for log messages from the server
245+
LANGUAGE_SERVER_MESSENGER.onNotification(
246+
LANGUAGE_SERVER_MESSAGE_LOOKUP.PROGRESS,
247+
(payload) => {
248+
VSCodeDisplayOrUpdateProgress(
249+
payload.progressId,
250+
payload.title,
251+
payload.increment,
252+
payload.finished
253+
);
254+
}
255+
);
256+
243257
// listen for usage metrics
244258
LANGUAGE_SERVER_MESSENGER.onNotification(
245259
LANGUAGE_SERVER_MESSAGE_LOOKUP.USAGE_METRIC,

libs/vscode/events/messages/src/lib/messages/progress.message.interface.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export interface ProgressMessagePayload {
88
/** ID of the progress we are sending a message for */
99
progressId: string;
1010
/** Progress message percentage from 0 to 100 */
11-
percent: number;
11+
increment: number;
1212
/** Title of the progress message */
1313
title: string;
14+
/** If set, the indicated progress message is finished */
15+
finished?: boolean;
1416
}

libs/vscode/server/src/lib/events/custom-events/on-format-workspace.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { IDL_LSP_LOG } from '@idl/logger';
2-
import { IDL_NOTEBOOK_EXTENSION } from '@idl/shared';
2+
import { IDL_JSON_URI, IDL_NOTEBOOK_EXTENSION, Sleep } from '@idl/shared';
33
import { IDL_TRANSLATION } from '@idl/translation';
44
import {
55
FormatWorkspacePayload,
66
FormatWorkspaceResponse,
77
LANGUAGE_SERVER_MESSAGE_LOOKUP,
88
} from '@idl/vscode/events/messages';
9+
import { writeFile } from 'fs/promises';
910
import { nanoid } from 'nanoid';
1011
import { URI } from 'vscode-uri';
1112

@@ -29,12 +30,19 @@ export const ON_FORMAT_WORKSPACE = async (
2930
): Promise<FormatWorkspaceResponse> => {
3031
await SERVER_INITIALIZED;
3132

33+
/** ID for progress bar */
34+
const id = nanoid();
35+
3236
try {
3337
/**
3438
* Find files and exclude notebooks
3539
*/
3640
const files = (await IDL_INDEX.findFiles(event.folders)).filter(
37-
(file) => !file.toLowerCase().endsWith(IDL_NOTEBOOK_EXTENSION)
41+
(file) =>
42+
!(
43+
file.toLowerCase().endsWith(IDL_NOTEBOOK_EXTENSION) ||
44+
file.toLowerCase().endsWith(IDL_JSON_URI)
45+
)
3846
);
3947

4048
/** Track file failures */
@@ -45,15 +53,12 @@ export const ON_FORMAT_WORKSPACE = async (
4553
return { failures };
4654
}
4755

48-
// init progress
49-
const id = nanoid();
50-
5156
// send message to start progress
5257
SERVER_EVENT_MANAGER.sendNotification(
5358
LANGUAGE_SERVER_MESSAGE_LOOKUP.PROGRESS,
5459
{
5560
progressId: id,
56-
percent: 0,
61+
increment: 0,
5762
title: IDL_TRANSLATION.lsp.progress.formatWorkspace,
5863
}
5964
);
@@ -97,8 +102,16 @@ export const ON_FORMAT_WORKSPACE = async (
97102
continue;
98103
}
99104

105+
console.log(formatted);
106+
100107
// update the doc in VSCode
101-
await UpdateDocument(info.uri, formatted, info.doc);
108+
if (info.doc !== undefined) {
109+
await UpdateDocument(info.uri, formatted, info.doc);
110+
} else {
111+
await writeFile(info.fsPath, formatted, 'utf-8');
112+
}
113+
114+
await Sleep(1000);
102115
} catch (err) {
103116
IDL_LANGUAGE_SERVER_LOGGER.log({
104117
log: IDL_LSP_LOG,
@@ -116,12 +129,23 @@ export const ON_FORMAT_WORKSPACE = async (
116129
LANGUAGE_SERVER_MESSAGE_LOOKUP.PROGRESS,
117130
{
118131
progressId: id,
119-
percent: 100 * Math.floor((i + 1) / files.length),
132+
increment: 100 * (1 / files.length),
120133
title: IDL_TRANSLATION.lsp.progress.formatWorkspace,
121134
}
122135
);
123136
}
124137

138+
// update progress
139+
SERVER_EVENT_MANAGER.sendNotification(
140+
LANGUAGE_SERVER_MESSAGE_LOOKUP.PROGRESS,
141+
{
142+
progressId: id,
143+
increment: 1 / files.length,
144+
title: IDL_TRANSLATION.lsp.progress.formatWorkspace,
145+
finished: true,
146+
}
147+
);
148+
125149
return {
126150
failures,
127151
};
@@ -132,5 +156,16 @@ export const ON_FORMAT_WORKSPACE = async (
132156
content: [`Error trying to format code in workspace`, err],
133157
alert: IDL_TRANSLATION.lsp.events.onWorkspaceFormatting,
134158
});
159+
160+
// update progress
161+
SERVER_EVENT_MANAGER.sendNotification(
162+
LANGUAGE_SERVER_MESSAGE_LOOKUP.PROGRESS,
163+
{
164+
progressId: id,
165+
increment: 1,
166+
title: IDL_TRANSLATION.lsp.progress.formatWorkspace,
167+
finished: true,
168+
}
169+
);
135170
}
136171
};

libs/vscode/server/src/lib/events/initialize-custom-event-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function InitializeCustomEventHandler() {
4343
);
4444

4545
// listen for workspace formatting
46-
SERVER_EVENT_MANAGER.onNotification(
46+
SERVER_EVENT_MANAGER.onRequest(
4747
LANGUAGE_SERVER_MESSAGE_LOOKUP.FORMAT_WORKSPACE,
4848
ON_FORMAT_WORKSPACE
4949
);

0 commit comments

Comments
 (0)