Skip to content

Commit

Permalink
share inlay hints cache between editors, #16221 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Jan 20, 2022
1 parent 12f9c5f commit 3fb542d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/vs/editor/contrib/inlayHints/inlayHintsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ import { ClickLinkGesture, ClickLinkMouseEvent } from 'vs/editor/contrib/gotoSym
import { InlayHintAnchor, InlayHintItem, InlayHintsFragments } from 'vs/editor/contrib/inlayHints/inlayHints';
import { goToDefinitionWithLocation, showGoToContextMenu } from 'vs/editor/contrib/inlayHints/inlayHintsLocations';
import { CommandsRegistry, ICommandService } from 'vs/platform/commands/common/commands';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { createDecorator, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { INotificationService } from 'vs/platform/notification/common/notification';
import * as colors from 'vs/platform/theme/common/colorRegistry';
import { themeColorFromId } from 'vs/platform/theme/common/themeService';

const MAX_DECORATORS = 1500;
// --- hint caching service (per session)

class InlayHintsCache {

declare readonly _serviceBrand: undefined;

private readonly _entries = new LRUCache<string, InlayHintItem[]>(50);

get(model: ITextModel): InlayHintItem[] | undefined {
Expand All @@ -52,6 +55,12 @@ class InlayHintsCache {
}
}

interface IInlayHintsCache extends InlayHintsCache { }
const IInlayHintsCache = createDecorator<IInlayHintsCache>('IInlayHintsCache');
registerSingleton(IInlayHintsCache, InlayHintsCache, true);

// --- rendered label

export class RenderedInlayHintLabelPart {
constructor(readonly item: InlayHintItem, readonly index: number) { }

Expand All @@ -65,18 +74,21 @@ export class RenderedInlayHintLabelPart {
}
}

// --- controller

export class InlayHintsController implements IEditorContribution {

static readonly ID: string = 'editor.contrib.InlayHints';

private static readonly _MAX_DECORATORS = 1500;

static get(editor: ICodeEditor) {
return editor.getContribution(InlayHintsController.ID) ?? undefined;
}

private readonly _disposables = new DisposableStore();
private readonly _sessionDisposables = new DisposableStore();
private readonly _debounceInfo: IFeatureDebounceInformation;
private readonly _cache = new InlayHintsCache();
private readonly _decorationsMetadata = new Map<string, { item: InlayHintItem, classNameRef: IDisposable; }>();
private readonly _ruleFactory = new DynamicCssRules(this._editor);

Expand All @@ -85,6 +97,7 @@ export class InlayHintsController implements IEditorContribution {
constructor(
private readonly _editor: ICodeEditor,
@ILanguageFeatureDebounceService _featureDebounce: ILanguageFeatureDebounceService,
@IInlayHintsCache private readonly _inlayHintsCache: IInlayHintsCache,
@ICommandService private readonly _commandService: ICommandService,
@INotificationService private readonly _notificationService: INotificationService,
@IInstantiationService private readonly _instaService: IInstantiationService,
Expand Down Expand Up @@ -121,7 +134,7 @@ export class InlayHintsController implements IEditorContribution {
}

// iff possible, quickly update from cache
const cached = this._cache.get(model);
const cached = this._inlayHintsCache.get(model);
if (cached) {
this._updateHintsDecorators([model.getFullModelRange()], cached);
}
Expand Down Expand Up @@ -173,7 +186,7 @@ export class InlayHintsController implements IEditorContribution {
// update inline hints when content or scroll position changes
this._sessionDisposables.add(this._editor.onDidChangeModelContent(() => scheduler.schedule()));
this._sessionDisposables.add(this._editor.onDidScrollChange(() => scheduler.schedule()));
scheduler.schedule();
scheduler.schedule(0);

// mouse gestures
this._sessionDisposables.add(this._installLinkGesture());
Expand Down Expand Up @@ -278,7 +291,7 @@ export class InlayHintsController implements IEditorContribution {
}
items.set(obj.item, value);
}
this._cache.set(model, Array.from(items.values()));
this._inlayHintsCache.set(model, Array.from(items.values()));
}

private _getHintsRanges(): Range[] {
Expand Down Expand Up @@ -402,7 +415,7 @@ export class InlayHintsController implements IEditorContribution {
addInjectedWhitespace(item, true);
}

if (newDecorationsData.length > MAX_DECORATORS) {
if (newDecorationsData.length > InlayHintsController._MAX_DECORATORS) {
break;
}
}
Expand Down

0 comments on commit 3fb542d

Please sign in to comment.