From 118e3517522060ec45ccbba606ec908cffd175a3 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 5 Jul 2019 13:04:45 +0200 Subject: [PATCH] debt - use DisposableStore in more places --- .../goToDefinition/goToDefinitionMouse.ts | 15 +++--- .../contrib/snippet/snippetController2.ts | 15 +++--- .../suggest/suggestCommitCharacters.ts | 14 +++--- .../contrib/suggest/suggestController.ts | 46 ++++++++----------- 4 files changed, 41 insertions(+), 49 deletions(-) diff --git a/src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts b/src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts index 5c81fdeae57..717c7ba4d13 100644 --- a/src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts +++ b/src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts @@ -16,7 +16,7 @@ import { DefinitionProviderRegistry, LocationLink } from 'vs/editor/common/modes import { ICodeEditor, IMouseTarget, MouseTargetType } from 'vs/editor/browser/editorBrowser'; import { registerEditorContribution } from 'vs/editor/browser/editorExtensions'; import { getDefinitionsAtPosition } from './goToDefinition'; -import { IDisposable, dispose } from 'vs/base/common/lifecycle'; +import { DisposableStore } from 'vs/base/common/lifecycle'; import { ITextModelService } from 'vs/editor/common/services/resolverService'; import { registerThemingParticipant } from 'vs/platform/theme/common/themeService'; import { editorActiveLinkForeground } from 'vs/platform/theme/common/colorRegistry'; @@ -33,7 +33,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC static MAX_SOURCE_PREVIEW_LINES = 8; private readonly editor: ICodeEditor; - private toUnhook: IDisposable[]; + private readonly toUnhook = new DisposableStore(); private decorations: string[]; private currentWordUnderMouse: IWordAtPosition | null; private previousPromise: CancelablePromise | null; @@ -43,19 +43,18 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC @ITextModelService private readonly textModelResolverService: ITextModelService, @IModeService private readonly modeService: IModeService ) { - this.toUnhook = []; this.decorations = []; this.editor = editor; this.previousPromise = null; let linkGesture = new ClickLinkGesture(editor); - this.toUnhook.push(linkGesture); + this.toUnhook.add(linkGesture); - this.toUnhook.push(linkGesture.onMouseMoveOrRelevantKeyDown(([mouseEvent, keyboardEvent]) => { + this.toUnhook.add(linkGesture.onMouseMoveOrRelevantKeyDown(([mouseEvent, keyboardEvent]) => { this.startFindDefinition(mouseEvent, withNullAsUndefined(keyboardEvent)); })); - this.toUnhook.push(linkGesture.onExecute((mouseEvent: ClickLinkMouseEvent) => { + this.toUnhook.add(linkGesture.onExecute((mouseEvent: ClickLinkMouseEvent) => { if (this.isEnabled(mouseEvent)) { this.gotoDefinition(mouseEvent.target, mouseEvent.hasSideBySideModifier).then(() => { this.removeDecorations(); @@ -66,7 +65,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC } })); - this.toUnhook.push(linkGesture.onCancel(() => { + this.toUnhook.add(linkGesture.onCancel(() => { this.removeDecorations(); this.currentWordUnderMouse = null; })); @@ -303,7 +302,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC } public dispose(): void { - this.toUnhook = dispose(this.toUnhook); + this.toUnhook.dispose(); } } diff --git a/src/vs/editor/contrib/snippet/snippetController2.ts b/src/vs/editor/contrib/snippet/snippetController2.ts index daccfa9eec8..214b0f390eb 100644 --- a/src/vs/editor/contrib/snippet/snippetController2.ts +++ b/src/vs/editor/contrib/snippet/snippetController2.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; -import { dispose, IDisposable } from 'vs/base/common/lifecycle'; +import { dispose, DisposableStore } from 'vs/base/common/lifecycle'; import { repeat } from 'vs/base/common/strings'; import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; import { EditorCommand, registerEditorCommand, registerEditorContribution } from 'vs/editor/browser/editorExtensions'; @@ -35,7 +35,7 @@ export class SnippetController2 implements IEditorContribution { private readonly _hasPrevTabstop: IContextKey; private _session?: SnippetSession; - private _snippetListener: IDisposable[] = []; + private _snippetListener = new DisposableStore(); private _modelVersionId: number; private _currentChoice?: Choice; @@ -54,6 +54,7 @@ export class SnippetController2 implements IEditorContribution { this._hasPrevTabstop.reset(); this._hasNextTabstop.reset(); dispose(this._session); + this._snippetListener.dispose(); } getId(): string { @@ -93,7 +94,7 @@ export class SnippetController2 implements IEditorContribution { // don't listen while inserting the snippet // as that is the inflight state causing cancelation - this._snippetListener = dispose(this._snippetListener); + this._snippetListener.clear(); if (undoStopBefore) { this._editor.getModel().pushStackElement(); @@ -113,11 +114,9 @@ export class SnippetController2 implements IEditorContribution { this._updateState(); - this._snippetListener = [ - this._editor.onDidChangeModelContent(e => e.isFlush && this.cancel()), - this._editor.onDidChangeModel(() => this.cancel()), - this._editor.onDidChangeCursorSelection(() => this._updateState()) - ]; + this._snippetListener.add(this._editor.onDidChangeModelContent(e => e.isFlush && this.cancel())); + this._snippetListener.add(this._editor.onDidChangeModel(() => this.cancel())); + this._snippetListener.add(this._editor.onDidChangeCursorSelection(() => this._updateState())); } private _updateState(): void { diff --git a/src/vs/editor/contrib/suggest/suggestCommitCharacters.ts b/src/vs/editor/contrib/suggest/suggestCommitCharacters.ts index ba0789bf893..8a6fca72612 100644 --- a/src/vs/editor/contrib/suggest/suggestCommitCharacters.ts +++ b/src/vs/editor/contrib/suggest/suggestCommitCharacters.ts @@ -4,14 +4,14 @@ *--------------------------------------------------------------------------------------------*/ import { isNonEmptyArray } from 'vs/base/common/arrays'; -import { dispose, IDisposable } from 'vs/base/common/lifecycle'; +import { DisposableStore } from 'vs/base/common/lifecycle'; import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; import { ISelectedSuggestion, SuggestWidget } from './suggestWidget'; import { CharacterSet } from 'vs/editor/common/core/characterClassifier'; export class CommitCharacterController { - private _disposables: IDisposable[] = []; + private readonly _disposables = new DisposableStore(); private _active?: { readonly acceptCharacters: CharacterSet; @@ -20,11 +20,11 @@ export class CommitCharacterController { constructor(editor: ICodeEditor, widget: SuggestWidget, accept: (selected: ISelectedSuggestion) => any) { - this._disposables.push(widget.onDidShow(() => this._onItem(widget.getFocusedItem()))); - this._disposables.push(widget.onDidFocus(this._onItem, this)); - this._disposables.push(widget.onDidHide(this.reset, this)); + this._disposables.add(widget.onDidShow(() => this._onItem(widget.getFocusedItem()))); + this._disposables.add(widget.onDidFocus(this._onItem, this)); + this._disposables.add(widget.onDidHide(this.reset, this)); - this._disposables.push(editor.onWillType(text => { + this._disposables.add(editor.onWillType(text => { if (this._active) { const ch = text.charCodeAt(text.length - 1); if (this._active.acceptCharacters.has(ch) && editor.getConfiguration().contribInfo.acceptSuggestionOnCommitCharacter) { @@ -54,6 +54,6 @@ export class CommitCharacterController { } dispose() { - dispose(this._disposables); + this._disposables.dispose(); } } diff --git a/src/vs/editor/contrib/suggest/suggestController.ts b/src/vs/editor/contrib/suggest/suggestController.ts index 0e56a54c170..32c77163d8b 100644 --- a/src/vs/editor/contrib/suggest/suggestController.ts +++ b/src/vs/editor/contrib/suggest/suggestController.ts @@ -7,7 +7,7 @@ import { alert } from 'vs/base/browser/ui/aria/aria'; import { isNonEmptyArray } from 'vs/base/common/arrays'; import { onUnexpectedError } from 'vs/base/common/errors'; import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; -import { dispose, IDisposable } from 'vs/base/common/lifecycle'; +import { dispose, IDisposable, DisposableStore, toDisposable } from 'vs/base/common/lifecycle'; import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; import { EditorAction, EditorCommand, registerEditorAction, registerEditorCommand, registerEditorContribution, ServicesAccessor } from 'vs/editor/browser/editorExtensions'; import { EditOperation } from 'vs/editor/common/core/editOperation'; @@ -45,7 +45,7 @@ export class SuggestController implements IEditorContribution { private readonly _model: SuggestModel; private readonly _widget: IdleValue; private readonly _alternatives: IdleValue; - private _toDispose: IDisposable[] = []; + private readonly _toDispose = new DisposableStore(); private readonly _sticky = false; // for development purposes only @@ -63,23 +63,21 @@ export class SuggestController implements IEditorContribution { const widget = this._instantiationService.createInstance(SuggestWidget, this._editor); - this._toDispose.push(widget); - this._toDispose.push(widget.onDidSelect(item => this._insertSuggestion(item, false, true), this)); + this._toDispose.add(widget); + this._toDispose.add(widget.onDidSelect(item => this._insertSuggestion(item, false, true), this)); // Wire up logic to accept a suggestion on certain characters const commitCharacterController = new CommitCharacterController(this._editor, widget, item => this._insertSuggestion(item, false, true)); - this._toDispose.push( - commitCharacterController, - this._model.onDidSuggest(e => { - if (e.completionModel.items.length === 0) { - commitCharacterController.reset(); - } - }) - ); + this._toDispose.add(commitCharacterController); + this._toDispose.add(this._model.onDidSuggest(e => { + if (e.completionModel.items.length === 0) { + commitCharacterController.reset(); + } + })); // Wire up makes text edit context key let makesTextEdit = SuggestContext.MakesTextEdit.bindTo(this._contextKeyService); - this._toDispose.push(widget.onDidFocus(({ item }) => { + this._toDispose.add(widget.onDidFocus(({ item }) => { const position = this._editor.getPosition()!; const startColumn = item.completion.range.startColumn; @@ -103,36 +101,32 @@ export class SuggestController implements IEditorContribution { } makesTextEdit.set(value); })); - this._toDispose.push({ - dispose() { makesTextEdit.reset(); } - }); + this._toDispose.add(toDisposable(() => makesTextEdit.reset())); return widget; }); this._alternatives = new IdleValue(() => { - let res = new SuggestAlternatives(this._editor, this._contextKeyService); - this._toDispose.push(res); - return res; + return this._toDispose.add(new SuggestAlternatives(this._editor, this._contextKeyService)); }); - this._toDispose.push(_instantiationService.createInstance(WordContextKey, _editor)); + this._toDispose.add(_instantiationService.createInstance(WordContextKey, _editor)); - this._toDispose.push(this._model.onDidTrigger(e => { + this._toDispose.add(this._model.onDidTrigger(e => { this._widget.getValue().showTriggered(e.auto, e.shy ? 250 : 50); })); - this._toDispose.push(this._model.onDidSuggest(e => { + this._toDispose.add(this._model.onDidSuggest(e => { if (!e.shy) { let index = this._memoryService.select(this._editor.getModel()!, this._editor.getPosition()!, e.completionModel.items); this._widget.getValue().showSuggestions(e.completionModel, index, e.isFrozen, e.auto); } })); - this._toDispose.push(this._model.onDidCancel(e => { + this._toDispose.add(this._model.onDidCancel(e => { if (this._widget && !e.retrigger) { this._widget.getValue().hideWidget(); } })); - this._toDispose.push(this._editor.onDidBlurEditorWidget(() => { + this._toDispose.add(this._editor.onDidBlurEditorWidget(() => { if (!this._sticky) { this._model.cancel(); this._model.clear(); @@ -145,7 +139,7 @@ export class SuggestController implements IEditorContribution { const { acceptSuggestionOnEnter } = this._editor.getConfiguration().contribInfo; acceptSuggestionsOnEnter.set(acceptSuggestionOnEnter === 'on' || acceptSuggestionOnEnter === 'smart'); }; - this._toDispose.push(this._editor.onDidChangeConfiguration((e) => updateFromConfig())); + this._toDispose.add(this._editor.onDidChangeConfiguration((e) => updateFromConfig())); updateFromConfig(); } @@ -155,7 +149,7 @@ export class SuggestController implements IEditorContribution { } dispose(): void { - this._toDispose = dispose(this._toDispose); + this._toDispose.dispose(); this._widget.dispose(); if (this._model) { this._model.dispose(); -- GitLab