Skip to content

Commit 8def732

Browse files
Workspace formatting uses threads
1 parent 385727f commit 8def732

3 files changed

Lines changed: 85 additions & 70 deletions

File tree

libs/translation/src/lib/languages/en/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export const EN: ITranslation = {
168168
notAllFilesFormatted:
169169
'Some files were not formatted, see logs for more details',
170170
pickWorkspace:
171-
'Specify the open workspace to format all IDL files for (Task and PRO)',
171+
'Specify the open workspace to format all PRO files in workspace',
172172
},
173173
initConfig: {
174174
noWorkspaceOpen: 'No currently open workspaces',

libs/vscode/client/src/lib/helpers/vscode-display-progress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function VSCodeDisplayOrUpdateProgress(
3131
delete dialogs[id];
3232
} else {
3333
// update progress
34-
dialogs[id].progress.report({ message, increment });
34+
dialogs[id].progress.report({ message: '', increment });
3535
}
3636
return;
3737
} else {

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

Lines changed: 83 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IDL_LSP_LOG } from '@idl/logger';
2-
import { IDL_JSON_URI, IDL_NOTEBOOK_EXTENSION } from '@idl/shared';
2+
import { PRO_CODE_GLOB_PATTERN } from '@idl/shared';
33
import { IDL_TRANSLATION } from '@idl/translation';
44
import {
55
FormatWorkspacePayload,
@@ -37,12 +37,9 @@ export const ON_FORMAT_WORKSPACE = async (
3737
/**
3838
* Find files and exclude notebooks
3939
*/
40-
const files = (await IDL_INDEX.findFiles(event.folders)).filter(
41-
(file) =>
42-
!(
43-
file.toLowerCase().endsWith(IDL_NOTEBOOK_EXTENSION) ||
44-
file.toLowerCase().endsWith(IDL_JSON_URI)
45-
)
40+
const files = await IDL_INDEX.findFiles(
41+
event.folders,
42+
PRO_CODE_GLOB_PATTERN
4643
);
4744

4845
/** Track file failures */
@@ -63,74 +60,92 @@ export const ON_FORMAT_WORKSPACE = async (
6360
}
6461
);
6562

63+
/**
64+
* Track all work
65+
*/
66+
const promises: Promise<any>[] = [];
67+
6668
/**
6769
* Format each file
6870
*/
6971
for (let i = 0; i < files.length; i++) {
70-
try {
71-
/**
72-
* Resolve the fspath to our cell and retrieve code
73-
*/
74-
const info = await ResolveFSPathAndCodeForURI(
75-
URI.file(files[i]).toString()
76-
);
77-
78-
// return if nothing found
79-
if (info === undefined) {
80-
return undefined;
81-
}
82-
83-
/**
84-
* Attempt to format
85-
*/
86-
const formatted = await FormatFile({
87-
// options are ignored
88-
options: {
89-
tabSize: 2,
90-
insertSpaces: true,
91-
},
92-
textDocument: {
93-
uri: info.uri,
94-
},
95-
});
96-
97-
/**
98-
* Check if we failed to format
99-
*/
100-
if (formatted === undefined) {
101-
failures.push(files[i]);
102-
continue;
103-
}
104-
105-
// update the doc in VSCode
106-
if (info.doc !== undefined) {
107-
await UpdateDocument(info.uri, formatted, info.doc);
108-
} else {
109-
await writeFile(info.fsPath, formatted, 'utf-8');
110-
}
111-
} catch (err) {
112-
IDL_LANGUAGE_SERVER_LOGGER.log({
113-
log: IDL_LSP_LOG,
114-
type: 'error',
115-
content: [
116-
`Error trying to format file in workspace: "${files[i]}"`,
117-
err,
118-
],
119-
});
120-
failures.push(files[i]);
121-
}
122-
123-
// update progress
124-
SERVER_EVENT_MANAGER.sendNotification(
125-
LANGUAGE_SERVER_MESSAGE_LOOKUP.PROGRESS,
126-
{
127-
progressId: id,
128-
increment: 100 * (1 / files.length),
129-
title: IDL_TRANSLATION.lsp.progress.formatWorkspace,
130-
}
72+
promises.push(
73+
// eslint-disable-next-line no-async-promise-executor
74+
new Promise<void>(async (res) => {
75+
try {
76+
/**
77+
* Resolve the fspath to our cell and retrieve code
78+
*/
79+
const info = await ResolveFSPathAndCodeForURI(
80+
URI.file(files[i]).toString()
81+
);
82+
83+
// return if nothing found
84+
if (info === undefined) {
85+
res();
86+
return;
87+
}
88+
89+
/**
90+
* Attempt to format
91+
*/
92+
const formatted = await FormatFile({
93+
// options are ignored
94+
options: {
95+
tabSize: 2,
96+
insertSpaces: true,
97+
},
98+
textDocument: {
99+
uri: info.uri,
100+
},
101+
});
102+
103+
/**
104+
* Check if we failed to format
105+
*/
106+
if (formatted === undefined) {
107+
failures.push(files[i]);
108+
res();
109+
return;
110+
}
111+
112+
// update the doc in VSCode
113+
if (info.doc !== undefined) {
114+
await UpdateDocument(info.uri, formatted, info.doc);
115+
} else {
116+
await writeFile(info.fsPath, formatted, 'utf-8');
117+
}
118+
} catch (err) {
119+
IDL_LANGUAGE_SERVER_LOGGER.log({
120+
log: IDL_LSP_LOG,
121+
type: 'error',
122+
content: [
123+
`Error trying to format file in workspace: "${files[i]}"`,
124+
err,
125+
],
126+
});
127+
failures.push(files[i]);
128+
}
129+
130+
// update progress
131+
SERVER_EVENT_MANAGER.sendNotification(
132+
LANGUAGE_SERVER_MESSAGE_LOOKUP.PROGRESS,
133+
{
134+
progressId: id,
135+
increment: 100 * (1 / files.length),
136+
title: IDL_TRANSLATION.lsp.progress.formatWorkspace,
137+
}
138+
);
139+
140+
// finish promise
141+
res();
142+
})
131143
);
132144
}
133145

146+
// wait for promises to finish
147+
await Promise.all(promises);
148+
134149
// update progress
135150
SERVER_EVENT_MANAGER.sendNotification(
136151
LANGUAGE_SERVER_MESSAGE_LOOKUP.PROGRESS,

0 commit comments

Comments
 (0)