Skip to content

Commit 118e351

Browse files
committed
debt - use DisposableStore in more places
1 parent 6b519e5 commit 118e351

4 files changed

Lines changed: 41 additions & 49 deletions

File tree

src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { DefinitionProviderRegistry, LocationLink } from 'vs/editor/common/modes
1616
import { ICodeEditor, IMouseTarget, MouseTargetType } from 'vs/editor/browser/editorBrowser';
1717
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
1818
import { getDefinitionsAtPosition } from './goToDefinition';
19-
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
19+
import { DisposableStore } from 'vs/base/common/lifecycle';
2020
import { ITextModelService } from 'vs/editor/common/services/resolverService';
2121
import { registerThemingParticipant } from 'vs/platform/theme/common/themeService';
2222
import { editorActiveLinkForeground } from 'vs/platform/theme/common/colorRegistry';
@@ -33,7 +33,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
3333
static MAX_SOURCE_PREVIEW_LINES = 8;
3434

3535
private readonly editor: ICodeEditor;
36-
private toUnhook: IDisposable[];
36+
private readonly toUnhook = new DisposableStore();
3737
private decorations: string[];
3838
private currentWordUnderMouse: IWordAtPosition | null;
3939
private previousPromise: CancelablePromise<LocationLink[] | null> | null;
@@ -43,19 +43,18 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
4343
@ITextModelService private readonly textModelResolverService: ITextModelService,
4444
@IModeService private readonly modeService: IModeService
4545
) {
46-
this.toUnhook = [];
4746
this.decorations = [];
4847
this.editor = editor;
4948
this.previousPromise = null;
5049

5150
let linkGesture = new ClickLinkGesture(editor);
52-
this.toUnhook.push(linkGesture);
51+
this.toUnhook.add(linkGesture);
5352

54-
this.toUnhook.push(linkGesture.onMouseMoveOrRelevantKeyDown(([mouseEvent, keyboardEvent]) => {
53+
this.toUnhook.add(linkGesture.onMouseMoveOrRelevantKeyDown(([mouseEvent, keyboardEvent]) => {
5554
this.startFindDefinition(mouseEvent, withNullAsUndefined(keyboardEvent));
5655
}));
5756

58-
this.toUnhook.push(linkGesture.onExecute((mouseEvent: ClickLinkMouseEvent) => {
57+
this.toUnhook.add(linkGesture.onExecute((mouseEvent: ClickLinkMouseEvent) => {
5958
if (this.isEnabled(mouseEvent)) {
6059
this.gotoDefinition(mouseEvent.target, mouseEvent.hasSideBySideModifier).then(() => {
6160
this.removeDecorations();
@@ -66,7 +65,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
6665
}
6766
}));
6867

69-
this.toUnhook.push(linkGesture.onCancel(() => {
68+
this.toUnhook.add(linkGesture.onCancel(() => {
7069
this.removeDecorations();
7170
this.currentWordUnderMouse = null;
7271
}));
@@ -303,7 +302,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
303302
}
304303

305304
public dispose(): void {
306-
this.toUnhook = dispose(this.toUnhook);
305+
this.toUnhook.dispose();
307306
}
308307
}
309308

src/vs/editor/contrib/snippet/snippetController2.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
7-
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
7+
import { dispose, DisposableStore } from 'vs/base/common/lifecycle';
88
import { repeat } from 'vs/base/common/strings';
99
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
1010
import { EditorCommand, registerEditorCommand, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
@@ -35,7 +35,7 @@ export class SnippetController2 implements IEditorContribution {
3535
private readonly _hasPrevTabstop: IContextKey<boolean>;
3636

3737
private _session?: SnippetSession;
38-
private _snippetListener: IDisposable[] = [];
38+
private _snippetListener = new DisposableStore();
3939
private _modelVersionId: number;
4040
private _currentChoice?: Choice;
4141

@@ -54,6 +54,7 @@ export class SnippetController2 implements IEditorContribution {
5454
this._hasPrevTabstop.reset();
5555
this._hasNextTabstop.reset();
5656
dispose(this._session);
57+
this._snippetListener.dispose();
5758
}
5859

5960
getId(): string {
@@ -93,7 +94,7 @@ export class SnippetController2 implements IEditorContribution {
9394

9495
// don't listen while inserting the snippet
9596
// as that is the inflight state causing cancelation
96-
this._snippetListener = dispose(this._snippetListener);
97+
this._snippetListener.clear();
9798

9899
if (undoStopBefore) {
99100
this._editor.getModel().pushStackElement();
@@ -113,11 +114,9 @@ export class SnippetController2 implements IEditorContribution {
113114

114115
this._updateState();
115116

116-
this._snippetListener = [
117-
this._editor.onDidChangeModelContent(e => e.isFlush && this.cancel()),
118-
this._editor.onDidChangeModel(() => this.cancel()),
119-
this._editor.onDidChangeCursorSelection(() => this._updateState())
120-
];
117+
this._snippetListener.add(this._editor.onDidChangeModelContent(e => e.isFlush && this.cancel()));
118+
this._snippetListener.add(this._editor.onDidChangeModel(() => this.cancel()));
119+
this._snippetListener.add(this._editor.onDidChangeCursorSelection(() => this._updateState()));
121120
}
122121

123122
private _updateState(): void {

src/vs/editor/contrib/suggest/suggestCommitCharacters.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { isNonEmptyArray } from 'vs/base/common/arrays';
7-
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
7+
import { DisposableStore } from 'vs/base/common/lifecycle';
88
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
99
import { ISelectedSuggestion, SuggestWidget } from './suggestWidget';
1010
import { CharacterSet } from 'vs/editor/common/core/characterClassifier';
1111

1212
export class CommitCharacterController {
1313

14-
private _disposables: IDisposable[] = [];
14+
private readonly _disposables = new DisposableStore();
1515

1616
private _active?: {
1717
readonly acceptCharacters: CharacterSet;
@@ -20,11 +20,11 @@ export class CommitCharacterController {
2020

2121
constructor(editor: ICodeEditor, widget: SuggestWidget, accept: (selected: ISelectedSuggestion) => any) {
2222

23-
this._disposables.push(widget.onDidShow(() => this._onItem(widget.getFocusedItem())));
24-
this._disposables.push(widget.onDidFocus(this._onItem, this));
25-
this._disposables.push(widget.onDidHide(this.reset, this));
23+
this._disposables.add(widget.onDidShow(() => this._onItem(widget.getFocusedItem())));
24+
this._disposables.add(widget.onDidFocus(this._onItem, this));
25+
this._disposables.add(widget.onDidHide(this.reset, this));
2626

27-
this._disposables.push(editor.onWillType(text => {
27+
this._disposables.add(editor.onWillType(text => {
2828
if (this._active) {
2929
const ch = text.charCodeAt(text.length - 1);
3030
if (this._active.acceptCharacters.has(ch) && editor.getConfiguration().contribInfo.acceptSuggestionOnCommitCharacter) {
@@ -54,6 +54,6 @@ export class CommitCharacterController {
5454
}
5555

5656
dispose() {
57-
dispose(this._disposables);
57+
this._disposables.dispose();
5858
}
5959
}

src/vs/editor/contrib/suggest/suggestController.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { alert } from 'vs/base/browser/ui/aria/aria';
77
import { isNonEmptyArray } from 'vs/base/common/arrays';
88
import { onUnexpectedError } from 'vs/base/common/errors';
99
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
10-
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
10+
import { dispose, IDisposable, DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
1111
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
1212
import { EditorAction, EditorCommand, registerEditorAction, registerEditorCommand, registerEditorContribution, ServicesAccessor } from 'vs/editor/browser/editorExtensions';
1313
import { EditOperation } from 'vs/editor/common/core/editOperation';
@@ -45,7 +45,7 @@ export class SuggestController implements IEditorContribution {
4545
private readonly _model: SuggestModel;
4646
private readonly _widget: IdleValue<SuggestWidget>;
4747
private readonly _alternatives: IdleValue<SuggestAlternatives>;
48-
private _toDispose: IDisposable[] = [];
48+
private readonly _toDispose = new DisposableStore();
4949

5050
private readonly _sticky = false; // for development purposes only
5151

@@ -63,23 +63,21 @@ export class SuggestController implements IEditorContribution {
6363

6464
const widget = this._instantiationService.createInstance(SuggestWidget, this._editor);
6565

66-
this._toDispose.push(widget);
67-
this._toDispose.push(widget.onDidSelect(item => this._insertSuggestion(item, false, true), this));
66+
this._toDispose.add(widget);
67+
this._toDispose.add(widget.onDidSelect(item => this._insertSuggestion(item, false, true), this));
6868

6969
// Wire up logic to accept a suggestion on certain characters
7070
const commitCharacterController = new CommitCharacterController(this._editor, widget, item => this._insertSuggestion(item, false, true));
71-
this._toDispose.push(
72-
commitCharacterController,
73-
this._model.onDidSuggest(e => {
74-
if (e.completionModel.items.length === 0) {
75-
commitCharacterController.reset();
76-
}
77-
})
78-
);
71+
this._toDispose.add(commitCharacterController);
72+
this._toDispose.add(this._model.onDidSuggest(e => {
73+
if (e.completionModel.items.length === 0) {
74+
commitCharacterController.reset();
75+
}
76+
}));
7977

8078
// Wire up makes text edit context key
8179
let makesTextEdit = SuggestContext.MakesTextEdit.bindTo(this._contextKeyService);
82-
this._toDispose.push(widget.onDidFocus(({ item }) => {
80+
this._toDispose.add(widget.onDidFocus(({ item }) => {
8381

8482
const position = this._editor.getPosition()!;
8583
const startColumn = item.completion.range.startColumn;
@@ -103,36 +101,32 @@ export class SuggestController implements IEditorContribution {
103101
}
104102
makesTextEdit.set(value);
105103
}));
106-
this._toDispose.push({
107-
dispose() { makesTextEdit.reset(); }
108-
});
104+
this._toDispose.add(toDisposable(() => makesTextEdit.reset()));
109105

110106
return widget;
111107
});
112108

113109
this._alternatives = new IdleValue(() => {
114-
let res = new SuggestAlternatives(this._editor, this._contextKeyService);
115-
this._toDispose.push(res);
116-
return res;
110+
return this._toDispose.add(new SuggestAlternatives(this._editor, this._contextKeyService));
117111
});
118112

119-
this._toDispose.push(_instantiationService.createInstance(WordContextKey, _editor));
113+
this._toDispose.add(_instantiationService.createInstance(WordContextKey, _editor));
120114

121-
this._toDispose.push(this._model.onDidTrigger(e => {
115+
this._toDispose.add(this._model.onDidTrigger(e => {
122116
this._widget.getValue().showTriggered(e.auto, e.shy ? 250 : 50);
123117
}));
124-
this._toDispose.push(this._model.onDidSuggest(e => {
118+
this._toDispose.add(this._model.onDidSuggest(e => {
125119
if (!e.shy) {
126120
let index = this._memoryService.select(this._editor.getModel()!, this._editor.getPosition()!, e.completionModel.items);
127121
this._widget.getValue().showSuggestions(e.completionModel, index, e.isFrozen, e.auto);
128122
}
129123
}));
130-
this._toDispose.push(this._model.onDidCancel(e => {
124+
this._toDispose.add(this._model.onDidCancel(e => {
131125
if (this._widget && !e.retrigger) {
132126
this._widget.getValue().hideWidget();
133127
}
134128
}));
135-
this._toDispose.push(this._editor.onDidBlurEditorWidget(() => {
129+
this._toDispose.add(this._editor.onDidBlurEditorWidget(() => {
136130
if (!this._sticky) {
137131
this._model.cancel();
138132
this._model.clear();
@@ -145,7 +139,7 @@ export class SuggestController implements IEditorContribution {
145139
const { acceptSuggestionOnEnter } = this._editor.getConfiguration().contribInfo;
146140
acceptSuggestionsOnEnter.set(acceptSuggestionOnEnter === 'on' || acceptSuggestionOnEnter === 'smart');
147141
};
148-
this._toDispose.push(this._editor.onDidChangeConfiguration((e) => updateFromConfig()));
142+
this._toDispose.add(this._editor.onDidChangeConfiguration((e) => updateFromConfig()));
149143
updateFromConfig();
150144
}
151145

@@ -155,7 +149,7 @@ export class SuggestController implements IEditorContribution {
155149
}
156150

157151
dispose(): void {
158-
this._toDispose = dispose(this._toDispose);
152+
this._toDispose.dispose();
159153
this._widget.dispose();
160154
if (this._model) {
161155
this._model.dispose();

0 commit comments

Comments
 (0)