Skip to content

Commit b8f8090

Browse files
committed
Fix #114 - Workaround for a bug in the VS Code API
1 parent 40bc23a commit b8f8090

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

src/extension.ts

+43-49
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,58 @@ const completionTriggerChars = ['"', '\'', ' ', '.'];
1515

1616
let caching: boolean = false;
1717

18-
function cache(): Promise<void> {
19-
return new Promise<void>(async (resolve, reject): Promise<void> => {
20-
try {
21-
notifier.notify('eye', 'Looking for CSS classes in the workspace...');
18+
async function cache(): Promise<void> {
19+
try {
20+
notifier.notify('eye', 'Looking for CSS classes in the workspace...');
2221

23-
console.log('Looking for parseable documents...');
24-
let uris: Uri[] = await Fetcher.findAllParseableDocuments();
22+
console.log('Looking for parseable documents...');
23+
let uris: Uri[] = await Fetcher.findAllParseableDocuments();
2524

26-
if (!uris) {
27-
console.log("Found no documents");
28-
notifier.statusBarItem.hide();
29-
return;
30-
}
31-
32-
console.log('Found all parseable documents.');
33-
let definitions: CssClassDefinition[] = [];
25+
if (!uris || uris.length === 0) {
26+
console.log("Found no documents");
27+
notifier.statusBarItem.hide();
28+
return;
29+
}
3430

35-
let filesParsed: number = 0;
36-
let failedLogs: string = '';
37-
let failedLogsCount: number = 0;
31+
console.log('Found all parseable documents.');
32+
let definitions: CssClassDefinition[] = [];
3833

39-
console.log('Parsing documents and looking for CSS class definitions...');
34+
let filesParsed: number = 0;
35+
let failedLogs: string = '';
36+
let failedLogsCount: number = 0;
4037

41-
try {
42-
await Bluebird.map(uris, async (uri) => {
43-
try {
44-
Array.prototype.push.apply(definitions, await ParseEngineGateway.callParser(uri));
45-
} catch (error) {
46-
failedLogs += `${uri.path}\n`;
47-
failedLogsCount++;
48-
}
49-
filesParsed++;
50-
notifier.notify('eye', 'Looking for CSS classes in the workspace... (' + ((filesParsed / uris.length) * 100).toFixed(2) + '%)', false);
51-
}, { concurrency: 30 });
52-
} catch (err) {
53-
console.error('Failed to parse the documents: ', err);
54-
notifier.notify('alert', 'Failed to cache the CSS classes in the workspace (click for another attempt)');
55-
return reject(err);
56-
}
38+
console.log('Parsing documents and looking for CSS class definitions...');
5739

58-
uniqueDefinitions = _.uniqBy(definitions, def => def.className);
40+
try {
41+
await Bluebird.map(uris, async (uri) => {
42+
try {
43+
Array.prototype.push.apply(definitions, await ParseEngineGateway.callParser(uri));
44+
} catch (error) {
45+
failedLogs += `${uri.path}\n`;
46+
failedLogsCount++;
47+
}
48+
filesParsed++;
49+
notifier.notify('eye', 'Looking for CSS classes in the workspace... (' + ((filesParsed / uris.length) * 100).toFixed(2) + '%)', false);
50+
}, { concurrency: 30 });
51+
} catch (err) {
52+
notifier.notify('alert', 'Failed to cache the CSS classes in the workspace (click for another attempt)');
53+
throw new verror.VError(err, 'Failed to parse the documents');
54+
}
5955

60-
console.log('Summary:');
61-
console.log(uris.length, 'parseable documents found');
62-
console.log(definitions.length, 'CSS class definitions found');
63-
console.log(uniqueDefinitions.length, 'unique CSS class definitions found');
64-
console.log(failedLogsCount, 'failed attempts to parse. List of the documents:');
65-
console.log(failedLogs);
56+
uniqueDefinitions = _.uniqBy(definitions, def => def.className);
6657

67-
notifier.notify('zap', 'CSS classes cached (click to cache again)');
58+
console.log('Summary:');
59+
console.log(uris.length, 'parseable documents found');
60+
console.log(definitions.length, 'CSS class definitions found');
61+
console.log(uniqueDefinitions.length, 'unique CSS class definitions found');
62+
console.log(failedLogsCount, 'failed attempts to parse. List of the documents:');
63+
console.log(failedLogs);
6864

69-
return resolve();
70-
} catch (error) {
71-
console.error('Failed to cache the class definitions during the iterations over the documents that were found:', error);
72-
notifier.notify('alert', 'Failed to cache the CSS classes in the workspace (click for another attempt)');
73-
return reject(error);
74-
}
75-
});
65+
notifier.notify('zap', 'CSS classes cached (click to cache again)');
66+
} catch (err) {
67+
notifier.notify('alert', 'Failed to cache the CSS classes in the workspace (click for another attempt)');
68+
throw new verror.VError(err, 'Failed to cache the class definitions during the iterations over the documents that were founds');
69+
}
7670
}
7771

7872
function provideCompletionItemsGenerator(languageSelector: string, classMatchRegex: RegExp, classPrefix: string = '') {

src/fetcher.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ import ParseEngineRegistry from './parse-engines/parse-engine-registry';
33

44
class Fetcher {
55
static async findAllParseableDocuments(): Promise<vscode.Uri[]> {
6+
// There's a bug in the latest version of the API in which calling vscode.workspace.findFiles
7+
// when the extension is not being executed inside a workspace, causes a "Cannot read property 'map' of undefined" error.
8+
// More info: https://github.com/zignd/HTML-CSS-Class-Completion/issues/114
9+
if (!vscode.workspace.name) {
10+
return [];
11+
}
12+
613
const includeGlobPattern = vscode.workspace.getConfiguration().get('html-css-class-completion.includeGlobPattern');
714
const excludeGlobPattern = vscode.workspace.getConfiguration().get('html-css-class-completion.excludeGlobPattern');
8-
15+
916
return await vscode.workspace.findFiles(`${includeGlobPattern}`, `${excludeGlobPattern}`);
1017
}
1118
}

0 commit comments

Comments
 (0)