Skip to content

Commit 90a2bed

Browse files
committed
fix delay issue for provideCodeLenses, microsoft#106267
1 parent 933bdbc commit 90a2bed

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

src/vs/base/common/async.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -614,15 +614,15 @@ export class RunOnceScheduler {
614614

615615
protected runner: ((...args: any[]) => void) | null;
616616

617-
private timeoutToken: any;
618-
private timeout: number;
619-
private timeoutHandler: () => void;
617+
private _timeoutToken: any;
618+
private _timeout: number;
619+
private _timeoutHandler: () => void;
620620

621621
constructor(runner: (...args: any[]) => void, timeout: number) {
622-
this.timeoutToken = -1;
622+
this._timeoutToken = -1;
623623
this.runner = runner;
624-
this.timeout = timeout;
625-
this.timeoutHandler = this.onTimeout.bind(this);
624+
this._timeout = timeout;
625+
this._timeoutHandler = this.onTimeout.bind(this);
626626
}
627627

628628
/**
@@ -638,28 +638,36 @@ export class RunOnceScheduler {
638638
*/
639639
cancel(): void {
640640
if (this.isScheduled()) {
641-
clearTimeout(this.timeoutToken);
642-
this.timeoutToken = -1;
641+
clearTimeout(this._timeoutToken);
642+
this._timeoutToken = -1;
643643
}
644644
}
645645

646646
/**
647647
* Cancel previous runner (if any) & schedule a new runner.
648648
*/
649-
schedule(delay = this.timeout): void {
649+
schedule(delay = this._timeout): void {
650650
this.cancel();
651-
this.timeoutToken = setTimeout(this.timeoutHandler, delay);
651+
this._timeoutToken = setTimeout(this._timeoutHandler, delay);
652+
}
653+
654+
get timeout(): number {
655+
return this._timeout;
656+
}
657+
658+
set timeout(value: number) {
659+
this._timeout = value;
652660
}
653661

654662
/**
655663
* Returns true if scheduled.
656664
*/
657665
isScheduled(): boolean {
658-
return this.timeoutToken !== -1;
666+
return this._timeoutToken !== -1;
659667
}
660668

661669
private onTimeout() {
662-
this.timeoutToken = -1;
670+
this._timeoutToken = -1;
663671
if (this.runner) {
664672
this.doRun();
665673
}

src/vs/editor/common/modes/languageFeatureRegistry.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,18 @@ export class LanguageFeatureRequestDelays {
207207

208208
get(model: ITextModel): number {
209209
const key = this._key(model);
210-
return this._clamp(this._cache.get(key)?.value);
210+
const avg = this._cache.get(key);
211+
return this._clamp(avg?.value);
211212
}
212213

213-
update(model: ITextModel, value: number) {
214+
update(model: ITextModel, value: number): number {
214215
const key = this._key(model);
215216
let avg = this._cache.get(key);
216217
if (!avg) {
217218
avg = new MovingAverage();
218219
this._cache.set(key, avg);
219220
}
220221
avg.update(value);
222+
return this.get(model);
221223
}
222224
}

src/vs/editor/contrib/codelens/codelensController.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ export class CodeLensContribution implements IEditorContribution {
160160
this._codeLensCache.put(model, result);
161161

162162
// update moving average
163-
this._getCodeLensModelDelays.update(model, Date.now() - t1);
163+
const newDelay = this._getCodeLensModelDelays.update(model, Date.now() - t1);
164+
scheduler.timeout = newDelay;
164165

165166
// render lenses
166167
this._renderCodeLensSymbols(result);
@@ -331,7 +332,7 @@ export class CodeLensContribution implements IEditorContribution {
331332
private _resolveCodeLensesInViewportSoon(): void {
332333
const model = this._editor.getModel();
333334
if (model) {
334-
this._resolveCodeLensesScheduler.schedule(this._resolveCodeLensesDelays.get(model));
335+
this._resolveCodeLensesScheduler.schedule();
335336
}
336337
}
337338

@@ -391,7 +392,8 @@ export class CodeLensContribution implements IEditorContribution {
391392
this._resolveCodeLensesPromise.then(() => {
392393

393394
// update moving average
394-
this._resolveCodeLensesDelays.update(model, Date.now() - t1);
395+
const newDelay = this._resolveCodeLensesDelays.update(model, Date.now() - t1);
396+
this._resolveCodeLensesScheduler.timeout = newDelay;
395397

396398
if (this._currentCodeLensModel) { // update the cached state with new resolved items
397399
this._codeLensCache.put(model, this._currentCodeLensModel);

0 commit comments

Comments
 (0)