From d1fdc09e8a7df5f56f04fffe5c77c4205c14940c Mon Sep 17 00:00:00 2001 From: Johannes Date: Thu, 16 May 2024 11:41:44 +0200 Subject: [PATCH] Add docs and register some child insta services for disposal https://github.com/microsoft/vscode/issues/212879 --- src/vs/editor/browser/widget/codeEditor/codeEditorWidget.ts | 2 +- src/vs/editor/browser/widget/diffEditor/diffEditorWidget.ts | 4 ++-- .../widget/multiDiffEditor/multiDiffEditorWidgetImpl.ts | 4 ++-- src/vs/platform/instantiation/common/instantiation.ts | 6 +++++- .../platform/instantiation/common/instantiationService.ts | 6 ++++-- src/vs/workbench/browser/parts/compositePart.ts | 1 + src/vs/workbench/contrib/chat/browser/chatWidget.ts | 2 +- .../contrib/inlineChat/browser/inlineChatContentWidget.ts | 3 ++- .../contrib/inlineChat/browser/inlineChatWidget.ts | 3 ++- 9 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/vs/editor/browser/widget/codeEditor/codeEditorWidget.ts b/src/vs/editor/browser/widget/codeEditor/codeEditorWidget.ts index 69bf235e7cbeb..07753688f7ed9 100644 --- a/src/vs/editor/browser/widget/codeEditor/codeEditorWidget.ts +++ b/src/vs/editor/browser/widget/codeEditor/codeEditorWidget.ts @@ -289,7 +289,7 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE this._register(new EditorContextKeysManager(this, this._contextKeyService)); this._register(new EditorModeContext(this, this._contextKeyService, languageFeaturesService)); - this._instantiationService = instantiationService.createChild(new ServiceCollection([IContextKeyService, this._contextKeyService])); + this._instantiationService = this._register(instantiationService.createChild(new ServiceCollection([IContextKeyService, this._contextKeyService]))); this._modelData = null; diff --git a/src/vs/editor/browser/widget/diffEditor/diffEditorWidget.ts b/src/vs/editor/browser/widget/diffEditor/diffEditorWidget.ts index 5fdc32e47ee60..cd5c2e8b60621 100644 --- a/src/vs/editor/browser/widget/diffEditor/diffEditorWidget.ts +++ b/src/vs/editor/browser/widget/diffEditor/diffEditorWidget.ts @@ -68,9 +68,9 @@ export class DiffEditorWidget extends DelegatingEditor implements IDiffEditor { public get onDidContentSizeChange() { return this._editors.onDidContentSizeChange; } private readonly _contextKeyService = this._register(this._parentContextKeyService.createScoped(this._domElement)); - private readonly _instantiationService = this._parentInstantiationService.createChild( + private readonly _instantiationService = this._register(this._parentInstantiationService.createChild( new ServiceCollection([IContextKeyService, this._contextKeyService]) - ); + )); private readonly _rootSizeObserver: ObservableElementSizeObserver; diff --git a/src/vs/editor/browser/widget/multiDiffEditor/multiDiffEditorWidgetImpl.ts b/src/vs/editor/browser/widget/multiDiffEditor/multiDiffEditorWidgetImpl.ts index 82343308057c3..c29fc74bddd4e 100644 --- a/src/vs/editor/browser/widget/multiDiffEditor/multiDiffEditorWidgetImpl.ts +++ b/src/vs/editor/browser/widget/multiDiffEditor/multiDiffEditorWidgetImpl.ts @@ -114,9 +114,9 @@ export class MultiDiffEditorWidgetImpl extends Disposable { }); private readonly _contextKeyService = this._register(this._parentContextKeyService.createScoped(this._element)); - private readonly _instantiationService = this._parentInstantiationService.createChild( + private readonly _instantiationService = this._register(this._parentInstantiationService.createChild( new ServiceCollection([IContextKeyService, this._contextKeyService]) - ); + )); constructor( private readonly _element: HTMLElement, diff --git a/src/vs/platform/instantiation/common/instantiation.ts b/src/vs/platform/instantiation/common/instantiation.ts index 86766a4a6c6be..c1ce656527147 100644 --- a/src/vs/platform/instantiation/common/instantiation.ts +++ b/src/vs/platform/instantiation/common/instantiation.ts @@ -3,6 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +import { DisposableStore } from 'vs/base/common/lifecycle'; import * as descriptors from './descriptors'; import { ServiceCollection } from './serviceCollection'; @@ -61,8 +62,11 @@ export interface IInstantiationService { /** * Creates a child of this service which inherits all current services * and adds/overwrites the given services. + * + * NOTE that the returned child is `disposable` and should be disposed when not used + * anymore. This will also dispose all the services that this service has created. */ - createChild(services: ServiceCollection): IInstantiationService; + createChild(services: ServiceCollection, store?: DisposableStore): IInstantiationService; /** * Disposes this instantiation service. diff --git a/src/vs/platform/instantiation/common/instantiationService.ts b/src/vs/platform/instantiation/common/instantiationService.ts index fd95305865a51..5815924b3606a 100644 --- a/src/vs/platform/instantiation/common/instantiationService.ts +++ b/src/vs/platform/instantiation/common/instantiationService.ts @@ -6,7 +6,7 @@ import { GlobalIdleValue } from 'vs/base/common/async'; import { Event } from 'vs/base/common/event'; import { illegalState } from 'vs/base/common/errors'; -import { dispose, IDisposable, isDisposable, toDisposable } from 'vs/base/common/lifecycle'; +import { DisposableStore, dispose, IDisposable, isDisposable, toDisposable } from 'vs/base/common/lifecycle'; import { SyncDescriptor, SyncDescriptor0 } from 'vs/platform/instantiation/common/descriptors'; import { Graph } from 'vs/platform/instantiation/common/graph'; import { GetLeadingNonServiceArgs, IInstantiationService, ServiceIdentifier, ServicesAccessor, _util } from 'vs/platform/instantiation/common/instantiation'; @@ -70,7 +70,7 @@ export class InstantiationService implements IInstantiationService { } } - createChild(services: ServiceCollection): IInstantiationService { + createChild(services: ServiceCollection, store?: DisposableStore): IInstantiationService { this._throwIfDisposed(); const result = new class extends InstantiationService { @@ -80,6 +80,8 @@ export class InstantiationService implements IInstantiationService { } }(services, this._strict, this, this._enableTracing); this._children.add(result); + + store?.add(result); return result; } diff --git a/src/vs/workbench/browser/parts/compositePart.ts b/src/vs/workbench/browser/parts/compositePart.ts index 3ea632e64d5d1..19c56d9b5092c 100644 --- a/src/vs/workbench/browser/parts/compositePart.ts +++ b/src/vs/workbench/browser/parts/compositePart.ts @@ -199,6 +199,7 @@ export abstract class CompositePart extends Part { // Register to title area update events from the composite disposable.add(composite.onTitleAreaUpdate(() => this.onTitleAreaUpdate(composite.getId()), this)); + disposable.add(compositeInstantiationService); return composite; } diff --git a/src/vs/workbench/contrib/chat/browser/chatWidget.ts b/src/vs/workbench/contrib/chat/browser/chatWidget.ts index cdd2f1f8bee54..0024b27a413ad 100644 --- a/src/vs/workbench/contrib/chat/browser/chatWidget.ts +++ b/src/vs/workbench/contrib/chat/browser/chatWidget.ts @@ -411,7 +411,7 @@ export class ChatWidget extends Disposable implements IChatWidget { } private createList(listContainer: HTMLElement, options: IChatListItemRendererOptions): void { - const scopedInstantiationService = this.instantiationService.createChild(new ServiceCollection([IContextKeyService, this.contextKeyService])); + const scopedInstantiationService = this._register(this.instantiationService.createChild(new ServiceCollection([IContextKeyService, this.contextKeyService]))); const delegate = scopedInstantiationService.createInstance(ChatListDelegate, this.viewOptions.defaultElementHeight ?? 200); const rendererDelegate: IChatRendererDelegate = { getListLength: () => this.tree.getNode(null).visibleChildrenCount, diff --git a/src/vs/workbench/contrib/inlineChat/browser/inlineChatContentWidget.ts b/src/vs/workbench/contrib/inlineChat/browser/inlineChatContentWidget.ts index d5ac64ea3f1b1..8a64d210ece63 100644 --- a/src/vs/workbench/contrib/inlineChat/browser/inlineChatContentWidget.ts +++ b/src/vs/workbench/contrib/inlineChat/browser/inlineChatContentWidget.ts @@ -56,7 +56,8 @@ export class InlineChatContentWidget implements IContentWidget { new ServiceCollection([ IContextKeyService, this._store.add(contextKeyService.createScoped(this._domNode)) - ]) + ]), + this._store ); this._widget = scopedInstaService.createInstance( diff --git a/src/vs/workbench/contrib/inlineChat/browser/inlineChatWidget.ts b/src/vs/workbench/contrib/inlineChat/browser/inlineChatWidget.ts index aa8c32c6391f3..96bf08b444add 100644 --- a/src/vs/workbench/contrib/inlineChat/browser/inlineChatWidget.ts +++ b/src/vs/workbench/contrib/inlineChat/browser/inlineChatWidget.ts @@ -158,7 +158,8 @@ export class InlineChatWidget { new ServiceCollection([ IContextKeyService, this.scopedContextKeyService - ]) + ]), + this._store ); this._chatWidget = scopedInstaService.createInstance(