Skip to content

Commit

Permalink
Adopt ILanguageFeatureDebounceService (#140557)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Feb 4, 2022
1 parent dce13b6 commit 60e3e1a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
66 changes: 37 additions & 29 deletions src/vs/editor/contrib/links/browser/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
*--------------------------------------------------------------------------------------------*/

import * as async from 'vs/base/common/async';
import { RunOnceScheduler } from 'vs/base/common/async';
import { CancellationToken } from 'vs/base/common/cancellation';
import { onUnexpectedError } from 'vs/base/common/errors';
import { MarkdownString } from 'vs/base/common/htmlContent';
import { Disposable } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import * as platform from 'vs/base/common/platform';
import * as resources from 'vs/base/common/resources';
import { StopWatch } from 'vs/base/common/stopwatch';
import { URI } from 'vs/base/common/uri';
import 'vs/css!./links';
import { ICodeEditor, MouseTargetType } from 'vs/editor/browser/editorBrowser';
Expand All @@ -22,6 +24,7 @@ import { LanguageFeatureRegistry } from 'vs/editor/common/languageFeatureRegistr
import { LinkProvider } from 'vs/editor/common/languages';
import { IModelDecorationsChangeAccessor, IModelDeltaDecoration, TrackedRangeStickiness } from 'vs/editor/common/model';
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
import { IFeatureDebounceInformation, ILanguageFeatureDebounceService } from 'vs/editor/common/services/languageFeatureDebounce';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
import { ClickLinkGesture, ClickLinkKeyboardEvent, ClickLinkMouseEvent } from 'vs/editor/contrib/gotoSymbol/browser/link/clickLinkGesture';
import { getLinks, Link, LinksList } from 'vs/editor/contrib/links/browser/getLinks';
Expand All @@ -39,10 +42,9 @@ export class LinkDetector extends Disposable implements IEditorContribution {
return editor.getContribution<LinkDetector>(LinkDetector.ID);
}

static readonly RECOMPUTE_TIME = 1000; // ms

private readonly providers: LanguageFeatureRegistry<LinkProvider>;
private readonly timeout: async.TimeoutTimer;
private readonly debounceInformation: IFeatureDebounceInformation;
private readonly computeLinks: RunOnceScheduler;
private computePromise: async.CancelablePromise<LinksList> | null;
private activeLinksList: LinksList | null;
private activeLinkDecorationId: string | null;
Expand All @@ -53,11 +55,13 @@ export class LinkDetector extends Disposable implements IEditorContribution {
@IOpenerService private readonly openerService: IOpenerService,
@INotificationService private readonly notificationService: INotificationService,
@ILanguageFeaturesService private readonly languageFeaturesService: ILanguageFeaturesService,
@ILanguageFeatureDebounceService languageFeatureDebounceService: ILanguageFeatureDebounceService,
) {
super();

this.providers = this.languageFeaturesService.linkProvider;
this.timeout = new async.TimeoutTimer();
this.debounceInformation = languageFeatureDebounceService.for(this.providers, 'Links', { min: 1000, max: 4000 });
this.computeLinks = this._register(new RunOnceScheduler(() => this.computeLinksNow(), 1000));
this.computePromise = null;
this.activeLinksList = null;
this.currentOccurrences = {};
Expand Down Expand Up @@ -85,33 +89,33 @@ export class LinkDetector extends Disposable implements IEditorContribution {
this.stop();

// Start computing (for the getting enabled case)
this.beginCompute();
this.computeLinks.schedule(0);
}));
this._register(editor.onDidChangeModelContent((e) => {
if (!this.editor.hasModel()) {
return;
}
this.computeLinks.schedule(this.debounceInformation.get(this.editor.getModel()));
}));
this._register(editor.onDidChangeModel((e) => {
this.currentOccurrences = {};
this.activeLinkDecorationId = null;
this.stop();
this.computeLinks.schedule(0);
}));
this._register(editor.onDidChangeModelLanguage((e) => {
this.stop();
this.computeLinks.schedule(0);
}));
this._register(this.providers.onDidChange((e) => {
this.stop();
this.computeLinks.schedule(0);
}));
this._register(editor.onDidChangeModelContent((e) => this.onChange()));
this._register(editor.onDidChangeModel((e) => this.onModelChanged()));
this._register(editor.onDidChangeModelLanguage((e) => this.onModelLanguageChanged()));
this._register(this.providers.onDidChange((e) => this.onModelLanguageChanged()));

this.beginCompute();
}

private onModelChanged(): void {
this.currentOccurrences = {};
this.activeLinkDecorationId = null;
this.stop();
this.beginCompute();
}

private onModelLanguageChanged(): void {
this.stop();
this.beginCompute();
}

private onChange(): void {
this.timeout.setIfNotSet(() => this.beginCompute(), LinkDetector.RECOMPUTE_TIME);
this.computeLinks.schedule(0);
}

private async beginCompute(): Promise<void> {
private async computeLinksNow(): Promise<void> {
if (!this.editor.hasModel() || !this.editor.getOption(EditorOption.links)) {
return;
}
Expand All @@ -129,7 +133,12 @@ export class LinkDetector extends Disposable implements IEditorContribution {

this.computePromise = async.createCancelablePromise(token => getLinks(this.providers, model, token));
try {
const sw = new StopWatch(false);
this.activeLinksList = await this.computePromise;
this.debounceInformation.update(model, sw.elapsed());
if (model.isDisposed()) {
return;
}
this.updateDecorations(this.activeLinksList.links);
} catch (err) {
onUnexpectedError(err);
Expand Down Expand Up @@ -283,7 +292,7 @@ export class LinkDetector extends Disposable implements IEditorContribution {
}

private stop(): void {
this.timeout.cancel();
this.computeLinks.cancel();
if (this.activeLinksList) {
this.activeLinksList?.dispose();
this.activeLinksList = null;
Expand All @@ -297,7 +306,6 @@ export class LinkDetector extends Disposable implements IEditorContribution {
public override dispose(): void {
super.dispose();
this.stop();
this.timeout.dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ViewportSemanticTokensContribution extends Disposable implements IEditorCo
this._editor = editor;
this._provider = languageFeaturesService.documentRangeSemanticTokensProvider;
this._debounceInformation = languageFeatureDebounceService.for(this._provider, 'DocumentRangeSemanticTokens', { min: 100, max: 500 });
this._tokenizeViewport = new RunOnceScheduler(() => this._tokenizeViewportNow(), 100);
this._tokenizeViewport = this._register(new RunOnceScheduler(() => this._tokenizeViewportNow(), 100));
this._outstandingRequests = [];
const scheduleTokenizeViewport = () => {
if (this._editor.hasModel()) {
Expand Down

0 comments on commit 60e3e1a

Please sign in to comment.