Skip to content

Commit 3ff81e3

Browse files
committed
Refactored to add proper asynchronous parallel parsing of the documents
1 parent 04ea031 commit 3ff81e3

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

src/extension.ts

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,62 @@ import ParseEngineGateway from './parse-engine-gateway';
1212
let notifier: Notifier = new Notifier('html-css-class-completion.cache');
1313
let uniqueDefinitions: CssClassDefinition[];
1414

15-
async function cache(): Promise<void> {
16-
console.log('HTML CSS Class Completion: Looking for CSS classes on the workspace...');
17-
notifier.notify('eye', 'Looking for CSS classes on the workspace...');
18-
19-
let uris: vscode.Uri[] = await Fetcher.findAllParseableDocuments();
20-
let definitions: CssClassDefinition[] = [];
21-
22-
try {
23-
for (let uri of uris) {
24-
try {
25-
Array.prototype.push.apply(definitions, await ParseEngineGateway.callParser(uri));
26-
} catch (error) {
27-
console.error(`HTML CSS Class Completion: Failed to cache css classes from "${uri}"`);
28-
}
29-
}
15+
function cache(): Promise<void> {
16+
return new Promise<void>(async (resolve, reject): Promise<void> => {
17+
try {
18+
notifier.notify('eye', 'Looking for CSS classes on the workspace...');
19+
20+
console.log('Looking for parseable documents...');
21+
let uris: vscode.Uri[] = await Fetcher.findAllParseableDocuments();
22+
23+
console.log('Found all parseable documents.');
24+
let definitions: CssClassDefinition[] = [];
25+
26+
let failedLogs: string = '';
27+
let failedLogsCount: number = 0;
28+
29+
console.log('Parsing documents and looking for CSS class definitions...');
30+
return async.each(uris, async (uri, callback) => {
31+
try {
32+
Array.prototype.push.apply(definitions, await ParseEngineGateway.callParser(uri));
33+
callback();
34+
} catch (error) {
35+
failedLogs += `${uri.path}\n`;
36+
failedLogsCount++;
37+
callback();
38+
}
39+
}, (error) => {
40+
if (error) {
41+
console.error('Failed to parse the documents: ', error);
42+
return reject(error);
43+
}
3044

31-
uniqueDefinitions = _.uniqBy(definitions, (x) => x.className);
32-
notifier.notify('zap', 'CSS classes cached (click to cache again)');
33-
} catch (error) {
34-
console.error('HTML CSS Class Completion: Failed while looping through the documents to cache the classes definitions:', error);
35-
notifier.notify('alert', 'Failed to cache the CSS classes on the workspace (click for another attempt)');
36-
}
37-
}
45+
uniqueDefinitions = _.uniqBy(definitions, (x) => x.className);
3846

39-
function registerCompletionItemProvider(): void {
40-
47+
console.log('Sumary:');
48+
console.log(uris.length, 'parseable documents found');
49+
console.log(definitions.length, 'CSS class definitions found');
50+
console.log(uniqueDefinitions.length, 'unique CSS class definitions found');
51+
console.log(failedLogsCount, 'failed attempts to parse. List of the documents:');
52+
console.log(failedLogs);
53+
54+
notifier.notify('zap', 'CSS classes cached (click to cache again)');
55+
56+
return resolve();
57+
});
58+
} catch (error) {
59+
console.error('Failed while looping through the documents to cache the classes definitions:', error);
60+
notifier.notify('alert', 'Failed to cache the CSS classes on the workspace (click for another attempt)');
61+
return reject(error);
62+
}
63+
});
4164
}
4265

4366
export async function activate(context: vscode.ExtensionContext): Promise<void> {
4467
context.subscriptions.push(vscode.commands.registerCommand('html-css-class-completion.cache', async () => {
4568
await cache();
4669
}));
47-
70+
4871
await cache();
4972

5073
context.subscriptions.push(vscode.languages.registerCompletionItemProvider('html', {
@@ -98,4 +121,4 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
98121
export function deactivate(): void {
99122
}
100123

101-
// TODO: Look for the CSS classes in case a new file is added to the workspace. I think the API does not provide and event for that. Maybe I should consider opening a PR.
124+
// TODO: Look for CSS class definitions automatically in case a new file is added to the workspace. I think the API does not provide and event for that. Maybe I should consider opening a PR.

0 commit comments

Comments
 (0)