Skip to content

Commit dcc0473

Browse files
committed
Fix possibility to run the caching process multiple times simultaneously
1 parent cef6702 commit dcc0473

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
1111
"stopOnEntry": false,
1212
"sourceMaps": true,
13+
"preLaunchTask": "npm",
1314
"outFiles": [
1415
"${workspaceRoot}/out/**/*.js"
1516
]

src/extension.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ let uniqueDefinitions: CssClassDefinition[];
1212

1313
const completionTriggerChars = ['"', '\'', ' '];
1414

15+
let caching: boolean = false;
16+
1517
function cache(): Promise<void> {
1618
return new Promise<void>(async (resolve, reject): Promise<void> => {
1719
try {
18-
notifier.notify('eye', 'Looking for CSS classes on the workspace...');
20+
notifier.notify('eye', 'Looking for CSS classes in the workspace...');
1921

2022
console.log('Looking for parseable documents...');
2123
let uris: vscode.Uri[] = await Fetcher.findAllParseableDocuments();
@@ -62,8 +64,8 @@ function cache(): Promise<void> {
6264
return resolve();
6365
});
6466
} catch (error) {
65-
console.error('Failed while looping through the documents to cache the classes definitions:', error);
66-
notifier.notify('alert', 'Failed to cache the CSS classes on the workspace (click for another attempt)');
67+
console.error('Failed to cache the class definitions during the iterations over the documents that were found:', error);
68+
notifier.notify('alert', 'Failed to cache the CSS classes in the workspace (click for another attempt)');
6769
return reject(error);
6870
}
6971
});
@@ -106,7 +108,15 @@ function provideCompletionItemsGenerator(languageSelector: string, classMatchReg
106108

107109
export async function activate(context: vscode.ExtensionContext): Promise<void> {
108110
context.subscriptions.push(vscode.commands.registerCommand('html-css-class-completion.cache', async () => {
109-
await cache();
111+
if (caching)
112+
return;
113+
114+
caching = true;
115+
try {
116+
await cache();
117+
} finally {
118+
caching = false;
119+
}
110120
}));
111121

112122
const htmlRegex = /class=["|']([\w- ]*$)/;
@@ -138,10 +148,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
138148
context.subscriptions.push(hbs);
139149
context.subscriptions.push(ejs);
140150

141-
await cache();
151+
caching = true;
152+
try {
153+
await cache();
154+
} finally {
155+
caching = false;
156+
}
142157
}
143158

144159
export function deactivate(): void {
145160
}
146-
147-
// 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.

src/fetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Fetcher {
55
static async findAllParseableDocuments(): Promise<vscode.Uri[]> {
66
const languages = ParseEngineRegistry.supportedLanguagesIds.join(',');
77

8-
return await vscode.workspace.findFiles(`**/*.{${languages}}`, '**/node_modules');
8+
return await vscode.workspace.findFiles(`**/*.{${languages}}`, '');
99
}
1010
}
1111

0 commit comments

Comments
 (0)