Skip to content

Commit c6daabb

Browse files
committed
debounce the squiggles processing
1 parent 67fa524 commit c6daabb

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

Extension/src/LanguageServer/configurations.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { PersistentFolderState } from './persistentState';
2424
import { CppSettings, OtherSettings } from './settings';
2525
import { SettingsPanel } from './settingsPanel';
2626
import { ConfigurationType, getUI } from './ui';
27+
import { Deferral } from './utils';
2728
import escapeStringRegExp = require('escape-string-regexp');
2829

2930
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
@@ -279,13 +280,13 @@ export class CppProperties {
279280

280281
vscode.workspace.onDidChangeTextDocument((e) => {
281282
if (e.document.uri.fsPath === settingsPath) {
282-
void this.handleSquiggles().catch(logAndReturn.undefined);
283+
this.handleSquiggles(e.document);
283284
}
284285
});
285286

286287
vscode.workspace.onDidOpenTextDocument(document => {
287288
if (document.uri.fsPath === settingsPath) {
288-
void this.handleSquiggles().catch(logAndReturn.undefined);
289+
this.handleSquiggles(document);
289290
}
290291
});
291292

@@ -352,7 +353,7 @@ export class CppProperties {
352353

353354
private onSelectionChanged(): void {
354355
this.selectionChanged.fire(this.CurrentConfigurationIndex);
355-
void this.handleSquiggles().catch(logAndReturn.undefined);
356+
this.handleSquiggles();
356357
}
357358

358359
private onCompileCommandsChanged(path: string): void {
@@ -1590,8 +1591,9 @@ export class CppProperties {
15901591
if (firstParse) {
15911592
// Check documents that are already open since no event will fire for them.
15921593
const fsPath = this.propertiesFile.fsPath;
1593-
if (vscode.workspace.textDocuments.some(doc => doc.uri.fsPath === fsPath)) {
1594-
void this.handleSquiggles().catch(logAndReturn.undefined);
1594+
const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === fsPath);
1595+
if (document) {
1596+
this.handleSquiggles(document);
15951597
}
15961598
}
15971599
} catch (errJS) {
@@ -1859,7 +1861,22 @@ export class CppProperties {
18591861
return errorMsg;
18601862
}
18611863

1862-
private async handleSquiggles(): Promise<void> {
1864+
private lastConfigurationVersion: number = 0;
1865+
private handleSquigglesDeferral: Deferral | undefined;
1866+
1867+
private handleSquiggles(doc?: vscode.TextDocument): void {
1868+
// When the active config changes, we don't pass the doc in since the version would not have changed.
1869+
if (doc?.version !== this.lastConfigurationVersion) {
1870+
this.lastConfigurationVersion = doc?.version ?? 0;
1871+
this.handleSquigglesDeferral?.cancel();
1872+
this.handleSquigglesDeferral = new Deferral(() => void this.handleSquigglesImpl().catch(logAndReturn.undefined), 1000);
1873+
}
1874+
}
1875+
1876+
/**
1877+
* Not to be called directly. Use the `handleSquiggles` method instead which will debounce the calls.
1878+
*/
1879+
private async handleSquigglesImpl(): Promise<void> {
18631880
if (!this.propertiesFile) {
18641881
return;
18651882
}

Extension/src/LanguageServer/dataBinding.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,7 @@
33
* See 'LICENSE' in the project root for license information.
44
* ------------------------------------------------------------------------------------------ */
55
import * as vscode from 'vscode';
6-
7-
class Deferral {
8-
private timer?: NodeJS.Timeout;
9-
10-
constructor(callback: () => void, timeout: number) {
11-
this.timer = setTimeout(() => {
12-
this.timer = undefined;
13-
callback();
14-
}, timeout);
15-
}
16-
public cancel() {
17-
if (this.timer) {
18-
clearTimeout(this.timer);
19-
this.timer = undefined;
20-
}
21-
}
22-
}
6+
import { Deferral } from './utils';
237

248
export class DataBinding<T> {
259
private valueChanged = new vscode.EventEmitter<T>();

Extension/src/LanguageServer/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,23 @@ export async function checkDuration<T>(fn: () => Promise<T>): Promise<{ result:
118118
const result = await fn();
119119
return { result, duration: performance.now() - start };
120120
}
121+
122+
/**
123+
* A class that delays the execution of a callback until a specified timeout period has elapsed.
124+
*/
125+
export class Deferral {
126+
private timer?: NodeJS.Timeout;
127+
128+
constructor(callback: () => void, timeout: number) {
129+
this.timer = setTimeout(() => {
130+
this.timer = undefined;
131+
callback();
132+
}, timeout);
133+
}
134+
public cancel() {
135+
if (this.timer) {
136+
clearTimeout(this.timer);
137+
this.timer = undefined;
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)