From e6b698b4fd59f3f6f3713529a52e5d68d1155972 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 3 Jul 2019 18:14:55 -0700 Subject: [PATCH] Using DisposableStore in place of disposable arrays in a few more files --- src/vs/editor/contrib/gotoError/gotoError.ts | 23 ++++++------ src/vs/platform/list/browser/listService.ts | 36 +++++++++---------- .../browser/parts/editor/rangeDecorations.ts | 18 ++++------ .../markers/browser/markersTreeViewer.ts | 17 ++++----- .../contrib/search/common/searchModel.ts | 21 ++++++----- .../partsSplash.contribution.ts | 6 ++-- .../contrib/tasks/common/problemCollectors.ts | 7 ++-- 7 files changed, 59 insertions(+), 69 deletions(-) diff --git a/src/vs/editor/contrib/gotoError/gotoError.ts b/src/vs/editor/contrib/gotoError/gotoError.ts index 4e46bd7e2b1..8f1231cbfb8 100644 --- a/src/vs/editor/contrib/gotoError/gotoError.ts +++ b/src/vs/editor/contrib/gotoError/gotoError.ts @@ -6,7 +6,7 @@ import * as nls from 'vs/nls'; import { Emitter } from 'vs/base/common/event'; import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; -import { IDisposable, dispose } from 'vs/base/common/lifecycle'; +import { IDisposable, dispose, DisposableStore } from 'vs/base/common/lifecycle'; import { URI } from 'vs/base/common/uri'; import { RawContextKey, IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { IMarker, IMarkerService, MarkerSeverity } from 'vs/platform/markers/common/markers'; @@ -206,7 +206,7 @@ export class MarkerController implements editorCommon.IEditorContribution { private _model: MarkerModel | null = null; private _widget: MarkerNavigationWidget | null = null; private readonly _widgetVisible: IContextKey; - private _disposeOnClose: IDisposable[] = []; + private readonly _disposeOnClose = new DisposableStore(); constructor( editor: ICodeEditor, @@ -226,11 +226,12 @@ export class MarkerController implements editorCommon.IEditorContribution { public dispose(): void { this._cleanUp(); + this._disposeOnClose.dispose(); } private _cleanUp(): void { this._widgetVisible.reset(); - this._disposeOnClose = dispose(this._disposeOnClose); + this._disposeOnClose.clear(); this._widget = null; this._model = null; } @@ -255,19 +256,21 @@ export class MarkerController implements editorCommon.IEditorContribution { this._widgetVisible.set(true); this._widget.onDidClose(() => this._cleanUp(), this, this._disposeOnClose); - this._disposeOnClose.push(this._model); - this._disposeOnClose.push(this._widget); - this._disposeOnClose.push(...actions); - this._disposeOnClose.push(this._widget.onDidSelectRelatedInformation(related => { + this._disposeOnClose.add(this._model); + this._disposeOnClose.add(this._widget); + for (const action of actions) { + this._disposeOnClose.add(action); + } + this._disposeOnClose.add(this._widget.onDidSelectRelatedInformation(related => { this._editorService.openCodeEditor({ resource: related.resource, options: { pinned: true, revealIfOpened: true, selection: Range.lift(related).collapseToStart() } }, this._editor).then(undefined, onUnexpectedError); this.closeMarkersNavigation(false); })); - this._disposeOnClose.push(this._editor.onDidChangeModel(() => this._cleanUp())); + this._disposeOnClose.add(this._editor.onDidChangeModel(() => this._cleanUp())); - this._disposeOnClose.push(this._model.onCurrentMarkerChanged(marker => { + this._disposeOnClose.add(this._model.onCurrentMarkerChanged(marker => { if (!marker || !this._model) { this._cleanUp(); } else { @@ -279,7 +282,7 @@ export class MarkerController implements editorCommon.IEditorContribution { }); } })); - this._disposeOnClose.push(this._model.onMarkerSetChanged(() => { + this._disposeOnClose.add(this._model.onMarkerSetChanged(() => { if (!this._widget || !this._widget.position || !this._model) { return; } diff --git a/src/vs/platform/list/browser/listService.ts b/src/vs/platform/list/browser/listService.ts index 3847287678b..383cff04a94 100644 --- a/src/vs/platform/list/browser/listService.ts +++ b/src/vs/platform/list/browser/listService.ts @@ -268,7 +268,7 @@ export class WorkbenchList extends List { } ); - this.disposables.push(workbenchListOptionsDisposable); + this.disposables.add(workbenchListOptionsDisposable); this.contextKeyService = createScopedContextKeyService(contextKeyService, this); this.configurationService = configurationService; @@ -282,31 +282,29 @@ export class WorkbenchList extends List { this._useAltAsMultipleSelectionModifier = useAltAsMultipleSelectionModifier(configurationService); - this.disposables.push( - this.contextKeyService, - (listService as ListService).register(this), - attachListStyler(this, themeService), - this.onSelectionChange(() => { - const selection = this.getSelection(); - const focus = this.getFocus(); + this.disposables.add(this.contextKeyService); + this.disposables.add((listService as ListService).register(this)); + this.disposables.add(attachListStyler(this, themeService)); + this.disposables.add(this.onSelectionChange(() => { + const selection = this.getSelection(); + const focus = this.getFocus(); - this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0); - this.listMultiSelection.set(selection.length > 1); - this.listDoubleSelection.set(selection.length === 2); - }), - this.onFocusChange(() => { - const selection = this.getSelection(); - const focus = this.getFocus(); + this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0); + this.listMultiSelection.set(selection.length > 1); + this.listDoubleSelection.set(selection.length === 2); + })); + this.disposables.add(this.onFocusChange(() => { + const selection = this.getSelection(); + const focus = this.getFocus(); - this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0); - }) - ); + this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0); + })); this.registerListeners(); } private registerListeners(): void { - this.disposables.push(this.configurationService.onDidChangeConfiguration(e => { + this.disposables.add(this.configurationService.onDidChangeConfiguration(e => { if (e.affectsConfiguration(multiSelectModifierSettingKey)) { this._useAltAsMultipleSelectionModifier = useAltAsMultipleSelectionModifier(this.configurationService); } diff --git a/src/vs/workbench/browser/parts/editor/rangeDecorations.ts b/src/vs/workbench/browser/parts/editor/rangeDecorations.ts index b22a8290edd..0e994f72c34 100644 --- a/src/vs/workbench/browser/parts/editor/rangeDecorations.ts +++ b/src/vs/workbench/browser/parts/editor/rangeDecorations.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { IDisposable, Disposable } from 'vs/base/common/lifecycle'; +import { Disposable, DisposableStore } from 'vs/base/common/lifecycle'; import { URI } from 'vs/base/common/uri'; import { Event, Emitter } from 'vs/base/common/event'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; @@ -23,7 +23,7 @@ export class RangeHighlightDecorations extends Disposable { private rangeHighlightDecorationId: string | null = null; private editor: ICodeEditor | null = null; - private editorDisposables: IDisposable[] = []; + private readonly editorDisposables = this._register(new DisposableStore()); private readonly _onHighlightRemoved: Emitter = this._register(new Emitter()); get onHighlightRemoved(): Event { return this._onHighlightRemoved.event; } @@ -72,9 +72,9 @@ export class RangeHighlightDecorations extends Disposable { private setEditor(editor: ICodeEditor) { if (this.editor !== editor) { - this.disposeEditorListeners(); + this.editorDisposables.clear(); this.editor = editor; - this.editorDisposables.push(this.editor.onDidChangeCursorPosition((e: ICursorPositionChangedEvent) => { + this.editorDisposables.add(this.editor.onDidChangeCursorPosition((e: ICursorPositionChangedEvent) => { if ( e.reason === CursorChangeReason.NotSet || e.reason === CursorChangeReason.Explicit @@ -84,19 +84,14 @@ export class RangeHighlightDecorations extends Disposable { this.removeHighlightRange(); } })); - this.editorDisposables.push(this.editor.onDidChangeModel(() => { this.removeHighlightRange(); })); - this.editorDisposables.push(this.editor.onDidDispose(() => { + this.editorDisposables.add(this.editor.onDidChangeModel(() => { this.removeHighlightRange(); })); + this.editorDisposables.add(this.editor.onDidDispose(() => { this.removeHighlightRange(); this.editor = null; })); } } - private disposeEditorListeners() { - this.editorDisposables.forEach(disposable => disposable.dispose()); - this.editorDisposables = []; - } - private static readonly _WHOLE_LINE_RANGE_HIGHLIGHT = ModelDecorationOptions.register({ stickiness: TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges, className: 'rangeHighlight', @@ -117,7 +112,6 @@ export class RangeHighlightDecorations extends Disposable { if (this.editor && this.editor.getModel()) { this.removeHighlightRange(); - this.disposeEditorListeners(); this.editor = null; } } diff --git a/src/vs/workbench/contrib/markers/browser/markersTreeViewer.ts b/src/vs/workbench/contrib/markers/browser/markersTreeViewer.ts index dac61a035c0..ed087ccf8c0 100644 --- a/src/vs/workbench/contrib/markers/browser/markersTreeViewer.ts +++ b/src/vs/workbench/contrib/markers/browser/markersTreeViewer.ts @@ -15,7 +15,7 @@ import Messages from 'vs/workbench/contrib/markers/browser/messages'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { attachBadgeStyler } from 'vs/platform/theme/common/styler'; import { IThemeService } from 'vs/platform/theme/common/themeService'; -import { IDisposable, dispose, Disposable, toDisposable } from 'vs/base/common/lifecycle'; +import { IDisposable, dispose, Disposable, toDisposable, DisposableStore } from 'vs/base/common/lifecycle'; import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar'; import { QuickFixAction, QuickFixActionViewItem } from 'vs/workbench/contrib/markers/browser/markersPanelActions'; import { ILabelService } from 'vs/platform/label/common/label'; @@ -143,7 +143,7 @@ export type FilterData = ResourceMarkersFilterData | MarkerFilterData | RelatedI export class ResourceMarkersRenderer implements ITreeRenderer { private renderedNodes = new Map, IResourceMarkersTemplateData>(); - private disposables: IDisposable[] = []; + private readonly disposables = new DisposableStore(); constructor( private labels: ResourceLabels, @@ -207,7 +207,7 @@ export class ResourceMarkersRenderer implements ITreeRenderer this.disposables = dispose(this.disposables))); } render(element: Marker, filterData: MarkerFilterData | undefined): void { this.actionBar.clear(); this.multilineActionbar.clear(); - if (this.disposables.length) { - this.disposables = dispose(this.disposables); - } + this.disposables.clear(); dom.clearNode(this.messageAndDetailsContainer); this.icon.className = `marker-icon ${SeverityIcon.className(MarkerSeverity.toSeverity(element.marker.severity))}`; @@ -275,8 +272,8 @@ class MarkerWidget extends Disposable { this.renderMultilineActionbar(element); this.renderMessageAndDetails(element, filterData); - this.disposables.push(dom.addDisposableListener(this.parent, dom.EventType.MOUSE_OVER, () => this.markersViewModel.onMarkerMouseHover(element))); - this.disposables.push(dom.addDisposableListener(this.parent, dom.EventType.MOUSE_LEAVE, () => this.markersViewModel.onMarkerMouseLeave(element))); + this.disposables.add(dom.addDisposableListener(this.parent, dom.EventType.MOUSE_OVER, () => this.markersViewModel.onMarkerMouseHover(element))); + this.disposables.add(dom.addDisposableListener(this.parent, dom.EventType.MOUSE_LEAVE, () => this.markersViewModel.onMarkerMouseLeave(element))); } private renderQuickfixActionbar(marker: Marker): void { diff --git a/src/vs/workbench/contrib/search/common/searchModel.ts b/src/vs/workbench/contrib/search/common/searchModel.ts index b843faa1662..5f8ba902bd9 100644 --- a/src/vs/workbench/contrib/search/common/searchModel.ts +++ b/src/vs/workbench/contrib/search/common/searchModel.ts @@ -8,7 +8,7 @@ import { CancellationTokenSource } from 'vs/base/common/cancellation'; import * as errors from 'vs/base/common/errors'; import { Emitter, Event } from 'vs/base/common/event'; import { getBaseLabel } from 'vs/base/common/labels'; -import { Disposable, IDisposable } from 'vs/base/common/lifecycle'; +import { Disposable, IDisposable, DisposableStore } from 'vs/base/common/lifecycle'; import { ResourceMap, TernarySearchTree, values } from 'vs/base/common/map'; import * as objects from 'vs/base/common/objects'; import { lcut } from 'vs/base/common/strings'; @@ -1044,7 +1044,7 @@ export class RangeHighlightDecorations implements IDisposable { private _decorationId: string | null = null; private _model: ITextModel | null = null; - private _modelDisposables: IDisposable[] = []; + private readonly _modelDisposables = new DisposableStore(); constructor( @IModelService private readonly _modelService: IModelService @@ -1079,30 +1079,29 @@ export class RangeHighlightDecorations implements IDisposable { private setModel(model: ITextModel) { if (this._model !== model) { - this.disposeModelListeners(); + this.clearModelListeners(); this._model = model; - this._modelDisposables.push(this._model.onDidChangeDecorations((e) => { - this.disposeModelListeners(); + this._modelDisposables.add(this._model.onDidChangeDecorations((e) => { + this.clearModelListeners(); this.removeHighlightRange(); this._model = null; })); - this._modelDisposables.push(this._model.onWillDispose(() => { - this.disposeModelListeners(); + this._modelDisposables.add(this._model.onWillDispose(() => { + this.clearModelListeners(); this.removeHighlightRange(); this._model = null; })); } } - private disposeModelListeners() { - this._modelDisposables.forEach(disposable => disposable.dispose()); - this._modelDisposables = []; + private clearModelListeners() { + this._modelDisposables.clear(); } dispose() { if (this._model) { this.removeHighlightRange(); - this.disposeModelListeners(); + this._modelDisposables.dispose(); this._model = null; } } diff --git a/src/vs/workbench/contrib/splash/electron-browser/partsSplash.contribution.ts b/src/vs/workbench/contrib/splash/electron-browser/partsSplash.contribution.ts index 8fc47ec3f77..31de07b29b1 100644 --- a/src/vs/workbench/contrib/splash/electron-browser/partsSplash.contribution.ts +++ b/src/vs/workbench/contrib/splash/electron-browser/partsSplash.contribution.ts @@ -9,7 +9,7 @@ import { onDidChangeFullscreen, isFullscreen } from 'vs/base/browser/browser'; import { getTotalHeight, getTotalWidth } from 'vs/base/browser/dom'; import { Color } from 'vs/base/common/color'; import { Event } from 'vs/base/common/event'; -import { dispose, IDisposable } from 'vs/base/common/lifecycle'; +import { DisposableStore } from 'vs/base/common/lifecycle'; import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle'; import { Registry } from 'vs/platform/registry/common/platform'; import { ColorIdentifier, editorBackground, foreground } from 'vs/platform/theme/common/colorRegistry'; @@ -30,7 +30,7 @@ class PartsSplash { private static readonly _splashElementId = 'monaco-parts-splash'; - private readonly _disposables: IDisposable[] = []; + private readonly _disposables = new DisposableStore(); private _didChangeTitleBarStyle: boolean; private _lastBaseTheme: string; @@ -64,7 +64,7 @@ class PartsSplash { } dispose(): void { - dispose(this._disposables); + this._disposables.dispose(); } private _savePartsSplash() { diff --git a/src/vs/workbench/contrib/tasks/common/problemCollectors.ts b/src/vs/workbench/contrib/tasks/common/problemCollectors.ts index b4d430db6c9..eac610b4d4a 100644 --- a/src/vs/workbench/contrib/tasks/common/problemCollectors.ts +++ b/src/vs/workbench/contrib/tasks/common/problemCollectors.ts @@ -6,7 +6,7 @@ import { IStringDictionary, INumberDictionary } from 'vs/base/common/collections'; import { URI } from 'vs/base/common/uri'; import { Event, Emitter } from 'vs/base/common/event'; -import { IDisposable } from 'vs/base/common/lifecycle'; +import { IDisposable, DisposableStore } from 'vs/base/common/lifecycle'; import { IModelService } from 'vs/editor/common/services/modelService'; @@ -43,7 +43,7 @@ export abstract class AbstractProblemCollector implements IDisposable { private buffer: string[]; private bufferLength: number; private openModels: IStringDictionary; - private modelListeners: IDisposable[]; + private readonly modelListeners = new DisposableStore(); private tail: Promise; // [owner] -> ApplyToKind @@ -77,7 +77,6 @@ export abstract class AbstractProblemCollector implements IDisposable { this._numberOfMatches = 0; this._maxMarkerSeverity = undefined; this.openModels = Object.create(null); - this.modelListeners = []; this.applyToByOwner = new Map(); for (let problemMatcher of problemMatchers) { let current = this.applyToByOwner.get(problemMatcher.owner); @@ -119,7 +118,7 @@ export abstract class AbstractProblemCollector implements IDisposable { protected abstract async processLineInternal(line: string): Promise; public dispose() { - this.modelListeners.forEach(disposable => disposable.dispose()); + this.modelListeners.dispose(); } public get numberOfMatches(): number {