Allow editor contributions to mark themselves as instantiated on idle (#166065)

* Allow editor contributions to mark themselves as instantiated on idle

For #164171

Editor contributions are currently created eagerly when editors are instantiated. In many cases however, the contribution is not needed instantly. A contributed widget for instance may only be shown after a user action, while some info that shows in the editor also does not need to be shown instantly as soon as the editor opens

Contributions like those can be lazily created, but actually adopting lazy instantiation for individual contributions is a lot of work

Instead this PR lets editor contributions tell VS Code that the entire contribution does not need to be eagerly created and can be instead on idle

With `EditorContributionInstantiation.Idle`, the contribution will be created:

- Either when the editor is idle
- Or when the contribution is explicitly requested.

Idle contributions cannot participate in saving and restoring of editor view states

This change will make it easier to lazily create editor contributions and also lets us remove existing code that was added to make individual editor contributions lazy

* Mark as optional

* Addressing comments

* IdleValue should throw again if there's ctor error
This commit is contained in:
Matt Bierner
2022-11-16 01:07:24 -08:00
committed by GitHub
parent 3dd0aea2ee
commit e4b6657f93
7 changed files with 85 additions and 57 deletions

View File

@@ -30,9 +30,24 @@ export type ServicesAccessor = InstantiationServicesAccessor;
export type IEditorContributionCtor = IConstructorSignature<IEditorContribution, [ICodeEditor]>;
export type IDiffEditorContributionCtor = IConstructorSignature<IDiffEditorContribution, [IDiffEditor]>;
export enum EditorContributionInstantiation {
/**
* The contribution is created eagerly when the {@linkcode ICodeEditor} is instantiated.
*/
Eager,
/**
* The contribution is created on idle (or when explicitly requested).
*
* Idle contributions cannot participate in saving or restoring of view states.
*/
Idle,
}
export interface IEditorContributionDescription {
id: string;
ctor: IEditorContributionCtor;
readonly id: string;
readonly ctor: IEditorContributionCtor;
readonly instantiation: EditorContributionInstantiation;
}
export interface IDiffEditorContributionDescription {
@@ -481,8 +496,8 @@ export function registerInstantiatedEditorAction(editorAction: EditorAction): vo
EditorContributionRegistry.INSTANCE.registerEditorAction(editorAction);
}
export function registerEditorContribution<Services extends BrandedService[]>(id: string, ctor: { new(editor: ICodeEditor, ...services: Services): IEditorContribution }): void {
EditorContributionRegistry.INSTANCE.registerEditorContribution(id, ctor);
export function registerEditorContribution<Services extends BrandedService[]>(id: string, ctor: { new(editor: ICodeEditor, ...services: Services): IEditorContribution }, instantiation = EditorContributionInstantiation.Eager): void {
EditorContributionRegistry.INSTANCE.registerEditorContribution(id, ctor, instantiation);
}
export function registerDiffEditorContribution<Services extends BrandedService[]>(id: string, ctor: { new(editor: IDiffEditor, ...services: Services): IEditorContribution }): void {
@@ -533,8 +548,8 @@ class EditorContributionRegistry {
this.editorCommands = Object.create(null);
}
public registerEditorContribution<Services extends BrandedService[]>(id: string, ctor: { new(editor: ICodeEditor, ...services: Services): IEditorContribution }): void {
this.editorContributions.push({ id, ctor: ctor as IEditorContributionCtor });
public registerEditorContribution<Services extends BrandedService[]>(id: string, ctor: { new(editor: ICodeEditor, ...services: Services): IEditorContribution }, instantiation = EditorContributionInstantiation.Eager): void {
this.editorContributions.push({ id, ctor: ctor as IEditorContributionCtor, instantiation });
}
public getEditorContributions(): IEditorContributionDescription[] {