Skip to content

Commit ad7cbfd

Browse files
committed
Use toDisposable in more places in editor
1 parent f1e0505 commit ad7cbfd

8 files changed

Lines changed: 71 additions & 89 deletions

File tree

src/vs/editor/common/modes/languageConfigurationRegistry.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Event, Emitter } from 'vs/base/common/event';
1313
import { ITextModel } from 'vs/editor/common/model';
1414
import { onUnexpectedError } from 'vs/base/common/errors';
1515
import * as strings from 'vs/base/common/strings';
16-
import { IDisposable } from 'vs/base/common/lifecycle';
16+
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
1717
import { DEFAULT_WORD_REGEXP, ensureValidWordDefinition } from 'vs/editor/common/model/wordHelper';
1818
import { createScopedLineTokens } from 'vs/editor/common/modes/supports';
1919
import { LineTokens } from 'vs/editor/common/core/lineTokens';
@@ -189,14 +189,12 @@ export class LanguageConfigurationRegistryImpl {
189189
let current = new RichEditSupport(languageIdentifier, previous, configuration);
190190
this._entries[languageIdentifier.id] = current;
191191
this._onDidChange.fire({ languageIdentifier });
192-
return {
193-
dispose: () => {
194-
if (this._entries[languageIdentifier.id] === current) {
195-
this._entries[languageIdentifier.id] = previous;
196-
this._onDidChange.fire({ languageIdentifier });
197-
}
192+
return toDisposable(() => {
193+
if (this._entries[languageIdentifier.id] === current) {
194+
this._entries[languageIdentifier.id] = previous;
195+
this._onDidChange.fire({ languageIdentifier });
198196
}
199-
};
197+
});
200198
}
201199

202200
private _getRichEditSupport(languageId: LanguageId): RichEditSupport {

src/vs/editor/common/modes/languageFeatureRegistry.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77

88
import { Event, Emitter } from 'vs/base/common/event';
9-
import { IDisposable } from 'vs/base/common/lifecycle';
9+
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
1010
import { ITextModel } from 'vs/editor/common/model';
1111
import { LanguageSelector, score } from 'vs/editor/common/modes/languageSelector';
1212
import { shouldSynchronizeModel } from 'vs/editor/common/services/modelService';
@@ -54,19 +54,17 @@ export default class LanguageFeatureRegistry<T> {
5454
this._lastCandidate = undefined;
5555
this._onDidChange.fire(this._entries.length);
5656

57-
return {
58-
dispose: () => {
59-
if (entry) {
60-
let idx = this._entries.indexOf(entry);
61-
if (idx >= 0) {
62-
this._entries.splice(idx, 1);
63-
this._lastCandidate = undefined;
64-
this._onDidChange.fire(this._entries.length);
65-
entry = undefined;
66-
}
57+
return toDisposable(() => {
58+
if (entry) {
59+
let idx = this._entries.indexOf(entry);
60+
if (idx >= 0) {
61+
this._entries.splice(idx, 1);
62+
this._lastCandidate = undefined;
63+
this._onDidChange.fire(this._entries.length);
64+
entry = undefined;
6765
}
6866
}
69-
};
67+
});
7068
}
7169

7270
has(model: ITextModel): boolean {

src/vs/editor/common/modes/tokenizationRegistry.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { IDisposable } from 'vs/base/common/lifecycle';
7+
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
88
import { Event, Emitter } from 'vs/base/common/event';
99
import { ColorId, ITokenizationRegistry, ITokenizationSupport, ITokenizationSupportChangedEvent } from 'vs/editor/common/modes';
1010
import { Color } from 'vs/base/common/color';
@@ -33,15 +33,13 @@ export class TokenizationRegistryImpl implements ITokenizationRegistry {
3333
public register(language: string, support: ITokenizationSupport): IDisposable {
3434
this._map[language] = support;
3535
this.fire([language]);
36-
return {
37-
dispose: () => {
38-
if (this._map[language] !== support) {
39-
return;
40-
}
41-
delete this._map[language];
42-
this.fire([language]);
36+
return toDisposable(() => {
37+
if (this._map[language] !== support) {
38+
return;
4339
}
44-
};
40+
delete this._map[language];
41+
this.fire([language]);
42+
});
4543
}
4644

4745
public get(language: string): ITokenizationSupport {

src/vs/editor/common/services/editorWorkerServiceImpl.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'use strict';
66

77
import { IntervalTimer, ShallowCancelThenPromise, wireCancellationToken } from 'vs/base/common/async';
8-
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
8+
import { Disposable, IDisposable, dispose, toDisposable } from 'vs/base/common/lifecycle';
99
import URI from 'vs/base/common/uri';
1010
import { TPromise } from 'vs/base/common/winjs.base';
1111
import { SimpleWorkerClient, logOnceWebWorkerWarning } from 'vs/base/common/worker/simpleWorker';
@@ -285,11 +285,9 @@ class EditorModelManager extends Disposable {
285285
toDispose.push(model.onWillDispose(() => {
286286
this._stopModelSync(modelUrl);
287287
}));
288-
toDispose.push({
289-
dispose: () => {
290-
this._proxy.acceptRemovedModel(modelUrl);
291-
}
292-
});
288+
toDispose.push(toDisposable(() => {
289+
this._proxy.acceptRemovedModel(modelUrl);
290+
}));
293291

294292
this._syncedModels[modelUrl] = toDispose;
295293
}

src/vs/editor/common/view/viewEvents.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Selection } from 'vs/editor/common/core/selection';
99
import { ScrollEvent } from 'vs/base/common/scrollable';
1010
import { IConfigurationChangedEvent } from 'vs/editor/common/config/editorOptions';
1111
import * as errors from 'vs/base/common/errors';
12-
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
12+
import { IDisposable, Disposable, toDisposable } from 'vs/base/common/lifecycle';
1313
import { ScrollType } from 'vs/editor/common/editorCommon';
1414

1515
export const enum ViewEventType {
@@ -354,17 +354,15 @@ export class ViewEventEmitter extends Disposable {
354354

355355
public addEventListener(listener: (events: ViewEvent[]) => void): IDisposable {
356356
this._listeners.push(listener);
357-
return {
358-
dispose: () => {
359-
let listeners = this._listeners;
360-
for (let i = 0, len = listeners.length; i < len; i++) {
361-
if (listeners[i] === listener) {
362-
listeners.splice(i, 1);
363-
break;
364-
}
357+
return toDisposable(() => {
358+
let listeners = this._listeners;
359+
for (let i = 0, len = listeners.length; i < len; i++) {
360+
if (listeners[i] === listener) {
361+
listeners.splice(i, 1);
362+
break;
365363
}
366364
}
367-
};
365+
});
368366
}
369367
}
370368

src/vs/editor/contrib/codelens/codelensController.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55

66
'use strict';
77

8-
import { RunOnceScheduler, CancelablePromise, createCancelablePromise } from 'vs/base/common/async';
8+
import { CancelablePromise, createCancelablePromise, RunOnceScheduler } from 'vs/base/common/async';
99
import { onUnexpectedError } from 'vs/base/common/errors';
10-
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
11-
import { ICommandService } from 'vs/platform/commands/common/commands';
12-
import * as editorCommon from 'vs/editor/common/editorCommon';
13-
import { CodeLensProviderRegistry, ICodeLensSymbol } from 'vs/editor/common/modes';
10+
import { dispose, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
11+
import { StableEditorScrollState } from 'vs/editor/browser/core/editorState';
1412
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
1513
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
16-
import { ICodeLensData, getCodeLensData } from './codelens';
1714
import { IConfigurationChangedEvent } from 'vs/editor/common/config/editorOptions';
18-
import { CodeLens, CodeLensHelper } from 'vs/editor/contrib/codelens/codelensWidget';
15+
import * as editorCommon from 'vs/editor/common/editorCommon';
1916
import { IModelDecorationsChangeAccessor } from 'vs/editor/common/model';
17+
import { CodeLensProviderRegistry, ICodeLensSymbol } from 'vs/editor/common/modes';
18+
import { CodeLens, CodeLensHelper } from 'vs/editor/contrib/codelens/codelensWidget';
19+
import { ICommandService } from 'vs/platform/commands/common/commands';
2020
import { INotificationService } from 'vs/platform/notification/common/notification';
21-
import { StableEditorScrollState } from 'vs/editor/browser/core/editorState';
21+
import { getCodeLensData, ICodeLensData } from './codelens';
2222

2323
export class CodeLensContribution implements editorCommon.IEditorContribution {
2424

@@ -167,22 +167,20 @@ export class CodeLensContribution implements editorCommon.IEditorContribution {
167167
this._localToDispose.push(this._editor.onDidLayoutChange(e => {
168168
this._detectVisibleLenses.schedule();
169169
}));
170-
this._localToDispose.push({
171-
dispose: () => {
172-
if (this._editor.getModel()) {
173-
const scrollState = StableEditorScrollState.capture(this._editor);
174-
this._editor.changeDecorations((changeAccessor) => {
175-
this._editor.changeViewZones((accessor) => {
176-
this._disposeAllLenses(changeAccessor, accessor);
177-
});
170+
this._localToDispose.push(toDisposable(() => {
171+
if (this._editor.getModel()) {
172+
const scrollState = StableEditorScrollState.capture(this._editor);
173+
this._editor.changeDecorations((changeAccessor) => {
174+
this._editor.changeViewZones((accessor) => {
175+
this._disposeAllLenses(changeAccessor, accessor);
178176
});
179-
scrollState.restore(this._editor);
180-
} else {
181-
// No accessors available
182-
this._disposeAllLenses(null, null);
183-
}
177+
});
178+
scrollState.restore(this._editor);
179+
} else {
180+
// No accessors available
181+
this._disposeAllLenses(null, null);
184182
}
185-
});
183+
}));
186184

187185
scheduler.schedule();
188186
}

src/vs/editor/standalone/browser/simpleServices.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti
2424
import { IProgressService, IProgressRunner } from 'vs/platform/progress/common/progress';
2525
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
2626
import { ITextModelService, ITextModelContentProvider, ITextEditorModel } from 'vs/editor/common/services/resolverService';
27-
import { IDisposable, IReference, ImmortalReference, combinedDisposable } from 'vs/base/common/lifecycle';
27+
import { IDisposable, IReference, ImmortalReference, combinedDisposable, toDisposable } from 'vs/base/common/lifecycle';
2828
import * as dom from 'vs/base/browser/dom';
2929
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
3030
import { KeybindingsRegistry, IKeybindingItem } from 'vs/platform/keybinding/common/keybindingsRegistry';
@@ -230,11 +230,9 @@ export class StandaloneCommandService implements ICommandService {
230230
public addCommand(command: ICommand): IDisposable {
231231
const { id } = command;
232232
this._dynamicCommands[id] = command;
233-
return {
234-
dispose: () => {
235-
delete this._dynamicCommands[id];
236-
}
237-
};
233+
return toDisposable(() => {
234+
delete this._dynamicCommands[id];
235+
});
238236
}
239237

240238
public executeCommand<T>(id: string, ...args: any[]): TPromise<T> {
@@ -289,18 +287,16 @@ export class StandaloneKeybindingService extends AbstractKeybindingService {
289287
weight2: 0
290288
});
291289

292-
toDispose.push({
293-
dispose: () => {
294-
for (let i = 0; i < this._dynamicKeybindings.length; i++) {
295-
let kb = this._dynamicKeybindings[i];
296-
if (kb.command === commandId) {
297-
this._dynamicKeybindings.splice(i, 1);
298-
this.updateResolver({ source: KeybindingSource.Default });
299-
return;
300-
}
290+
toDispose.push(toDisposable(() => {
291+
for (let i = 0; i < this._dynamicKeybindings.length; i++) {
292+
let kb = this._dynamicKeybindings[i];
293+
if (kb.command === commandId) {
294+
this._dynamicKeybindings.splice(i, 1);
295+
this.updateResolver({ source: KeybindingSource.Default });
296+
return;
301297
}
302298
}
303-
});
299+
}));
304300

305301
let commandService = this._commandService;
306302
if (commandService instanceof StandaloneCommandService) {

src/vs/editor/standalone/browser/standaloneCodeEditor.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77

8-
import { Disposable, IDisposable, combinedDisposable } from 'vs/base/common/lifecycle';
8+
import { Disposable, IDisposable, combinedDisposable, toDisposable } from 'vs/base/common/lifecycle';
99
import { TPromise } from 'vs/base/common/winjs.base';
1010
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
1111
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
@@ -272,11 +272,9 @@ export class StandaloneCodeEditor extends CodeEditorWidget implements IStandalon
272272

273273
// Store it under the original id, such that trigger with the original id will work
274274
this._actions[id] = internalAction;
275-
toDispose.push({
276-
dispose: () => {
277-
delete this._actions[id];
278-
}
279-
});
275+
toDispose.push(toDisposable(() => {
276+
delete this._actions[id];
277+
}));
280278

281279
return combinedDisposable(toDispose);
282280
}

0 commit comments

Comments
 (0)